Class: DEVp2p::Protocol

Inherits:
Object
  • Object
show all
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_packet plus create_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

P2PProtocol

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configurable

add_config

Constructor Details

#initialize(peer, service) ⇒ Protocol

Returns a new instance of Protocol.

Raises:

  • (ArgumentError)


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_idObject (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

#peerObject (readonly)

Returns the value of attribute peer.



34
35
36
# File 'lib/devp2p/protocol.rb', line 34

def peer
  @peer
end

#serviceObject (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

#startObject



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

#stopObject



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

Returns:

  • (Boolean)


66
67
68
# File 'lib/devp2p/protocol.rb', line 66

def stopped?
  @stopped
end

#to_sObject Also known as: inspect



83
84
85
# File 'lib/devp2p/protocol.rb', line 83

def to_s
  "<#{name} #{peer}>"
end