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

on_abort[RW]

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.

on_close[RW]

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.

on_data_received[RW]

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(format: :json, **params) click to toggle source

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
new() click to toggle source
# File lib/farcall/transport.rb, line 72
def initialize
  @in_buffer = []
end

Public Instance Methods

close() click to toggle source

Flush and close transport

# File lib/farcall/transport.rb, line 88
def close
  @closed = true
  @on_close and @on_close.call
end
closed?() click to toggle source
# File lib/farcall/transport.rb, line 93
def closed?
  @closed
end
on_data_received=(proc) click to toggle source

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
push_input(data) click to toggle source

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
receive_data(&block) click to toggle source

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
send_data(hash) click to toggle source

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

connection_aborted(exceptoin) click to toggle source

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
connection_closed() click to toggle source

Call it when your connection is closed

# File lib/farcall/transport.rb, line 121
def connection_closed
  close
end
drain() click to toggle source
# 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