Class: Mongo::Address
- Inherits:
-
Object
- Object
- Mongo::Address
- Extended by:
- Forwardable
- Defined in:
- lib/mongo/address.rb,
lib/mongo/address/ipv4.rb,
lib/mongo/address/ipv6.rb,
lib/mongo/address/unix.rb
Overview
Represents an address to a server, either with an IP address or socket path.
Defined Under Namespace
Constant Summary collapse
- FAMILY_MAP =
Mapping from socket family to resolver class.
{ ::Socket::PF_UNIX => Unix, ::Socket::AF_INET6 => IPv6, ::Socket::AF_INET => IPv4 }.freeze
- LOCALHOST =
The localhost constant.
'localhost'.freeze
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
Host The original host name.
-
#port ⇒ Integer
readonly
Port The port.
-
#seed ⇒ String
readonly
Seed The seed address.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Check equality of the address to another.
-
#connect_socket!(socket) ⇒ Object
Connect a socket.
-
#eql?(other) ⇒ true, false
Check equality for hashing.
-
#hash ⇒ Integer
Calculate the hash value for the address.
-
#initialize(seed, options = {}) ⇒ Address
constructor
Initialize the address.
-
#inspect ⇒ String
Get a pretty printed address inspection.
-
#socket(socket_timeout, ssl_options = {}) ⇒ Mongo::Socket::SSL, ...
Get a socket for the provided address, given the options.
-
#to_s ⇒ String
Get the address as a string.
Constructor Details
#initialize(seed, options = {}) ⇒ Address
Initialize the address.
69 70 71 72 73 |
# File 'lib/mongo/address.rb', line 69 def initialize(seed, = {}) @seed = seed @host, @port = parse_host_port @options = end |
Instance Attribute Details
#host ⇒ String (readonly)
Returns host The original host name.
79 80 81 |
# File 'lib/mongo/address.rb', line 79 def host @host end |
#port ⇒ Integer (readonly)
Returns port The port.
82 83 84 |
# File 'lib/mongo/address.rb', line 82 def port @port end |
#seed ⇒ String (readonly)
Returns seed The seed address.
76 77 78 |
# File 'lib/mongo/address.rb', line 76 def seed @seed end |
Instance Method Details
#==(other) ⇒ true, false
Check equality of the address to another.
94 95 96 97 |
# File 'lib/mongo/address.rb', line 94 def ==(other) return false unless other.is_a?(Address) host == other.host && port == other.port end |
#connect_socket!(socket) ⇒ Object
Connect a socket.
184 185 186 |
# File 'lib/mongo/address.rb', line 184 def connect_socket!(socket) socket.connect!(connect_timeout) end |
#eql?(other) ⇒ true, false
Check equality for hashing.
109 110 111 |
# File 'lib/mongo/address.rb', line 109 def eql?(other) self == other end |
#hash ⇒ Integer
Calculate the hash value for the address.
121 122 123 |
# File 'lib/mongo/address.rb', line 121 def hash [ host, port ].hash end |
#inspect ⇒ String
Get a pretty printed address inspection.
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 = {}) ⇒ Mongo::Socket::SSL, ...
Get a socket for the provided address, given the options.
The address the socket connects to is determined by the algorithm described in the #intialize_resolver! documentation. Each time this method is called, #initialize_resolver! will be called, meaning that a new hostname lookup will occur. This is done so that any changes to which addresses the hostname resolves to will be picked up even if a socket has been connected to it before.
154 155 156 |
# File 'lib/mongo/address.rb', line 154 def socket(socket_timeout, = {}) create_resolver().socket(socket_timeout, ) end |
#to_s ⇒ String
Get the address as a string.
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/mongo/address.rb', line 166 def to_s if port if host.include?(':') "[#{host}]:#{port}" else "#{host}:#{port}" end else host end end |