Class: Bones::RPC::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/bones/rpc/address.rb

Overview

Encapsulates behaviour around addresses and resolving dns.

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, port = nil) ⇒ Address

Instantiate the new address.

Examples:

Instantiate the address.

Bones::RPC::Address.new("localhost:27017")

Parameters:

  • address (String)

    The host:port pair as a string.

Since:

  • 2.0.0



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bones/rpc/address.rb', line 30

def initialize(address, port = nil)
  if address.is_a?(Bones::RPC::Address)
    @host     = address.host
    @ip       = address.ip
    @original = address.original
    @port     = address.port
    @path     = address.path
    return
  end

  if port.nil?
    @original = address
  else
    @original = "#{address}:#{port}"
  end
end

Instance Attribute Details

#hostString

Returns The host name.

Returns:

  • (String)

    The host name.



20
21
22
# File 'lib/bones/rpc/address.rb', line 20

def host
  @host
end

#ipString

Returns The ip address.

Returns:

  • (String)

    The ip address.



20
# File 'lib/bones/rpc/address.rb', line 20

attr_reader :host, :ip, :original, :path, :port

#originalString

Returns The original host name.

Returns:

  • (String)

    The original host name.



20
# File 'lib/bones/rpc/address.rb', line 20

attr_reader :host, :ip, :original, :path, :port

#pathObject (readonly)

Since:

  • 2.0.0



20
21
22
# File 'lib/bones/rpc/address.rb', line 20

def path
  @path
end

#portObject

Since:

  • 2.0.0



20
# File 'lib/bones/rpc/address.rb', line 20

attr_reader :host, :ip, :original, :path, :port

#resolvedString Also known as: to_s

Returns The full resolved address.

Returns:

  • (String)

    The full resolved address.

Since:

  • 2.0.0



20
# File 'lib/bones/rpc/address.rb', line 20

attr_reader :host, :ip, :original, :path, :port

Instance Method Details

#inspectObject

Since:

  • 2.0.0



47
48
49
# File 'lib/bones/rpc/address.rb', line 47

def inspect
  "<#{self.class} \"#{to_s}\">"
end

#ipv4?Boolean

Returns:

  • (Boolean)

Since:

  • 2.0.0



51
52
53
# File 'lib/bones/rpc/address.rb', line 51

def ipv4?
  ip.is_a?(Resolv::IPv4)
end

#ipv6?Boolean

Returns:

  • (Boolean)

Since:

  • 2.0.0



55
56
57
# File 'lib/bones/rpc/address.rb', line 55

def ipv6?
  ip.is_a?(Resolv::IPv6)
end

#resolve(node) ⇒ String

Resolve the address for the provided node. If the address cannot be resolved the node will be flagged as down.

Examples:

Resolve the address.

address.resolve(node)

Parameters:

  • node (Node)

    The node to resolve for.

Returns:

  • (String)

    The resolved address.

Since:

  • 2.0.0



101
102
103
104
105
106
107
108
109
# File 'lib/bones/rpc/address.rb', line 101

def resolve(node)
  begin
    resolve! unless valid?
    valid?
  rescue Resolv::ResolvError, SocketError => e
    node.instrument(Node::WARN, prefix: "  BONES-RPC:", message: "Could not resolve IP or UNIX path for: #{original}")
    node.down and false
  end
end

#resolve!Object

Since:

  • 2.0.0



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/bones/rpc/address.rb', line 111

def resolve!
  address = @original

  host = nil
  path = nil

  if address.is_a?(String) && port.nil?
    if !!(address =~ /\Aunix:/) # UNIX
      path = address.gsub(/\Aunix:/, '')
    elsif !!(address =~ /\A\[.+\]\:\d+\z/) # IPv6
      host, port = address.split(']:')
      host.gsub!(/\A\[/, '')
    else # IPv4 (hopefully)
      host, port = address.split(':')
    end
  end

  if path
    # Ensure path is valid
    @path = ::Socket.unpack_sockaddr_un(::Socket.pack_sockaddr_un(path))
    return
  elsif port.nil?
    raise ArgumentError, "wrong number of arguments (1 for 2)"
  else
    @host = host || address
    @port = port.to_i
  end

  # Is it an IPv4 address?
  if !!(Resolv::IPv4::Regex =~ @host)
    @ip = Resolv::IPv4.create(@host)
  end

  # Guess it's not IPv4! Is it IPv6?
  unless @ip
    if !!(Resolv::IPv6::Regex =~ @host)
      @original = "[#{@host}]:#{@port}"
      @ip = Resolv::IPv6.create(@host)
    end
  end

  # Guess it's not an IP address, so let's try DNS
  unless @ip
    addrs = Array(::Celluloid::IO::DNSResolver.new.resolve(@host))
    raise Resolv::ResolvError, "DNS result has no information for #{@host}" if addrs.empty?

    # Pseudorandom round-robin DNS support :/
    @ip = addrs[rand(addrs.size)]
  end

  if !valid_ip?
    raise ArgumentError, "unsupported address class: #{@ip.class}"
  end
end

#unix?Boolean

Returns:

  • (Boolean)

Since:

  • 2.0.0



73
74
75
# File 'lib/bones/rpc/address.rb', line 73

def unix?
  path.is_a?(String)
end

#valid?Boolean Also known as: connectable?

Returns:

  • (Boolean)

Since:

  • 2.0.0



77
78
79
# File 'lib/bones/rpc/address.rb', line 77

def valid?
  unix? or (valid_ip? and valid_port?)
end

#valid_ip?Boolean

Returns:

  • (Boolean)

Since:

  • 2.0.0



82
83
84
# File 'lib/bones/rpc/address.rb', line 82

def valid_ip?
  ipv4? or ipv6?
end

#valid_port?Boolean

Returns:

  • (Boolean)

Since:

  • 2.0.0



86
87
88
# File 'lib/bones/rpc/address.rb', line 86

def valid_port?
  !port.nil? && port != 0
end