Class: JCukeForker::StatusServer
- Inherits:
-
Object
- Object
- JCukeForker::StatusServer
- Includes:
- Observable
- Defined in:
- lib/jcukeforker/status_server.rb
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #handle_connection(socket) ⇒ Object
-
#initialize(port = '6333') ⇒ StatusServer
constructor
A new instance of StatusServer.
- #run ⇒ Object
- #shutdown ⇒ Object
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
#port ⇒ Object (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 = socket.gets json_obj = JSON.parse fire json_obj.first, *json_obj[1..-1] end socket.close end |
#run ⇒ Object
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 |
#shutdown ⇒ Object
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 |