Parent

Class/Module Index [+]

Quicksearch

Moped::Connection

This class contains behaviour of database socket connections.

@api private

Attributes

host[R]
options[R]
port[R]
timeout[R]

Public Class Methods

new(host, port, timeout, options = {}) click to toggle source

Initialize the connection.

@example Initialize the connection.

Connection.new("localhost", 27017, 5)

@param [ String ] host The host to connect to. @param [ Integer ] post The server port. @param [ Integer ] timeout The connection timeout. @param [ Hash ] options Options for the connection.

@option options [ Boolean ] :ssl Connect using SSL @since 1.0.0

# File lib/moped/connection.rb, line 82
def initialize(host, port, timeout, options = {})
  @sock = nil
  @request_id = 0
  @host, @port, @timeout, @options = host, port, timeout, options
end

Public Instance Methods

alive?() click to toggle source

Is the connection alive?

@example Is the connection alive?

connection.alive?

@return [ true, false ] If the connection is alive.

@since 1.0.0

# File lib/moped/connection.rb, line 23
def alive?
  connected? ? @sock.alive? : false
end
connect() click to toggle source

Connect to the server defined by @host, @port without timeout @timeout.

@example Open the connection

connection.connect

@return [ TCPSocket ] The socket.

@since 1.0.0

# File lib/moped/connection.rb, line 35
def connect
  @sock = if !!options[:ssl]
    Sockets::SSL.connect(host, port, timeout)
  else
    Sockets::TCP.connect(host, port, timeout)
  end
end
connected?() click to toggle source

Is the connection connected?

@example Is the connection connected?

connection.connected?

@return [ true, false ] If the connection is connected.

@since 1.0.0

# File lib/moped/connection.rb, line 51
def connected?
  !!@sock
end
disconnect() click to toggle source

Disconnect from the server.

@example Disconnect from the server.

connection.disconnect

@return [ nil ] nil.

@since 1.0.0

# File lib/moped/connection.rb, line 63
def disconnect
  @sock.close
rescue
ensure
  @sock = nil
end
read() click to toggle source

Read from the connection.

@example Read from the connection.

connection.read

@return [ Hash ] The returned document.

@since 1.0.0

# File lib/moped/connection.rb, line 96
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.deserialize(buffer)
      end
    end
    reply
  end
end
receive_replies(operations) click to toggle source

Get the replies to the database operation.

@example Get the replies.

connection.receive_replies(operations)

@param [ Array<Message> ] operations The query or get more ops.

@return [ Array<Hash> ] The returned deserialized documents.

@since 1.0.0

# File lib/moped/connection.rb, line 133
def receive_replies(operations)
  operations.map do |operation|
    operation.receive_replies(self)
  end
end
write(operations) click to toggle source

Write to the connection.

@example Write to the connection.

connection.write(data)

@param [ Array<Message> ] operations The database operations.

@return [ Integer ] The number of bytes written.

@since 1.0.0

# File lib/moped/connection.rb, line 149
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.