Class: Bitcoin::Protocol::Addr

Inherits:
Struct
  • Object
show all
Defined in:
lib/bitcoin/protocol/address.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#ipObject

Returns the value of attribute ip

Returns:

  • (Object)

    the current value of ip



6
7
8
# File 'lib/bitcoin/protocol/address.rb', line 6

def ip
  @ip
end

#portObject

Returns the value of attribute port

Returns:

  • (Object)

    the current value of port



6
7
8
# File 'lib/bitcoin/protocol/address.rb', line 6

def port
  @port
end

#serviceObject

Returns the value of attribute service

Returns:

  • (Object)

    the current value of service



6
7
8
# File 'lib/bitcoin/protocol/address.rb', line 6

def service
  @service
end

#timeObject

Returns the value of attribute time

Returns:

  • (Object)

    the current value of 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?

Returns:

  • (Boolean)


29
30
31
# File 'lib/bitcoin/protocol/address.rb', line 29

def alive?
  (Time.now.tv_sec-7200) <= self[:time]
end

#stringObject



38
39
40
# File 'lib/bitcoin/protocol/address.rb', line 38

def string
  "#{self[:ip]}:#{self[:port]}"
end

#to_payloadObject



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