class Farcall::Transport
The transport interface. Farcall
works via anything that can send and receive dictionary objects. The transport should only implement Transport#send_data
and invoke Transport#on_data_received
when incoming data are available
Attributes
Tansport must call this process on each incoming hash passing it as the only parameter, e.g. self.on_data_received.call(hash) Common trick is to start inner event loop on on_data_recieved=, don't forget to call super first.
Tansport must call this process on each incoming hash passing it as the only parameter, e.g. self.on_data_received.call(hash) Common trick is to start inner event loop on on_data_recieved=, don't forget to call super first.
Tansport must call this process on each incoming hash passing it as the only parameter, e.g. self.on_data_received.call(hash) Common trick is to start inner event loop on on_data_recieved=, don't forget to call super first.
Public Class Methods
Create transport with a given format and parameters.
format right now can be only :json
creation parameters can be:
- socket: connect transport to some socket (should be connected) - input and output: two stream-like objects which support read(length) and write(data) parameters
# File lib/farcall/transport.rb, line 51 def self.create format: :json, **params case format when :json Farcall::JsonTransport.new **params when :boss if defined?(Farcall::BossTransport) Farcall::BossTransport.new **params else raise Farcall::Error.new("add gem 'boss-protocol' to use boss transport") end else raise Farcall::Error, "unknown format: #{format}" end end
# File lib/farcall/transport.rb, line 72 def initialize @in_buffer = [] end
Public Instance Methods
Flush and close transport
# File lib/farcall/transport.rb, line 88 def close @closed = true @on_close and @on_close.call end
# File lib/farcall/transport.rb, line 93 def closed? @closed end
set handler and drain all input packets that may be buffered by the time.
# File lib/farcall/transport.rb, line 98 def on_data_received= proc @on_data_received = proc drain end
Input buffering: transport may start before configure endpoint delegates observer, so the transport can simply push it here and rely on default buffering.
# File lib/farcall/transport.rb, line 105 def push_input data @in_buffer << data drain end
Utility function. Calls the provided block on data reception. Resets the block with on_data_received
# File lib/farcall/transport.rb, line 78 def receive_data &block self.on_data_received = block end
Transmit somehow a dictionary to the remote part
# File lib/farcall/transport.rb, line 83 def send_data hash raise 'not implemented' end
Protected Instance Methods
Call it when the connection is aborted due to an exception
# File lib/farcall/transport.rb, line 126 def connection_aborted exceptoin STDERR.puts "Farcall: connection aborted: #{$!.class.name}: #{$!}" @on_abort and @on_abort.call $! close end
Call it when your connection is closed
# File lib/farcall/transport.rb, line 121 def connection_closed close end
# File lib/farcall/transport.rb, line 112 def drain if @in_buffer.size > 0 && on_data_received @in_buffer.each { |x| on_data_received.call(x) } @in_buffer.clear end end