Class: Arpie::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/arpie/server.rb

Overview

A Server is the server-side part of a RPC setup. It accepts connections (via the acceptor), and handles incoming RPC calls on them.

There will be one Thread per connection, so order of execution with multiple threads is not guaranteed.

Direct Known Subclasses

ProxyServer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*protocols) ⇒ Server

Create a new Server with the given protocols. You will need to define a handler, and an acceptor before it becomes operational.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/arpie/server.rb', line 42

def initialize *protocols
  @protocol = Arpie::ProtocolChain.new(*protocols)
  @endpoints = []

  @on_connect = lambda {|server, endpoint| }
  @on_disconnect = lambda {|server, endpoint, exception| }
  @on_handler_error = lambda {|server, endpoint, message, exception|
    $stderr.puts "Error in handler: #{exception.message.to_s}"
    $stderr.puts exception.backtrace.join("\n")
    $stderr.puts "Returning exception for this call."
    Exception.new("internal error")
  }
  @handler = lambda {|server, endpoint, message| raise ArgumentError, "No handler defined." }
end

Instance Attribute Details

#endpointsObject (readonly)

Returns the value of attribute endpoints.



37
38
39
# File 'lib/arpie/server.rb', line 37

def endpoints
  @endpoints
end

#protocolObject (readonly)

Returns the value of attribute protocol.



35
36
37
# File 'lib/arpie/server.rb', line 35

def protocol
  @protocol
end

Instance Method Details

#accept(&acceptor) ⇒ Object

Provide an acceptor; this will be run in a a loop to get IO objects.

Example:

listener = TCPServer.new(12345)
my_server.accept do
  listener.accept
end


65
66
67
68
69
# File 'lib/arpie/server.rb', line 65

def accept &acceptor #:yields: server
  @acceptor = acceptor
  Thread.new { _acceptor_thread }
  self
end

#handle(&handler) ⇒ Object

Set a message handler, which is a proc that will receive three parameters: the server, the endpoint, and the message.

Example:

my_server.handle do |server, endpoint, message|
  puts "Got a message: #{message.inspect}"
  endpoint.write_message "ok"
end

Raises:

  • (ArgumentError)


79
80
81
82
83
# File 'lib/arpie/server.rb', line 79

def handle &handler #:yields: server, endpoint, message
  raise ArgumentError, "No handler given; need a block or proc." unless handler
  @handler = handler
  self
end

#on_connect(&handler) ⇒ Object

Callback that gets invoked when a new client connects. You can throw :kill_client here to stop this client from connecting. Clients stopped this way will invoke the on_disconnect handler normally.

Raises:

  • (ArgumentError)


100
101
102
103
104
# File 'lib/arpie/server.rb', line 100

def on_connect &handler #:yields: server, endpoint
  raise ArgumentError, "No handler given; need a block or proc." unless handler
  @on_connect = handler
  self
end

#on_disconnect(&handler) ⇒ Object

Callback that gets invoked when a client disconnects. The exception is the error that occured (usually a EOFError).

Raises:

  • (ArgumentError)


108
109
110
111
112
# File 'lib/arpie/server.rb', line 108

def on_disconnect &handler #:yields: server, endpoint, exception
  raise ArgumentError, "No handler given; need a block or proc." unless handler
  @on_disconnect = handler
  self
end

#on_handler_error(&handler) ⇒ Object

Set an error handler. The return value will be sent to the client.

Default is to print the exception to stderr, and return a generic exception that does not leak information.

Raises:

  • (ArgumentError)


90
91
92
93
94
# File 'lib/arpie/server.rb', line 90

def on_handler_error &handler #:yields: server, endpoint, message, exception
  raise ArgumentError, "No handler given; need a block or proc." unless handler
  @on_handler_error = handler
  self
end