Class: Vrowser::HTTPDaemon

Inherits:
Object
  • Object
show all
Defined in:
lib/vrowser/http_daemon.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ HTTPDaemon

Returns a new instance of HTTPDaemon.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vrowser/http_daemon.rb', line 14

def initialize(options={})
  @config_path = options[:config_path] or raise ArgumentError("config_path")

  @server = WEBrick::HTTPServer.new(options)
  @server.mount_proc("/api/updated/json"){ |req, res|
    res.header["Content-Type"] = "application/json"
    res.body = get_active_servers_nary.to_json
  }
  @server.mount_proc("/api/connected/json"){ |req, res|
    res.header["Content-Type"] = "application/json"
    res.body = get_active_servers.to_json
  }

  yield(self) if block_given?
  self
end

Class Method Details

.start(options = {}) ⇒ Object



7
8
9
10
11
# File 'lib/vrowser/http_daemon.rb', line 7

def self.start(options={})
  self.new(options) do |instance|
    instance.start
  end
end

Instance Method Details

#daemonize!Object



39
40
41
# File 'lib/vrowser/http_daemon.rb', line 39

def daemonize!
  Process.daemon
end

#startObject



31
32
33
34
35
36
37
# File 'lib/vrowser/http_daemon.rb', line 31

def start
  @th = Thread.start do
    fetch_and_update @config_path
  end
  regist_stop
  @server.start
end

#stopObject



43
44
45
46
# File 'lib/vrowser/http_daemon.rb', line 43

def stop
  @server.shutdown if @server
  Thread.kill(@th) if @th
end