Class: Mongo::Address

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mongo/address.rb,
lib/mongo/address/ipv6.rb,
lib/mongo/address/unix.rb,
lib/mongo/address/ipv4.rb

Overview

Represents an address to a server, either with an IP address or socket path.

Since:

  • 2.0.0

Defined Under Namespace

Classes: IPv4, IPv6, Unix

Constant Summary

FAMILY_MAP =

Mapping from socket family to resolver class.

Since:

  • 2.0.0

{
  ::Socket::PF_UNIX => Unix,
  ::Socket::AF_INET6 => IPv6,
  ::Socket::AF_INET => IPv4
}.freeze
LOCALHOST =

The localhost constant.

Since:

  • 2.1.0

'localhost'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed, options = {}) ⇒ Address

Initialize the address.

Examples:

Initialize the address with a DNS entry and port.

Mongo::Address.new("app.example.com:27017")

Initialize the address with a DNS entry and no port.

Mongo::Address.new("app.example.com")

Initialize the address with an IPV4 address and port.

Mongo::Address.new("127.0.0.1:27017")

Initialize the address with an IPV4 address and no port.

Mongo::Address.new("127.0.0.1")

Initialize the address with an IPV6 address and port.

Mongo::Address.new("[::1]:27017")

Initialize the address with an IPV6 address and no port.

Mongo::Address.new("[::1]")

Initialize the address with a unix socket.

Mongo::Address.new("/path/to/socket.sock")

Parameters:

  • seed (String)

    The provided address.

  • options (Hash) (defaults to: {})

    The address options.

Since:

  • 2.0.0



119
120
121
122
123
# File 'lib/mongo/address.rb', line 119

def initialize(seed, options = {})
  @seed = seed
  @host, @port = parse_host_port
  @options = options
end

Instance Attribute Details

#hostString (readonly)

Returns host The original host name.

Returns:

  • (String)

    host The original host name.

Since:

  • 2.0.0



46
47
48
# File 'lib/mongo/address.rb', line 46

def host
  @host
end

#portInteger (readonly)

Returns port The port.

Returns:

  • (Integer)

    port The port.

Since:

  • 2.0.0



49
50
51
# File 'lib/mongo/address.rb', line 49

def port
  @port
end

#seedString (readonly)

Returns seed The seed address.

Returns:

  • (String)

    seed The seed address.

Since:

  • 2.0.0



43
44
45
# File 'lib/mongo/address.rb', line 43

def seed
  @seed
end

Instance Method Details

#==(other) ⇒ true, false

Check equality of the address to another.

Examples:

Check address equality.

address == other

Parameters:

  • other (Object)

    The other object.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0



61
62
63
64
# File 'lib/mongo/address.rb', line 61

def ==(other)
  return false unless other.is_a?(Address)
  host == other.host && port == other.port
end

#connect_socket!(socket) ⇒ Object

Connect a socket.

Examples:

Connect a socket.

address.connect_socket!(socket)

Since:

  • 2.4.3



171
172
173
# File 'lib/mongo/address.rb', line 171

def connect_socket!(socket)
  socket.connect!(connect_timeout)
end

#eql?(other) ⇒ true, false

Check equality for hashing.

Examples:

Check hashing equality.

address.eql?(other)

Parameters:

  • other (Object)

    The other object.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.2.0



76
77
78
# File 'lib/mongo/address.rb', line 76

def eql?(other)
  self == other
end

#hashInteger

Calculate the hash value for the address.

Examples:

Calculate the hash value.

address.hash

Returns:

  • (Integer)

    The hash value.

Since:

  • 2.0.0



88
89
90
# File 'lib/mongo/address.rb', line 88

def hash
  [ host, port ].hash
end

#inspectString

Get a pretty printed address inspection.

Examples:

Get the address inspection.

address.inspect

Returns:

  • (String)

    The nice inspection string.

Since:

  • 2.0.0



133
134
135
# File 'lib/mongo/address.rb', line 133

def inspect
  "#<Mongo::Address:0x#{object_id} address=#{to_s}>"
end

#socket(socket_timeout, ssl_options = {}) ⇒ Pool::Socket::SSL, ...

Get a socket for the provided address, given the options.

Examples:

Get a socket.

address.socket(5, :ssl => true)

Parameters:

  • socket_timeout (Float)

    The socket timeout.

  • ssl_options (Hash) (defaults to: {})

    SSL options.

Returns:

  • (Pool::Socket::SSL, Pool::Socket::TCP, Pool::Socket::Unix)

    The socket.

Since:

  • 2.0.0



148
149
150
151
# File 'lib/mongo/address.rb', line 148

def socket(socket_timeout, ssl_options = {})
  @resolver ||= initialize_resolver!(ssl_options)
  @resolver.socket(socket_timeout, ssl_options)
end

#to_sString

Get the address as a string.

Examples:

Get the address as a string.

address.to_s

Returns:

  • (String)

    The nice string.

Since:

  • 2.0.0



161
162
163
# File 'lib/mongo/address.rb', line 161

def to_s
  port ? "#{host}:#{port}" : host
end