Class: Mongo::Address::IPv4

Inherits:
Object
  • Object
show all
Defined in:
build/ruby-driver-v2.19/lib/mongo/address/ipv4.rb

Overview

Sets up resolution with IPv4 support if the address is an ip address.

Since:

  • 2.0.0

Constant Summary collapse

MATCH =

The regular expression to use to match an IPv4 ip address.

Since:

  • 2.0.0

Regexp.new('/\./').freeze
SPLIT =

Split value constant.

Since:

  • 2.1.0

':'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, host_name = nil) ⇒ IPv4

Initialize the IPv4 resolver.

Examples:

Initialize the resolver.

IPv4.new("127.0.0.1", 27017, 'localhost')

Parameters:

  • host (String)

    The host.

  • port (Integer)

    The port.

Since:

  • 2.0.0



72
73
74
75
76
# File 'build/ruby-driver-v2.19/lib/mongo/address/ipv4.rb', line 72

def initialize(host, port, host_name=nil)
  @host = host
  @port = port
  @host_name = host_name
end

Instance Attribute Details

#hostString (readonly)

Returns host The host.

Returns:

  • (String)

    host The host.

Since:

  • 2.0.0



28
29
30
# File 'build/ruby-driver-v2.19/lib/mongo/address/ipv4.rb', line 28

def host
  @host
end

#host_nameString (readonly)

Returns host_name The original host name.

Returns:

  • (String)

    host_name The original host name.

Since:

  • 2.0.0



31
32
33
# File 'build/ruby-driver-v2.19/lib/mongo/address/ipv4.rb', line 31

def host_name
  @host_name
end

#portInteger (readonly)

Returns port The port.

Returns:

  • (Integer)

    port The port.

Since:

  • 2.0.0



34
35
36
# File 'build/ruby-driver-v2.19/lib/mongo/address/ipv4.rb', line 34

def port
  @port
end

Class Method Details

.parse(address) ⇒ Array<String, Integer>

Parse an IPv4 address into its host and port.

Examples:

Parse the address.

IPv4.parse("127.0.0.1:28011")

Parameters:

  • address (String)

    The address to parse.

Returns:

  • (Array<String, Integer>)

    The host and port pair.

Since:

  • 2.0.0



56
57
58
59
60
61
# File 'build/ruby-driver-v2.19/lib/mongo/address/ipv4.rb', line 56

def self.parse(address)
  parts = address.split(SPLIT)
  host = parts[0]
  port = (parts[1] || 27017).to_i
  [ host, port ]
end

Instance Method Details

#socket(socket_timeout, options = {}) ⇒ Mongo::Socket::SSL, Mongo::Socket::TCP

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

Get an IPv4 socket.

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

Parameters:

  • socket_timeout (Float)

    The socket timeout.

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

    The options.

Options Hash (options):

  • :connect_timeout (Float)

    Connect timeout.

  • :ssl (true | false)

    Whether to use TLS.

  • :ssl_ca_cert (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_ca_cert_object (Array<OpenSSL::X509::Certificate>)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_ca_cert_string (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_cert (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_cert_object (OpenSSL::X509::Certificate)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_cert_string (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key_object (OpenSSL::PKey)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key_pass_phrase (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key_string (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_verify (true, false)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_verify_certificate (true, false)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_verify_hostname (true, false)

    Same as the corresponding Client/Socket::SSL option.

Returns:

Since:

  • 2.0.0



119
120
121
122
123
124
125
# File 'build/ruby-driver-v2.19/lib/mongo/address/ipv4.rb', line 119

def socket(socket_timeout, options = {})
  if options[:ssl]
    Socket::SSL.new(host, port, host_name, socket_timeout, Socket::PF_INET, options)
  else
    Socket::TCP.new(host, port, socket_timeout, Socket::PF_INET, options)
  end
end