Class: Proxi::Server
- Inherits:
-
Object
- Object
- Proxi::Server
- Includes:
- Wisper::Publisher
- Defined in:
- lib/proxi/server.rb
Constant Summary collapse
- MAX_CONNECTIONS =
5
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(listen_port, connection_factory) ⇒ Server
constructor
A new instance of Server.
- #reap_connections ⇒ Object
Constructor Details
#initialize(listen_port, connection_factory) ⇒ Server
Returns a new instance of Server.
7 8 9 10 11 12 13 |
# File 'lib/proxi/server.rb', line 7 def initialize(listen_port, connection_factory) @connection_factory = connection_factory @server = TCPServer.new(nil, listen_port) @connections = [] end |
Instance Method Details
#call ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/proxi/server.rb', line 15 def call loop do in_socket = @server.accept connection = @connection_factory.call(in_socket) broadcast(:new_connection, connection) @connections.push(connection) connection.call reap_connections while @connections.size >= MAX_CONNECTIONS sleep 1 reap_connections end end end |
#reap_connections ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/proxi/server.rb', line 34 def reap_connections @connections = @connections.select do |t| if t.alive? true else t.join_thread false end end end |