Class: Bitcoin::Protocol::Addr
- Inherits:
-
Struct
- Object
- Struct
- Bitcoin::Protocol::Addr
- Defined in:
- lib/bitcoin/protocol/address.rb
Instance Attribute Summary collapse
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#port ⇒ Object
Returns the value of attribute port.
-
#service ⇒ Object
Returns the value of attribute service.
-
#time ⇒ Object
Returns the value of attribute time.
Class Method Summary collapse
Instance Method Summary collapse
-
#alive? ⇒ Boolean
is this address alive?.
-
#initialize(data = nil) ⇒ Addr
constructor
create addr from raw binary
data
. - #string ⇒ Object
- #to_payload ⇒ Object
Constructor Details
#initialize(data = nil) ⇒ Addr
create addr from raw binary data
18 19 20 21 22 23 24 25 26 |
# File 'lib/bitcoin/protocol/address.rb', line 18 def initialize(data = nil) if data self[:time], self[:service], self[:ip], self[:port] = data.unpack("VQx12a4n") self[:ip] = ip.unpack("C*").join(".") else self[:time], self[:service] = Time.now.to_i, 1 self[:ip], self[:port] = "127.0.0.1", Bitcoin.network[:default_port] end end |
Instance Attribute Details
#ip ⇒ Object
Returns the value of attribute ip
6 7 8 |
# File 'lib/bitcoin/protocol/address.rb', line 6 def ip @ip end |
#port ⇒ Object
Returns the value of attribute port
6 7 8 |
# File 'lib/bitcoin/protocol/address.rb', line 6 def port @port end |
#service ⇒ Object
Returns the value of attribute service
6 7 8 |
# File 'lib/bitcoin/protocol/address.rb', line 6 def service @service end |
#time ⇒ Object
Returns the value of attribute time
6 7 8 |
# File 'lib/bitcoin/protocol/address.rb', line 6 def time @time end |
Class Method Details
.pkt(*addrs) ⇒ Object
42 43 44 45 46 |
# File 'lib/bitcoin/protocol/address.rb', line 42 def self.pkt(*addrs) addrs = addrs.select{|i| i.is_a?(Bitcoin::Protocol::Addr) && i.ip =~ /^\d+\.\d+\.\d+\.\d+$/ } length = Bitcoin::Protocol.pack_var_int(addrs.size) Bitcoin::Protocol.pkt("addr", length + addrs.map(&:to_payload).join) end |
Instance Method Details
#alive? ⇒ Boolean
is this address alive?
29 30 31 |
# File 'lib/bitcoin/protocol/address.rb', line 29 def alive? (Time.now.tv_sec-7200) <= self[:time] end |
#string ⇒ Object
38 39 40 |
# File 'lib/bitcoin/protocol/address.rb', line 38 def string "#{self[:ip]}:#{self[:port]}" end |
#to_payload ⇒ Object
33 34 35 36 |
# File 'lib/bitcoin/protocol/address.rb', line 33 def to_payload ip = self[:ip].split(".").map(&:to_i) [ time, service, ("\x00"*10)+"\xff\xff", *ip, port ].pack("VQa12C4n") end |