Class: DEVp2p::Protocol
- Inherits:
-
Object
- Object
- DEVp2p::Protocol
- Extended by:
- Configurable
- Includes:
- Concurrent::Async
- Defined in:
- lib/devp2p/protocol.rb
Overview
A protocol mediates between the network and the service. It implements a collection of commands.
For each command X the following methods are created at initialization:
-
‘packet = protocol.create_X(*args, **kwargs)`
-
‘protocol.send_X(*args, **kwargs)`, which is a shortcut for
send_packetpluscreate_X. -
‘protocol.receive_X(data)`
On protocol.receive_packet, the packet is deserialized according to the command.structure and the command.receive method called with a hash containing the received data.
The default implementation of command.receive calls callbacks which can be registered in a list which is available as: protocol.receive_X_callbacks.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cmd_by_id ⇒ Object
readonly
Returns the value of attribute cmd_by_id.
-
#peer ⇒ Object
readonly
Returns the value of attribute peer.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
-
#initialize(peer, service) ⇒ Protocol
constructor
A new instance of Protocol.
- #receive_packet(packet) ⇒ Object
- #send_packet(packet) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #stopped? ⇒ Boolean
- #to_s ⇒ Object (also: #inspect)
Methods included from Configurable
Constructor Details
#initialize(peer, service) ⇒ Protocol
Returns a new instance of Protocol.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/devp2p/protocol.rb', line 36 def initialize(peer, service) raise ArgumentError, 'service must be WiredService' unless service.is_a?(WiredService) raise ArgumentError, 'peer.send_packet must be callable' unless peer.respond_to?(:send_packet) @peer = peer @service = service @stopped = false setup end |
Instance Attribute Details
#cmd_by_id ⇒ Object (readonly)
Returns the value of attribute cmd_by_id.
34 35 36 |
# File 'lib/devp2p/protocol.rb', line 34 def cmd_by_id @cmd_by_id end |
#peer ⇒ Object (readonly)
Returns the value of attribute peer.
34 35 36 |
# File 'lib/devp2p/protocol.rb', line 34 def peer @peer end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
34 35 36 |
# File 'lib/devp2p/protocol.rb', line 34 def service @service end |
Instance Method Details
#receive_packet(packet) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/devp2p/protocol.rb', line 70 def receive_packet(packet) cmd_name = @cmd_by_id[packet.cmd_id] cmd = "receive_#{cmd_name}" send cmd, packet rescue ProtocolError => e logger.warn "protocol exception, stopping", error: e stop end |
#send_packet(packet) ⇒ Object
79 80 81 |
# File 'lib/devp2p/protocol.rb', line 79 def send_packet(packet) peer.async.send_packet packet end |
#start ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/devp2p/protocol.rb', line 48 def start logger.debug 'starting', proto: self service.async.on_wire_protocol_start self rescue puts $! puts $!.backtrace[0,10].join("\n") end |
#stop ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/devp2p/protocol.rb', line 56 def stop logger.debug 'stopping', proto: self service.async.on_wire_protocol_stop self @stopped = true rescue puts $! puts $!.backtrace[0,10].join("\n") end |
#stopped? ⇒ Boolean
66 67 68 |
# File 'lib/devp2p/protocol.rb', line 66 def stopped? @stopped end |
#to_s ⇒ Object Also known as: inspect
83 84 85 |
# File 'lib/devp2p/protocol.rb', line 83 def to_s "<#{name} #{peer}>" end |