Class: Ione::Rpc::Server

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options={})
  @port = port
  @codec = codec
  @io_reactor = options[:io_reactor] || Io::IoReactor.new
  @stop_reactor = !options[:io_reactor]
  @queue_length = options[:queue_size] || 5
  @bind_address = options[:bind_address] || '0.0.0.0'
  @logger = options[:logger]
end

Instance Attribute Details

#portObject (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.

Returns:

  • (nil)

    the return value of this method is ignored



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.

Returns:

  • (Ione::Future<Object>)

    a future that will resolve to an alternate response for this request.



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.

Parameters:

  • message (Object)

    a (decoded) message from a client

  • connection (#host, #port, #on_closed)

    the client connection that received the message

Returns:

  • (Ione::Future<Object>)

    a future that will resolve to the response.



88
89
90
# File 'lib/ione/rpc/server.rb', line 88

def handle_request(message, connection)
  Future.resolved
end

#startIone::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.

Returns:

  • (Ione::Future<Ione::Rpc::Server>)

    a future that resolves to the server when all hosts have been connected to.



32
33
34
# File 'lib/ione/rpc/server.rb', line 32

def start
  @io_reactor.start.flat_map { setup_server }.map(self)
end

#stopIone::Future<Ione::Rpc::Server>

Stop the server and close all connections. This also stops the IO reactor if it has not already stopped.

Returns:

  • (Ione::Future<Ione::Rpc::Server>)

    a future that resolves to the server when all connections have closed and the IO reactor has stopped.



41
42
43
# File 'lib/ione/rpc/server.rb', line 41

def stop
  @io_reactor.stop.map(self)
end