Class: Received::Server
- Inherits:
-
Object
- Object
- Received::Server
- Defined in:
- lib/received/server.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#idle? ⇒ Boolean
Checks if the server is processing any connections.
-
#initialize(options) ⇒ Server
constructor
A new instance of Server.
- #logger ⇒ Object
- #remove_connection(conn) ⇒ Object
- #serve! ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
-
#stopping? ⇒ Boolean
Checks whether the server is in stopping mode.
Constructor Details
#initialize(options) ⇒ Server
Returns a new instance of Server.
10 11 12 13 14 15 16 17 |
# File 'lib/received/server.rb', line 10 def initialize() @options = Received.logger ||= ([:logger] || Logger.new(STDERR)) @connections = [] # For how long the server will wait for connections to finish @grace_period = [:grace_period] || 10 create_backend end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/received/server.rb', line 8 def @options end |
Instance Method Details
#idle? ⇒ Boolean
Checks if the server is processing any connections
57 58 59 |
# File 'lib/received/server.rb', line 57 def idle? @connections.empty? end |
#remove_connection(conn) ⇒ Object
61 62 63 64 |
# File 'lib/received/server.rb', line 61 def remove_connection(conn) @connections.delete(conn) set_title end |
#serve! ⇒ Object
19 20 21 |
# File 'lib/received/server.rb', line 19 def serve! EventMachine.run { start } end |
#start ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/received/server.rb', line 23 def start unless [:unix_socket] or [:port] raise "No port or UNIX socket path were provided" end set_title if host = [:unix_socket] port = nil else host = [:host] || '127.0.0.1' port = [:port] end logger.info "Starting server on #{host}#{port ? ":" + port.to_s : ''}" @signature = EventMachine.start_server(host, port, Received::Connection, self, @backend) do |conn| add_connection(conn) end end |
#stop ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/received/server.rb', line 40 def stop return if stopping? logger.info "Stopping server" EventMachine.stop_server(@signature) @stopped_at = Time.now unless wait_for_connections_and_stop # Still some connections running, schedule a check later EventMachine.add_periodic_timer(1) { wait_for_connections_and_stop } end end |
#stopping? ⇒ Boolean
Checks whether the server is in stopping mode
52 53 54 |
# File 'lib/received/server.rb', line 52 def stopping? !!@stopped_at end |