Class: DEVp2p::Discovery::Address
- Inherits:
-
Object
- Object
- DEVp2p::Discovery::Address
- Defined in:
- lib/devp2p/discovery/address.rb
Instance Attribute Summary collapse
-
#udp_port ⇒ Object
readonly
Returns the value of attribute udp_port.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
addresses equal if they share ip and udp_port.
-
#initialize(ip, udp_port, tcp_port = nil, from_binary = false) ⇒ Address
constructor
A new instance of Address.
- #ip ⇒ Object
- #to_b ⇒ Object (also: #to_endpoint)
- #to_h ⇒ Object
- #to_s ⇒ Object
- #update(addr) ⇒ Object
Constructor Details
#initialize(ip, udp_port, tcp_port = nil, from_binary = false) ⇒ Address
Returns a new instance of Address.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/devp2p/discovery/address.rb', line 17 def initialize(ip, udp_port, tcp_port=nil, from_binary=false) tcp_port ||= udp_port if from_binary raise ArgumentError, "invalid ip" unless [4,16].include?(ip.size) @udp_port = dec_port udp_port @tcp_port = dec_port tcp_port else raise ArgumentError, "udp_port must be Integer" unless udp_port.is_a?(Integer) raise ArgumentError, "tcp_port must be Integer" unless tcp_port.is_a?(Integer) @udp_port = udp_port @tcp_port = tcp_port end begin @ip = from_binary ? IPAddr.new_ntoh(ip) : IPAddr.new(ip) rescue IPAddr::InvalidAddressError => e ips = Resolv.getaddresses(ip).sort {|addr| addr =~ /:/ ? 1 : 0 } # use ipv4 first raise e if ips.empty? @ip = ips[0] end end |
Instance Attribute Details
#udp_port ⇒ Object (readonly)
Returns the value of attribute udp_port.
11 12 13 |
# File 'lib/devp2p/discovery/address.rb', line 11 def udp_port @udp_port end |
Class Method Details
.from_endpoint(ip, udp_port, tcp_port = "\x00\x00") ⇒ Object
13 14 15 |
# File 'lib/devp2p/discovery/address.rb', line 13 def self.from_endpoint(ip, udp_port, tcp_port="\x00\x00") new(ip, udp_port, tcp_port, true) end |
Instance Method Details
#==(other) ⇒ Object
addresses equal if they share ip and udp_port
53 54 55 |
# File 'lib/devp2p/discovery/address.rb', line 53 def ==(other) [ip, udp_port] == [other.ip, other.udp_port] end |
#ip ⇒ Object
42 43 44 |
# File 'lib/devp2p/discovery/address.rb', line 42 def ip @ip.to_s end |
#to_b ⇒ Object Also known as: to_endpoint
65 66 67 |
# File 'lib/devp2p/discovery/address.rb', line 65 def to_b [@ip.hton, enc_port(udp_port), enc_port(tcp_port)] end |
#to_h ⇒ Object
61 62 63 |
# File 'lib/devp2p/discovery/address.rb', line 61 def to_h {ip: ip, udp_port: udp_port, tcp_port: tcp_port} end |
#to_s ⇒ Object
57 58 59 |
# File 'lib/devp2p/discovery/address.rb', line 57 def to_s "Address(#{ip}:#{udp_port})" end |
#update(addr) ⇒ Object
46 47 48 |
# File 'lib/devp2p/discovery/address.rb', line 46 def update(addr) @tcp_port = addr.tcp_port if @tcp_port.nil? || @tcp_port == 0 end |