Class: JSCommander::ProxyServer::CommandDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/jscmd/proxyserver.rb

Defined Under Namespace

Classes: CLIENT_ABORTED, Client, SHUTDOWN

Instance Method Summary collapse

Constructor Details

#initialize(server, command_queue) ⇒ CommandDispatcher

Returns a new instance of CommandDispatcher.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jscmd/proxyserver.rb', line 36

def initialize(server, command_queue)
  @server = server
  @client_mutex = Mutex.new
  @client_ready = ConditionVariable.new
  @clients = []
  @last_clients = nil
  @thread = Thread.start do
    while server.status != :Shutdown
      command = command_queue.pop
      @client_mutex.synchronize do
        while @clients.empty?
          @client_ready.wait(@client_mutex)
        end
        @clients.first.push_command(command)
      end
    end
  end
end

Instance Method Details

#format_clientsObject



65
66
67
# File 'lib/jscmd/proxyserver.rb', line 65

def format_clients
  @last_clients = @clients.map{|c|"[#{c.hostname}]"}.join(",")
end

#last_clientsObject



61
62
63
# File 'lib/jscmd/proxyserver.rb', line 61

def last_clients
  @last_clients
end

#new_client(req, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jscmd/proxyserver.rb', line 69

def new_client(req, &block)
  client = Client.new(req, @server.reconnect_interval)
  @client_mutex.synchronize do
    @clients << client
    @client_ready.signal
  end
  begin
    yield client
  ensure
    @client_mutex.synchronize do
      @clients.delete(client)
    end
  end
end

#shutdownObject



55
56
57
58
59
# File 'lib/jscmd/proxyserver.rb', line 55

def shutdown
  @clients.each do |client|
    client.push_command SHUTDOWN
  end
end