vrowser 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.9
1
+ 0.1.0
@@ -1,63 +1,133 @@
1
- #!/usr/bin/env ruby
1
+ #!/bin/env ruby
2
2
  # encoding: utf-8
3
3
  # Author: kimoto
4
+ require 'trollop'
5
+ require 'pathname'
4
6
  require 'vrowser'
5
- require 'optparse'
6
7
  require 'vrowser/http_daemon'
7
8
  require 'fileutils'
8
9
 
9
- options = {
10
- :host => 'localhost',
11
- :port => '3000',
12
- :document_root => File.expand_path(File.join(File.dirname(__FILE__), '../public_html'))
13
- }
14
- parser = OptionParser.new{ |opts|
15
- opts.banner = "Usage: #{File.basename($0)}"
16
- opts.on("-f", "--config-file=PATH", "specify config file"){ |v|
17
- options[:config_path] = v
18
- }
19
-
20
- opts.on("-h", "--host=HOST", "host"){ |v| options[:host] = v }
21
- opts.on("-p", "--port=PORT", "port"){ |v| options[:port] = v }
22
- opts.on("-r", "--document-root=PATH", "root"){ |v| options[:document_root] = v }
23
-
24
- }
25
- parser.parse!
26
-
27
- if ARGV.first == "sample"
28
- sample_config = File.expand_path(File.join(File.dirname(__FILE__), "../examples/config.yml"))
29
- FileUtils.cp(sample_config, "./config.yml", :verbose => true)
30
- exit(0)
31
- end
10
+ class VrowserCLI
11
+ def self.run(argv)
12
+ self.new.parse(argv)
13
+ end
32
14
 
33
- if options[:config_path].nil?
34
- parser.help.display
35
- exit(1)
36
- end
15
+ # get subcommand names
16
+ def self.sub_commands
17
+ self.instance_methods.map(&:to_s).grep(/command_/).map{ |command_symbol|
18
+ command_symbol.to_s.gsub(/^command_/, "")
19
+ }
20
+ end
21
+
22
+ def initialize
23
+ end
24
+
25
+ def parse(argv)
26
+ @argv = argv
27
+ sub_commands = self.class.sub_commands
28
+
29
+ global_opts = Trollop::options do
30
+ banner <<-EOS
31
+ Usage: #{File.basename($0)} [#{sub_commands.join(',')}]
32
+ hoge
33
+ EOS
34
+ version File.read(Pathname.new(__FILE__).dirname.realpath + "../VERSION")
35
+ stop_on sub_commands
36
+ end
37
+
38
+ cmd = @argv.shift
39
+ cmd_opts = Trollop::options do
40
+ case cmd
41
+ when "sample"
42
+ opt :output_path, "output path", :short => "-o", :type => String, :default => "./config.yml"
43
+ when "list"
44
+ opt :config_file, "config file path", :short => "-f", :type => String, :required => true
45
+ when "fetch"
46
+ opt :config_file, "config file path", :short => "-f", :type => String, :required => true
47
+ when "update"
48
+ opt :config_file, "config file path", :short => "-f", :type => String, :required => true
49
+ when "json"
50
+ opt :config_file, "config file path", :short => "-f", :type => String, :required => true
51
+ when "server", "daemon"
52
+ opt :config_file, "config file path", :short => "-f", :type => String, :required => true
53
+ opt :port, "port number", :short => "-p", :type => String, :default => '3000'
54
+ opt :host, "host or ip address", :short => "-h", :type => String, :default => 'localhost'
55
+ opt :log_path, "log file path", :short => "-l", :default => STDERR
56
+ opt :document_root, "document root path", :short => "-d", :type => String,
57
+ :default => (Pathname.new(__FILE__).dirname.realpath + '../public_html').to_s
58
+ else
59
+ Trollop::die "unknown subcommand: #{cmd.inspect}"
60
+ end
61
+ end
62
+
63
+ if sub_commands.include? cmd
64
+ self.send("command_" + cmd, cmd_opts)
65
+ return 0
66
+ else
67
+ cmd_opts.help.display
68
+ end
69
+ end
70
+
71
+ ### define sub commands
72
+ def command_list(options)
73
+ Vrowser.load_file(options[:config_file]) do |vrowser|
74
+ puts vrowser.active_servers.map(&:name).join($/)
75
+ end
76
+ end
77
+
78
+ def command_sample(options)
79
+ sample_config_path = (Pathname.new(__FILE__).dirname + "../examples/config.yml").realpath
80
+ output_path = options[:output_path]
81
+ if File.exist? output_path
82
+ STDERR.puts "Already file exists!: #{output_path}"
83
+ else
84
+ FileUtils.cp(sample_config_path, options[:output_path])
85
+ STDOUT.puts "Generated sample config file: #{output_path}"
86
+ end
87
+ end
88
+
89
+ def command_update(options)
90
+ Vrowser.load_file(options[:config_file]) do |vrowser|
91
+ vrowser.update
92
+ vrowser.clear
93
+ end
94
+ end
95
+
96
+ def command_json(options)
97
+ Vrowser.load_file(options[:config_file]) do |vrowser|
98
+ greped = vrowser.active_servers.select(:name, :host, :ping, :num_players, :type, :map, :players)
99
+ ordered = greped.order(:host)
100
+ ordered.map(&:values).to_json.display
101
+ end
102
+ end
103
+
104
+ def command_server(options)
105
+ execute_as_server(options.merge({:damonize => false}))
106
+ end
37
107
 
