Class: WsProxy

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

Constant Summary collapse

PORTS =

Allowed ports to communicate with our web sockets proxy

5910..5930

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ WsProxy


12
13
14
15
16
17
# File 'lib/ws_proxy.rb', line 12

def initialize(attributes)
  # setup all attributes.
  defaults.merge(attributes).each do |k, v|
    eval("self.#{k}= v") if self.respond_to?("#{k}=")
  end
end

Instance Attribute Details

#hostObject

Returns the value of attribute host


6
7
8
# File 'lib/ws_proxy.rb', line 6

def host
  @host
end

#host_portObject

Returns the value of attribute host_port


6
7
8
# File 'lib/ws_proxy.rb', line 6

def host_port
  @host_port
end

#idle_timeoutObject

Returns the value of attribute idle_timeout


6
7
8
# File 'lib/ws_proxy.rb', line 6

def idle_timeout
  @idle_timeout
end

#passwordObject

Returns the value of attribute password


6
7
8
# File 'lib/ws_proxy.rb', line 6

def password
  @password
end

#proxy_portObject (readonly)

Returns the value of attribute proxy_port


7
8
9
# File 'lib/ws_proxy.rb', line 7

def proxy_port
  @proxy_port
end

#ssl_targetObject

Returns the value of attribute ssl_target


6
7
8
# File 'lib/ws_proxy.rb', line 6

def ssl_target
  @ssl_target
end

#timeoutObject

Returns the value of attribute timeout


6
7
8
# File 'lib/ws_proxy.rb', line 6

def timeout
  @timeout
end

Class Method Details

.start(attributes) ⇒ Object


19
20
21
22
# File 'lib/ws_proxy.rb', line 19

def self.start attributes
  proxy = WsProxy.new(attributes)
  proxy.start_proxy
end

Instance Method Details

#start_proxyObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ws_proxy.rb', line 24

def start_proxy

  # try to execute our web sockets proxy
  port = PORTS.first
  begin
    cmd  = "#{ws_proxy} --daemon --idle-timeout=#{idle_timeout} --timeout=#{timeout} #{port} #{host}:#{host_port}"
    cmd += " --ssl-target" if ssl_target
    execute(cmd)
    # if the port is already in use, try another one from the pool
    # this is not ideal, as it would try all ports in order
    # but it avoids any threading issues etc.
    # TODO: try to select a port from a pool randomly, so we always hit all active connections.
  rescue PortInUse
    port += 1
    retry if port <= PORTS.last
  end
  @proxy_port = port

  { :host => host, :port => host_port, :password => password, :proxy_port => proxy_port }
end