Class: Mongo::Server::Connection
- Inherits:
-
Object
- Object
- Mongo::Server::Connection
- Extended by:
- Forwardable
- Includes:
- Monitoring::Publishable, Connectable
- Defined in:
- lib/mongo/server/connection.rb
Overview
This class models the socket connections for servers and their behavior.
Constant Summary collapse
- PING =
The ping command.
{ :ping => 1 }.freeze
- PING_MESSAGE =
Ping message.
Protocol::Query.new(Database::ADMIN, Database::COMMAND, PING, :limit => -1)
- PING_BYTES =
The ping message as raw bytes.
PING_MESSAGE.serialize.to_s.freeze
Constants included from Connectable
Mongo::Server::Connectable::SSL, Mongo::Server::Connectable::TIMEOUT
Instance Attribute Summary collapse
-
#authenticator ⇒ Mongo::Auth::CR, ...
readonly
Authenticator The authentication strategy.
Attributes included from Monitoring::Publishable
Attributes included from Connectable
Instance Method Summary collapse
-
#authenticated? ⇒ true, false
Is this connection authenticated.
-
#connect! ⇒ true
Tell the underlying socket to establish a connection to the host.
-
#disconnect! ⇒ true
Disconnect the connection.
-
#dispatch(messages, operation_id = nil) ⇒ Protocol::Reply
Dispatch the provided messages to the connection.
-
#initialize(server, options = {}) ⇒ Connection
constructor
private
Initialize a new socket connection from the client to the server.
-
#ping ⇒ true, false
Ping the connection to see if the server is responding to commands.
Methods included from Monitoring::Publishable
Methods included from Connectable
#connectable?, #connected?, #timeout
Constructor Details
#initialize(server, options = {}) ⇒ Connection
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.
Connection must never be directly instantiated outside of a Server.
Initialize a new socket connection from the client to the server.
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/mongo/server/connection.rb', line 146 def initialize(server, = {}) @address = server.address @monitoring = server.monitoring @options = .freeze @server = server @ssl_options = .reject { |k, v| !k.to_s.start_with?(SSL) } @socket = nil @pid = Process.pid setup_authentication! end |
Instance Attribute Details
#authenticator ⇒ Mongo::Auth::CR, ... (readonly)
Returns authenticator The authentication strategy.
43 44 45 |
# File 'lib/mongo/server/connection.rb', line 43 def authenticator @authenticator end |
Instance Method Details
#authenticated? ⇒ true, false
Is this connection authenticated. Will return true if authorization details were provided and authentication passed.
60 61 62 |
# File 'lib/mongo/server/connection.rb', line 60 def authenticated? !!@authenticated end |
#connect! ⇒ true
This method mutates the connection class by setting a socket if one previously did not exist.
Tell the underlying socket to establish a connection to the host.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mongo/server/connection.rb', line 75 def connect! unless socket @socket = address.socket(timeout, ) socket.connect! if authenticator authenticator.login(self) @authenticated = true end end true end |
#disconnect! ⇒ true
This method mutates the connection by setting the socket to nil if the closing succeeded.
Disconnect the connection.
98 99 100 101 102 103 104 105 |
# File 'lib/mongo/server/connection.rb', line 98 def disconnect! if socket socket.close @socket = nil @authenticated = false end true end |
#dispatch(messages, operation_id = nil) ⇒ Protocol::Reply
This method is named dispatch since ‘send’ is a core Ruby method on all objects.
Dispatch the provided messages to the connection. If the last message requires a response a reply will be returned.
122 123 124 125 126 127 128 129 130 |
# File 'lib/mongo/server/connection.rb', line 122 def dispatch(, operation_id = nil) if monitoring.subscribers?(Monitoring::COMMAND) publish_command(, operation_id || Monitoring.next_operation_id) do |msgs| deliver(msgs) end else deliver() end end |
#ping ⇒ true, false
This uses a pre-serialized ping message for optimization.
Ping the connection to see if the server is responding to commands. This is non-blocking on the server side.
168 169 170 171 172 173 174 |
# File 'lib/mongo/server/connection.rb', line 168 def ping ensure_connected do |socket| socket.write(PING_BYTES) reply = Protocol::Reply.deserialize(socket, ) reply.documents[0][Operation::Result::OK] == 1 end end |