Class: Ciri::P2P::Peer

Inherits:
Object
  • Object
show all
Defined in:
lib/ciri/p2p/peer.rb

Overview

represent a connected remote node

Constant Summary collapse

OUTGOING =
:outgoing
INCOMING =
:incoming

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, handshake, protocols, direction:) ⇒ Peer

Returns a new instance of Peer.



40
41
42
43
44
45
46
# File 'lib/ciri/p2p/peer.rb', line 40

def initialize(connection, handshake, protocols, direction:)
  @connection = connection
  @handshake = handshake
  @protocols = protocols
  @protocol_io_hash = make_protocol_io_hash(protocols, handshake.caps, connection)
  @direction = direction
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



38
39
40
# File 'lib/ciri/p2p/peer.rb', line 38

def connection
  @connection
end

#directionObject (readonly)

Returns the value of attribute direction.



38
39
40
# File 'lib/ciri/p2p/peer.rb', line 38

def direction
  @direction
end

Instance Method Details

#==(peer) ⇒ Object Also known as: eql?



70
71
72
# File 'lib/ciri/p2p/peer.rb', line 70

def ==(peer)
  self.class == peer.class && raw_node_id == peer.raw_node_id
end

#disconnectObject

disconnect peer connections



87
88
89
# File 'lib/ciri/p2p/peer.rb', line 87

def disconnect
  @connection.close
end

#disconnected?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/ciri/p2p/peer.rb', line 91

def disconnected?
  @connection.closed?
end

#find_protocol(name) ⇒ Object



99
100
101
102
103
# File 'lib/ciri/p2p/peer.rb', line 99

def find_protocol(name)
  @protocol.find do |protocol|
    protocol.name == name
  end
end

#find_protocol_io(name) ⇒ Object



105
106
107
108
109
# File 'lib/ciri/p2p/peer.rb', line 105

def find_protocol_io(name)
  protocol_ios.find do |protocol_io|
    protocol_io.protocol.name == name
  end
end

#find_protocol_io_by_msg_code(raw_code) ⇒ Object

find ProtocolIO by raw message code used by DEVP2P to find stream of sub-protocol



113
114
115
116
117
118
119
# File 'lib/ciri/p2p/peer.rb', line 113

def find_protocol_io_by_msg_code(raw_code)
  @protocol_io_hash.values.find do |protocol_io|
    offset = protocol_io.offset
    protocol = protocol_io.protocol
    raw_code >= offset && raw_code < offset + protocol.length
  end
end

#hashObject



66
67
68
# File 'lib/ciri/p2p/peer.rb', line 66

def hash
  raw_node_id.hash
end

#incoming?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/ciri/p2p/peer.rb', line 52

def incoming?
  @direction == INCOMING
end

#inspectObject



62
63
64
# File 'lib/ciri/p2p/peer.rb', line 62

def inspect
  "<Peer:#{to_s} direction: #{@direction}>"
end

#node_idObject

get NodeID object



82
83
84
# File 'lib/ciri/p2p/peer.rb', line 82

def node_id
  @node_id ||= NodeID.from_raw_id(@handshake.id)
end

#outgoing?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ciri/p2p/peer.rb', line 48

def outgoing?
  @direction == OUTGOING
end

#protocol_iosObject



95
96
97
# File 'lib/ciri/p2p/peer.rb', line 95

def protocol_ios
  @protocol_io_hash.values
end

#raw_node_idObject

get id of node in bytes form



77
78
79
# File 'lib/ciri/p2p/peer.rb', line 77

def raw_node_id
  node_id.to_bytes
end

#to_sObject



56
57
58
59
60
# File 'lib/ciri/p2p/peer.rb', line 56

def to_s
  @display_name ||= begin
    Utils.to_hex(node_id.id)[0..8]
  end
end