Class: Farcall::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/farcall/transport.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransport

Returns a new instance of Transport.



72
73
74
# File 'lib/farcall/transport.rb', line 72

def initialize
  @in_buffer = []
end

Instance Attribute Details

#on_abortObject

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.



70
71
72
# File 'lib/farcall/transport.rb', line 70

def on_abort
  @on_abort
end

#on_closeObject

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.



70
71
72
# File 'lib/farcall/transport.rb', line 70

def on_close
  @on_close
end

#on_data_receivedObject

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.



70
71
72
# File 'lib/farcall/transport.rb', line 70

def on_data_received
  @on_data_received
end

Class Method Details

.create(format: :json, **params) ⇒ Object

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


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

Instance Method Details

#closeObject

Flush and close transport



88
89
90
91
# File 'lib/farcall/transport.rb', line 88

def close
  @closed = true
  @on_close and @on_close.call
end

#closed?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/farcall/transport.rb', line 93

def closed?
  @closed
end

#push_input(data) ⇒ Object

Input buffering: transport may start before configure endpoint delegates observer, so the transport can simply push it here and rely on default buffering.



105
106
107
108
# File 'lib/farcall/transport.rb', line 105

def push_input data
  @in_buffer << data
  drain
end

#receive_data(&block) ⇒ Object

Utility function. Calls the provided block on data reception. Resets the block with #on_data_received



78
79
80
# File 'lib/farcall/transport.rb', line 78

def receive_data &block
  self.on_data_received = block
end

#send_data(hash) ⇒ Object

Transmit somehow a dictionary to the remote part



83
84
85
# File 'lib/farcall/transport.rb', line 83

def send_data hash
  raise 'not implemented'
end