38
- Vrowser.load_file(options[:config_path]) do |vrowser|
39
- case sub_command = ARGV.shift
40
- when "fetch"
41
- vrowser.fetch
42
- when "update"
43
- vrowser.update
44
- vrowser.clear
45
- when "list"
46
- puts vrowser.servers.map(&:name).join($/)
47
- when "json"
48
- vrowser.active_servers.select(:name, :host, :ping, :num_players, :type, :map, :players).order(:host).map(&:values).to_json.display
49
- when "daemon"
50
- unless options[:host] and options[:port] and options[:document_root]
51
- raise ArgumentError
52
- end
53
-
54
- Vrowser::HTTPDaemon.start(
55
- :Host => options[:host],
56
- :Port => options[:port],
57
- :DocumentRoot => options[:document_root],
58
- :config_path => options[:config_path])
59
- else
60
- raise ArgumentError(sub_command)
108
+ def command_daemon(options)
109
+ execute_as_server(options.merge({:daemonize => true}))
110
+ end
111
+
112
+ private
113
+ def execute_as_server(options)
114
+ Vrowser::HTTPDaemon.new(
115
+ :config_path => Pathname.new(options[:config_file]).realpath,
116
+ :BindAddress => options[:host],
117
+ :Port => options[:port],
118
+ :DocumentRoot => Pathname.new(options[:document_root]).realpath,
119
+ # :Logger => logger
120
+ ) do |vrowser|
121
+ if options[:daemonize]
122
+ vrowser.daemonize!
123
+ vrowser.start
124
+ else
125
+ vrowser.start
126
+ end
127
+ end
61
128
  end
62
129
  end
63
130
 
131
+ Vrowser.logger = Logger.new(STDERR)
132
+ exit VrowserCLI.run(ARGV)
133
+
@@ -36,6 +36,10 @@ class Vrowser::HTTPDaemon
36
36
  @server.start
37
37
  end
38
38
 
39
+ def daemonize!
40
+ Process.daemon
41
+ end
42
+
39
43
  def stop
40
44
  @server.shutdown if @server
41
45
  Thread.kill(@th) if @th
@@ -2,15 +2,15 @@
2
2
  <html>
3
3
  <head>
4
4
  <meta charset="UTF-8">
5
- <script src="/js/jquery.min.js"></script>
6
- <script src="/js/jquery-ui-1.8.16.custom.min.js"></script>
7
- <script src="/js/jquery.tmpl.min.js"></script>
8
- <script src="/js/jquery.dataTables.nightly.min.js"></script>
9
- <script src="/js/vrowser.js"></script>
10
- <link rel="StyleSheet" href="/css/main.css" />
11
- <link rel="StyleSheet" href="/css/demo_page.css" />
12
- <link rel="StyleSheet" href="/css/demo_table.css" />
13
- <link rel="StyleSheet" href="/css/demo_table_jui.css" />
5
+ <script src="./js/jquery.min.js"></script>
6
+ <script src="./js/jquery-ui-1.8.16.custom.min.js"></script>
7
+ <script src="./js/jquery.tmpl.min.js"></script>
8
+ <script src="./js/jquery.dataTables.nightly.min.js"></script>
9
+ <script src="./js/vrowser.js"></script>
10
+ <link rel="StyleSheet" href="./css/main.css" />
11
+ <link rel="StyleSheet" href="./css/demo_page.css" />
12
+ <link rel="StyleSheet" href="./css/demo_table.css" />
13
+ <link rel="StyleSheet" href="./css/demo_table_jui.css" />
14
14
  </head>
15
15
 
16
16
  <body>
@@ -1,7 +1,7 @@
1
1
  var server_browser = null;
2
2
  var get_new_data = null;
3
3
  $(document).ready(function(){
4
- $.getJSON("/api/connected/json", function(data){
4
+ $.getJSON("./api/connected/json", function(data){
5
5
  $("#myTemplate").tmpl(data).appendTo("#servers");
6
6
 
7
7
  $("#servers tbody tr").live("dblclick", function(){
@@ -43,7 +43,7 @@ $(document).ready(function(){
43
43
  get_new_data = function(){
44
44
  $.ajax({
45
45
  'dataType': 'json',
46
- 'url': '/api/updated/json',
46
+ 'url': './api/updated/json',
47
47
  'cache': true,
48
48
  'success': function(data){
49
49
  server_browser.fnClearTable(false);
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 9
9
- version: 0.0.9
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - kimoto