Class: DEVp2p::Command
- Inherits:
-
Object
- Object
- DEVp2p::Command
- Extended by:
- Configurable
- Defined in:
- lib/devp2p/command.rb
Direct Known Subclasses
P2PProtocol::Disconnect, P2PProtocol::Hello, P2PProtocol::Ping, P2PProtocol::Pong
Instance Attribute Summary collapse
-
#receive_callbacks ⇒ Object
readonly
Returns the value of attribute receive_callbacks.
Class Method Summary collapse
Instance Method Summary collapse
-
#create(proto, *args) ⇒ Object
optionally implement create.
-
#initialize ⇒ Command
constructor
A new instance of Command.
-
#receive(proto, data) ⇒ Object
optionally implement receive.
Methods included from Configurable
Constructor Details
#initialize ⇒ Command
Returns a new instance of Command.
59 60 61 62 |
# File 'lib/devp2p/command.rb', line 59 def initialize raise InvalidCommandStructure unless [Hash, RLP::Sedes::CountableList].any? {|c| structure.is_a?(c) } @receive_callbacks = [] end |
Instance Attribute Details
#receive_callbacks ⇒ Object (readonly)
Returns the value of attribute receive_callbacks.
57 58 59 |
# File 'lib/devp2p/command.rb', line 57 def receive_callbacks @receive_callbacks end |
Class Method Details
.decode_payload(rlp_data) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/devp2p/command.rb', line 32 def decode_payload(rlp_data) case structure when RLP::Sedes::CountableList decoder = structure when Hash decoder = RLP::Sedes::List.new(elements: sedes, strict: decode_strict) else raise InvalidCommandStructure end data = RLP.decode rlp_data, sedes: decoder data = structure.keys.zip(data).to_h if structure.is_a?(Hash) data rescue puts "error in decode: #{$!}" puts "rlp:" puts RLP.decode(rlp_data) raise $! end |
.encode_payload(data) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/devp2p/command.rb', line 15 def encode_payload(data) if data.is_a?(Hash) raise ArgumentError, 'structure must be hash of arg names and sedes' unless structure.instance_of?(Hash) data = structure.keys.map {|k| data[k] } end case structure when RLP::Sedes::CountableList RLP.encode data, sedes: structure when Hash raise ArgumentError, 'structure and data length mismatch' unless data.size == structure.size RLP.encode data, sedes: RLP::Sedes::List.new(elements: sedes) else raise InvalidCommandStructure end end |
.sedes ⇒ Object
52 53 54 |
# File 'lib/devp2p/command.rb', line 52 def sedes @sedes ||= structure.values end |
Instance Method Details
#create(proto, *args) ⇒ Object
optionally implement create
65 66 67 68 69 70 |
# File 'lib/devp2p/command.rb', line 65 def create(proto, *args) = args.last.is_a?(Hash) ? args.pop : {} raise ArgumentError, "proto #{proto} must be protocol" unless proto.is_a?(Protocol) raise ArgumentError, "command structure mismatch" if !.empty? && structure.instance_of?(RLP::Sedes::CountableList) .empty? ? args : end |
#receive(proto, data) ⇒ Object
optionally implement receive
73 74 75 76 77 78 79 |
# File 'lib/devp2p/command.rb', line 73 def receive(proto, data) if structure.instance_of?(RLP::Sedes::CountableList) receive_callbacks.each {|cb| cb.call(proto, data) } else receive_callbacks.each {|cb| cb.call(proto, **data) } end end |