Class: JSCommander::ProxyServer::CommandDispatcher::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(req, reconnect_interval) ⇒ Client

Returns a new instance of Client.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jscmd/proxyserver.rb', line 85

def initialize(req, reconnect_interval)
  @request = req
  @queue = Queue.new
  @pop_time = nil
  req_socket = req.instance_eval{@socket}
  @thread = Thread.start do
    sleep 1 until req_socket.eof?
    @queue.push CLIENT_ABORTED
  end
  @reconnect_thread = Thread.start do
    loop do
      if @pop_time && Time.now >= @pop_time + reconnect_interval
        @pop_time = nil
        @queue.push Message.new(nil, :type => "nop")
        break
      end
      sleep 1
    end
  end
end

Instance Method Details

#abortObject



119
120
121
# File 'lib/jscmd/proxyserver.rb', line 119

def abort
  @thread.kill
end

#hostnameObject



106
107
108
109
110
111
112
113
# File 'lib/jscmd/proxyserver.rb', line 106

def hostname
  uri = @request.header["referer"].to_s
  if uri =~ %r{^\w+://([^/]+)}
    $1
  else
    uri
  end
end

#pop_commandObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jscmd/proxyserver.rb', line 123

def pop_command
  @pop_time = Time.now
  r = @queue.pop
  @pop_time = nil
  # thread is automatically stopped if r == CLIENT_ABORTED
  if r != CLIENT_ABORTED
    @thread.kill
  end
  @reconnect_thread.kill
  r
end

#push_command(command) ⇒ Object



115
116
117
# File 'lib/jscmd/proxyserver.rb', line 115

def push_command(command)
  @queue.push(command)
end