vrowser 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/VERSION +1 -1
  2. data/lib/vrowser/http_daemon.rb +78 -0
  3. metadata +3 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+ # Author: kimoto
3
+ require 'vrowser'
4
+ require 'webrick'
5
+
6
+ class Vrowser::HTTPDaemon
7
+ def self.start(options={})
8
+ self.new(options) do |instance|
9
+ instance.start
10
+ end
11
+ end
12
+
13
+ public
14
+ def initialize(options={})
15
+ @config_path = options[:config_path] or raise ArgumentError("config_path")
16
+
17
+ @server = WEBrick::HTTPServer.new(options)
18
+ @server.mount_proc("/api/updated/json"){ |req, res|
19
+ res.header["Content-Type"] = "application/json"
20
+ res.body = get_active_servers_nary.to_json
21
+ }
22
+ @server.mount_proc("/api/connected/json"){ |req, res|
23
+ res.header["Content-Type"] = "application/json"
24
+ res.body = get_active_servers.to_json
25
+ }
26
+
27
+ yield(self) if block_given?
28
+ self
29
+ end
30
+
31
+ def start
32
+ @th = Thread.start do
33
+ fetch_and_update @config_path
34
+ end
35
+ regist_stop
36
+ @server.start
37
+ end
38
+
39
+ def stop
40
+ @server.shutdown if @server
41
+ Thread.kill(@th) if @th
42
+ end
43
+
44
+ private
45
+ def regist_stop
46
+ trap("INT") do
47
+ stop
48
+ end
49
+ end
50
+
51
+ def fetch_and_update(config_path)
52
+ Vrowser.load_file(config_path) do |vrowser|
53
+ while true
54
+ puts "update server list"
55
+ vrowser.fetch
56
+
57
+ (60 / 5).times do
58
+ puts "try update"
59
+ vrowser.update
60
+ sleep (60 * 5)
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ def get_active_servers
67
+ Vrowser.load_file(@config_path){ |v|
68
+ query = v.active_servers.select(:name, :host, :ping, :num_players, :type, :map, :players)
69
+ return query.order(:host).map(&:values)
70
+ }
71
+ nil
72
+ end
73
+
74
+ def get_active_servers_nary
75
+ get_active_servers.map(&:values)
76
+ end
77
+ end
78
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - kimoto
@@ -114,6 +114,7 @@ files:
114
114
  - bin/vrowser
115
115
  - examples/config.yml
116
116
  - lib/plugins/l4d2.rb
117
+ - lib/vrowser/http_daemon.rb
117
118
  - lib/vrowser.rb
118
119
  - test/helper.rb
119
120
  - test/test_vrowser.rb