Class: SpinalTap::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/spinal_tap/server.rb

Instance Method Summary collapse

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

#startObject



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.message}\n#{e.backtrace.join("\n")}")
        end
      end
    end
  end

  true
end

#stopObject



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

#workersObject



51
52
53
54
55
# File 'lib/spinal_tap/server.rb', line 51

def workers
  @workers_lock.synchronize do
    return @workers.clone
  end
end