Class: SpinalTap::Server
- Inherits:
-
Object
- Object
- SpinalTap::Server
- Defined in:
- lib/spinal_tap/server.rb
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Server
constructor
A new instance of Server.
- #register(thread, client) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #unregister(thread) ⇒ Object
- #workers ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Server
Returns a new instance of Server.
6 7 8 9 10 11 12 13 |
# File 'lib/spinal_tap/server.rb', line 6 def initialize(params = {}) @host = params[:host] || '127.0.0.1' @port = params[:port] || 9000 @running = false @workers = {} @workers_lock = Mutex.new end |
Instance Method Details
#register(thread, client) ⇒ Object
57 58 59 60 61 |
# File 'lib/spinal_tap/server.rb', line 57 def register(thread, client) @workers_lock.synchronize do @workers[thread] = client end end |
#start ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/spinal_tap/server.rb', line 15 def start return false if @running @running = true @listener_thread = Thread.new do @server_sock = TCPServer.new(@host, @port) while true Thread.new(@server_sock.accept) do |client| begin register(Thread.current, client) client.extend(SpinalTap::ClientHelpers) client.setup(self) client.process_loop rescue Exception => e puts("WORKER DIED: #{e.}\n#{e.backtrace.join("\n")}") end end end end true end |
#stop ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/spinal_tap/server.rb', line 42 def stop return false unless @running Thread.kill(@listener_thread) @server_sock.close true end |
#unregister(thread) ⇒ Object
63 64 65 66 67 |
# File 'lib/spinal_tap/server.rb', line 63 def unregister(thread) @workers_lock.synchronize do @workers.delete(thread) end end |
#workers ⇒ Object
51 52 53 54 55 |
# File 'lib/spinal_tap/server.rb', line 51 def workers @workers_lock.synchronize do return @workers.clone end end |