Class: Moped::Connection Private
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
This class contains behaviour of database socket connections.
Defined Under Namespace
Classes: TCPSocket
Instance Method Summary collapse
-
#alive? ⇒ true, false
private
Is the connection alive?.
-
#connect ⇒ TCPSocket
private
Connect to the server defined by @host, @port without timeout @timeout.
-
#connected? ⇒ true, false
private
Is the connection connected?.
-
#disconnect ⇒ nil
private
Disconnect from the server.
-
#initialize(host, port, timeout) ⇒ Connection
constructor
private
Initialize the connection.
-
#read ⇒ Hash
private
Read from the connection.
-
#receive_replies(operations) ⇒ Array<Hash>
private
Get the replies to the database operation.
-
#write(operations) ⇒ Integer
private
Write to the connection.
Constructor Details
#initialize(host, port, timeout) ⇒ 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.
Initialize the connection.
69 70 71 72 73 74 75 |
# File 'lib/moped/connection.rb', line 69 def initialize(host, port, timeout) @sock = nil @request_id = 0 @host = host @port = port @timeout = timeout end |
Instance Method Details
#alive? ⇒ true, false
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.
Is the connection alive?
17 18 19 |
# File 'lib/moped/connection.rb', line 17 def alive? connected? ? @sock.alive? : false end |
#connect ⇒ TCPSocket
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.
Connect to the server defined by @host, @port without timeout @timeout.
29 30 31 |
# File 'lib/moped/connection.rb', line 29 def connect create_connection end |
#connected? ⇒ true, false
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.
Is the connection connected?
41 42 43 |
# File 'lib/moped/connection.rb', line 41 def connected? !!@sock end |
#disconnect ⇒ nil
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.
Disconnect from the server.
53 54 55 56 57 58 |
# File 'lib/moped/connection.rb', line 53 def disconnect @sock.close rescue ensure @sock = nil end |
#read ⇒ Hash
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.
Read from the connection.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/moped/connection.rb', line 85 def read with_connection do |socket| reply = Protocol::Reply.allocate data = socket.read(36) unless data raise Errors::ConnectionFailure, "Attempted read from socket which returned no data." end response = data.unpack('l<5q<l<2') reply.length, reply.request_id, reply.response_to, reply.op_code, reply.flags, reply.cursor_id, reply.offset, reply.count = response if reply.count == 0 reply.documents = [] else sock_read = socket.read(reply.length - 36) buffer = StringIO.new(sock_read) reply.documents = reply.count.times.map do BSON::Document.deserialize(buffer) end end reply end end |
#receive_replies(operations) ⇒ Array<Hash>
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 the replies to the database operation.
127 128 129 130 131 |
# File 'lib/moped/connection.rb', line 127 def receive_replies(operations) operations.map do |operation| operation.receive_replies(self) end end |
#write(operations) ⇒ Integer
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.
Write to the connection.
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/moped/connection.rb', line 143 def write(operations) buf = "" operations.each do |operation| operation.request_id = (@request_id += 1) operation.serialize(buf) end with_connection do |socket| socket.write(buf) end end |