Class: JCukeForker::StatusServer

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/jcukeforker/status_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = '6333') ⇒ StatusServer

Returns a new instance of StatusServer.



9
10
11
12
13
# File 'lib/jcukeforker/status_server.rb', line 9

def initialize(port = '6333')
  @server = ::TCPServer.new 'localhost', port
  @port = @server.connect_address.ip_port
  @thread_pool = []
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/jcukeforker/status_server.rb', line 7

def port
  @port
end

Instance Method Details

#handle_connection(socket) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/jcukeforker/status_server.rb', line 32

def handle_connection(socket)
  until socket.eof? do
    raw_message = socket.gets
    json_obj = JSON.parse raw_message
    fire json_obj.first, *json_obj[1..-1]
  end
  socket.close
end

#runObject



15
16
17
18
19
20
21
22
# File 'lib/jcukeforker/status_server.rb', line 15

def run
  @master_thread = Thread.new do
    loop do
      socket = @server.accept
      @thread_pool << Thread.new { handle_connection(socket) }
    end
  end
end

#shutdownObject



24
25
26
27
28
29
30
# File 'lib/jcukeforker/status_server.rb', line 24

def shutdown
  if @server
    @server.close
    @master_thread.terminate
    @thread_pool.each(&:terminate)
  end
end