Class: Arpie::Server
- Inherits:
-
Object
- Object
- Arpie::Server
- 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
Instance Attribute Summary collapse
-
#endpoints ⇒ Object
readonly
Returns the value of attribute endpoints.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
Instance Method Summary collapse
-
#accept(&acceptor) ⇒ Object
Provide an acceptor; this will be run in a a loop to get IO objects.
-
#handle(&handler) ⇒ Object
Set a message handler, which is a proc that will receive three parameters: the server, the endpoint, and the message.
-
#initialize(*protocols) ⇒ Server
constructor
Create a new Server with the given protocols.
-
#on_connect(&handler) ⇒ Object
Callback that gets invoked when a new client connects.
-
#on_disconnect(&handler) ⇒ Object
Callback that gets invoked when a client disconnects.
-
#on_handler_error(&handler) ⇒ Object
Set an error handler.
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, , exception| $stderr.puts "Error in handler: #{exception..to_s}" $stderr.puts exception.backtrace.join("\n") $stderr.puts "Returning exception for this call." Exception.new("internal error") } @handler = lambda {|server, endpoint, | raise ArgumentError, "No handler defined." } end |
Instance Attribute Details
#endpoints ⇒ Object (readonly)
Returns the value of attribute endpoints.
37 38 39 |
# File 'lib/arpie/server.rb', line 37 def endpoints @endpoints end |
#protocol ⇒ Object (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, |
puts "Got a message: #{.inspect}"
endpoint. "ok"
end
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.
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).
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.
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 |