Class: Moped::Connection
- Inherits:
-
Object
- Object
- Moped::Connection
- Includes:
- Authenticatable
- Defined in:
- lib/moped/connection.rb,
lib/moped/connection/manager.rb,
lib/moped/connection/socket/ssl.rb,
lib/moped/connection/socket/tcp.rb,
lib/moped/connection/socket/connectable.rb
Overview
This class contains behaviour of database socket connections.
Defined Under Namespace
Constant Summary collapse
- TIMEOUT =
The default connection timeout, in seconds.
5
Instance Attribute Summary collapse
-
#host ⇒ String
The ip address of the host.
-
#options ⇒ Hash
The connection options.
-
#port ⇒ String
The port the connection connects on.
- #timeout ⇒ Object
Instance Method Summary collapse
-
#alive? ⇒ true, false
Is the connection alive?.
-
#connect ⇒ TCPSocket
Connect to the server defined by @host, @port without timeout @timeout.
-
#connected? ⇒ true, false
Is the connection connected?.
-
#disconnect ⇒ nil
Disconnect from the server.
-
#initialize(host, port, timeout, options = {}) ⇒ Connection
constructor
Initialize the connection.
-
#read ⇒ Hash
Read from the connection.
-
#receive_replies(operations) ⇒ Array<Hash>
Get the replies to the database operation.
-
#write(operations) ⇒ Integer
Write to the connection.
Methods included from Authenticatable
#apply_credentials, #credentials, #login, #logout
Constructor Details
#initialize(host, port, timeout, options = {}) ⇒ Connection
Initialize the connection.
96 97 98 99 100 101 102 103 |
# File 'lib/moped/connection.rb', line 96 def initialize(host, port, timeout, = {}) @host = host @port = port @timeout = timeout @options = @sock = nil @request_id = 0 end |
Instance Attribute Details
#host ⇒ String
Returns The ip address of the host.
26 27 28 |
# File 'lib/moped/connection.rb', line 26 def host @host end |
#options ⇒ Hash
Returns The connection options.
26 |
# File 'lib/moped/connection.rb', line 26 attr_reader :host, :options, :port, :timeout |
#port ⇒ String
Returns The port the connection connects on.
26 |
# File 'lib/moped/connection.rb', line 26 attr_reader :host, :options, :port, :timeout |
#timeout ⇒ Object
26 |
# File 'lib/moped/connection.rb', line 26 attr_reader :host, :options, :port, :timeout |
Instance Method Details
#alive? ⇒ true, false
Is the connection alive?
36 37 38 |
# File 'lib/moped/connection.rb', line 36 def alive? connected? ? @sock.alive? : false end |
#connect ⇒ TCPSocket
Connect to the server defined by @host, @port without timeout @timeout.
48 49 50 51 52 53 54 55 |
# File 'lib/moped/connection.rb', line 48 def connect credentials.clear @sock = if !![:ssl] Socket::SSL.connect(host, port, timeout) else Socket::TCP.connect(host, port, timeout) end end |
#connected? ⇒ true, false
Is the connection connected?
65 66 67 |
# File 'lib/moped/connection.rb', line 65 def connected? !!@sock end |
#disconnect ⇒ nil
Disconnect from the server.
77 78 79 80 81 82 |
# File 'lib/moped/connection.rb', line 77 def disconnect @sock.close rescue ensure @sock = nil end |
#read ⇒ Hash
Read from the connection.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/moped/connection.rb', line 113 def read with_connection do |socket| reply = Protocol::Reply.allocate data = read_data(socket, 36) 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 = read_data(socket, reply.length - 36) buffer = StringIO.new(sock_read) reply.documents = reply.count.times.map do ::BSON::Document.from_bson(buffer) end end reply end end |
#receive_replies(operations) ⇒ Array<Hash>
Get the replies to the database operation.
150 151 152 153 154 |
# File 'lib/moped/connection.rb', line 150 def receive_replies(operations) operations.map do |operation| operation.receive_replies(self) end end |
#write(operations) ⇒ Integer
Write to the connection.
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/moped/connection.rb', line 166 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 |