Class: Farcall::Transport
- Inherits:
-
Object
- Object
- Farcall::Transport
- 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
Direct Known Subclasses
BossTransport, JsonTransport, LocalConnection::Connection, WebsocketJsonClientTransport
Instance Attribute Summary collapse
-
#on_abort ⇒ Object
Tansport must call this process on each incoming hash passing it as the only parameter, e.g.
-
#on_close ⇒ Object
Tansport must call this process on each incoming hash passing it as the only parameter, e.g.
-
#on_data_received ⇒ Object
Tansport must call this process on each incoming hash passing it as the only parameter, e.g.
Class Method Summary collapse
-
.create(format: :json, **params) ⇒ Object
Create transport with a given format and parameters.
Instance Method Summary collapse
-
#close ⇒ Object
Flush and close transport.
- #closed? ⇒ Boolean
-
#receive_data(&block) ⇒ Object
Utility function.
-
#send_data(hash) ⇒ Object
Transmit somehow a dictionary to the remote part.
Instance Attribute Details
#on_abort ⇒ Object
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.
52 53 54 |
# File 'lib/farcall/transport.rb', line 52 def on_abort @on_abort end |
#on_close ⇒ Object
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.
52 53 54 |
# File 'lib/farcall/transport.rb', line 52 def on_close @on_close end |
#on_data_received ⇒ Object
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.
52 53 54 |
# File 'lib/farcall/transport.rb', line 52 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 aoutput: two stream-like objects which support read(length) and write(data)
parameters
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/farcall/transport.rb', line 33 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
#close ⇒ Object
Flush and close transport
66 67 68 69 |
# File 'lib/farcall/transport.rb', line 66 def close @closed = true @on_close and @on_close.call end |
#closed? ⇒ Boolean
71 72 73 |
# File 'lib/farcall/transport.rb', line 71 def closed? @closed end |
#receive_data(&block) ⇒ Object
Utility function. Calls the provided block on data reception. Resets the block with #on_data_received
56 57 58 |
# File 'lib/farcall/transport.rb', line 56 def receive_data &block self.on_data_received = block end |
#send_data(hash) ⇒ Object
Transmit somehow a dictionary to the remote part
61 62 63 |
# File 'lib/farcall/transport.rb', line 61 def send_data hash raise 'not implemented' end |