Class: ForwardMachine::Forwarder
- Inherits:
-
Object
- Object
- ForwardMachine::Forwarder
- Defined in:
- lib/forwardmachine/forwarder.rb
Overview
Server which accepts traffic on available port taken from ports pool. Each connection is handled by ForwarderConnection object
Constant Summary collapse
- FIRST_USE_TIMEOUT =
How long server will be open, waiting for the first connetion.
60
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#ports_pool ⇒ Object
readonly
Returns the value of attribute ports_pool.
Instance Method Summary collapse
-
#forwarder_connection_closed ⇒ Object
Internal: Callback which is called from connection to Forwarder when client disconnects.
-
#initialize(host, destination, ports_pool) ⇒ Forwarder
constructor
Public: Initialize new Forwarder server.
-
#socket_address ⇒ Object
Public: Fowarder socket address Returns: String with host and port on which forwarder listens.
-
#start ⇒ Object
Public: Start forwarding server on given host and port taken from PortsPool.
- #to_s ⇒ Object
Constructor Details
#initialize(host, destination, ports_pool) ⇒ Forwarder
Public: Initialize new Forwarder server
host - Host as String on which server will listen destination - Destination socket as String where traffic will
be forwarded (in format host:port)
ports - PortsPool object with ports numbers from which port
for forwarder will be taken
17 18 19 20 21 22 23 |
# File 'lib/forwardmachine/forwarder.rb', line 17 def initialize(host, destination, ports_pool) @host = host @ports_pool = ports_pool @port = ports_pool.reserve @destination = destination @connections = 0 end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
8 9 10 |
# File 'lib/forwardmachine/forwarder.rb', line 8 def connections @connections end |
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
8 9 10 |
# File 'lib/forwardmachine/forwarder.rb', line 8 def destination @destination end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
8 9 10 |
# File 'lib/forwardmachine/forwarder.rb', line 8 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
8 9 10 |
# File 'lib/forwardmachine/forwarder.rb', line 8 def port @port end |
#ports_pool ⇒ Object (readonly)
Returns the value of attribute ports_pool.
8 9 10 |
# File 'lib/forwardmachine/forwarder.rb', line 8 def ports_pool @ports_pool end |
Instance Method Details
#forwarder_connection_closed ⇒ Object
Internal: Callback which is called from connection to Forwarder when client disconnects. Stops Forwarder server if it’s not used by any connection.
43 44 45 |
# File 'lib/forwardmachine/forwarder.rb', line 43 def forwarder_connection_closed stop if (@connections -= 1).zero? end |
#socket_address ⇒ Object
Public: Fowarder socket address Returns: String with host and port on which forwarder listens
49 50 51 |
# File 'lib/forwardmachine/forwarder.rb', line 49 def socket_address "#{host}:#{port}" end |
#start ⇒ Object
Public: Start forwarding server on given host and port taken from PortsPool. Returns: Socket address of the server in format “host:port” as String
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/forwardmachine/forwarder.rb', line 27 def start @server = EM.start_server(host, port, ForwarderConnection, destination, self) do @connections += 1 @inactivity_timer.cancel end @inactivity_timer = EM::PeriodicTimer.new(FIRST_USE_TIMEOUT) do logger.info("Forwarder #{self} timed out after #{FIRST_USE_TIMEOUT} seconds") stop end logger.info("Started forwarder #{self}") socket_address end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/forwardmachine/forwarder.rb', line 53 def to_s "#{socket_address}->#{destination}" end |