Class: Ione::Rpc::Server
- Inherits:
-
Object
- Object
- Ione::Rpc::Server
- Defined in:
- lib/ione/rpc/server.rb
Overview
This is the base class of server peers.
To implement a server you need to create a subclass of this class and implement #handle_request. You can also optionally implement #handle_connection to do initialization when a new client connects, and #handle_error to handle errors that occur during the request handling.
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#handle_connection(connection) ⇒ nil
Override this method to do work when a new client connects.
-
#handle_error(error, request, connection) ⇒ Ione::Future<Object>
Override this method to handle errors raised or returned by #handle_request or any other part of the request handling (for example the response encoding).
-
#handle_request(message, connection) ⇒ Ione::Future<Object>
Override this method to handle requests.
-
#initialize(port, codec, options = {}) ⇒ Server
constructor
A new instance of Server.
-
#start ⇒ Ione::Future<Ione::Rpc::Server>
Start listening for client connections.
-
#stop ⇒ Ione::Future<Ione::Rpc::Server>
Stop the server and close all connections.
Constructor Details
#initialize(port, codec, options = {}) ⇒ Server
Returns a new instance of Server.
14 15 16 17 18 19 20 21 22 |
# File 'lib/ione/rpc/server.rb', line 14 def initialize(port, codec, ={}) @port = port @codec = codec @io_reactor = [:io_reactor] || Io::IoReactor.new @stop_reactor = ![:io_reactor] @queue_length = [:queue_size] || 5 @bind_address = [:bind_address] || '0.0.0.0' @logger = [:logger] end |
Instance Attribute Details
#port ⇒ Object (readonly)
Returns the value of attribute port.
12 13 14 |
# File 'lib/ione/rpc/server.rb', line 12 def port @port end |
Instance Method Details
#handle_connection(connection) ⇒ nil
Override this method to do work when a new client connects.
This method may be called concurrently.
50 51 |
# File 'lib/ione/rpc/server.rb', line 50 def handle_connection(connection) end |
#handle_error(error, request, connection) ⇒ Ione::Future<Object>
Override this method to handle errors raised or returned by #handle_request or any other part of the request handling (for example the response encoding).
When this method raises an error or returns a failed future there will be no response for the request. Unless you use a custom client this means that you lock up one of the connection's channels forever, and depending on the client implementation it may lock up resources. Make sure that you always return a future that will resolve to something that will be encodeable.
Should this method fail a message will be logged at the error
level
with the error message and class. The full backtrace will also be logged
at the debug
level.
70 71 72 |
# File 'lib/ione/rpc/server.rb', line 70 def handle_error(error, request, connection) Ione::Future.failed(error) end |
#handle_request(message, connection) ⇒ Ione::Future<Object>
Override this method to handle requests.
You must respond to all requests, otherwise the client will eventually use up all of its channels and not be able to send any more requests.
This method may be called concurrently.
When this method raises an error, or returns a failed future, #handle_error will be called with the error.
88 89 90 |
# File 'lib/ione/rpc/server.rb', line 88 def handle_request(, connection) Future.resolved end |
#start ⇒ Ione::Future<Ione::Rpc::Server>
Start listening for client connections. This also starts the IO reactor if it was not already started.
The returned future resolves when the server is ready to accept connections, or fails if there is an error starting the server.
32 33 34 |
# File 'lib/ione/rpc/server.rb', line 32 def start @io_reactor.start.flat_map { setup_server }.map(self) end |
#stop ⇒ Ione::Future<Ione::Rpc::Server>
Stop the server and close all connections. This also stops the IO reactor if it has not already stopped.
41 42 43 |
# File 'lib/ione/rpc/server.rb', line 41 def stop @io_reactor.stop.map(self) end |