Class: Moped::Connection

Inherits:
Object
  • Object
show all
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.

Since:

  • 2.0.0

Defined Under Namespace

Modules: Manager, Socket

Constant Summary collapse

TIMEOUT =

The default connection timeout, in seconds.

Since:

  • 2.0.0

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Authenticatable

#apply_credentials, #credentials, #login, #logout

Constructor Details

#initialize(host, port, timeout, options = {}) ⇒ Connection

Initialize the connection.

Examples:

Initialize the connection.

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

Parameters:

  • host (String)

    The host to connect to.

  • post (Integer)

    The server port.

  • timeout (Integer)

    The connection timeout.

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

    Options for the connection.

Options Hash (options):

  • :ssl (Boolean)

    Connect using SSL

Since:

  • 1.0.0



96
97
98
99
100
101
102
103
# File 'lib/moped/connection.rb', line 96

def initialize(host, port, timeout, options = {})
  @host = host
  @port = port
  @timeout = timeout
  @options = options
  @sock = nil
  @request_id = 0
end

Instance Attribute Details

#hostString

Returns The ip address of the host.

Returns:

  • (String)

    The ip address of the host.



26
27
28
# File 'lib/moped/connection.rb', line 26

def host
  @host
end

#optionsHash

Returns The connection options.

Returns:

  • (Hash)

    The connection options.



26
# File 'lib/moped/connection.rb', line 26

attr_reader :host, :options, :port, :timeout

#portString

Returns The port the connection connects on.

Returns:

  • (String)

    The port the connection connects on.



26
# File 'lib/moped/connection.rb', line 26

attr_reader :host, :options, :port, :timeout

#timeoutObject

Since:

  • 2.0.0



26
# File 'lib/moped/connection.rb', line 26

attr_reader :host, :options, :port, :timeout

Instance Method Details

#alive?true, false

Is the connection alive?

Examples:

Is the connection alive?

connection.alive?

Returns:

  • (true, false)

    If the connection is alive.

Since:

  • 1.0.0



36
37
38
# File 'lib/moped/connection.rb', line 36

def alive?
  connected? ? @sock.alive? : false
end

#connectTCPSocket

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

Examples:

Open the connection

connection.connect

Returns:

  • (TCPSocket)

    The socket.

Since:

  • 1.0.0



48
49
50
51
52
53
54
55
# File 'lib/moped/connection.rb', line 48

def connect
  credentials.clear
  @sock = if !!options[:ssl]
    Socket::SSL.connect(host, port, timeout)
  else
    Socket::TCP.connect(host, port, timeout)
  end
end

#connected?true, false

Is the connection connected?

Examples:

Is the connection connected?

connection.connected?

Returns:

  • (true, false)

    If the connection is connected.

Since:

  • 1.0.0



65
66
67
# File 'lib/moped/connection.rb', line 65

def connected?
  !!@sock
end

#disconnectnil

Disconnect from the server.

Examples:

Disconnect from the server.

connection.disconnect

Returns:

  • (nil)

    nil.

Since:

  • 1.0.0



77
78
79
80
81
82
# File 'lib/moped/connection.rb', line 77

def disconnect
  @sock.close
rescue
ensure
  @sock = nil
end

#readHash

Read from the connection.

Examples:

Read from the connection.

connection.read

Returns:

  • (Hash)

    The returned document.

Since:

  • 1.0.0



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.

Examples:

Get the replies.

connection.receive_replies(operations)

Parameters:

  • operations (Array<Message>)

    The query or get more ops.

Returns:

  • (Array<Hash>)

    The returned deserialized documents.

Since:

  • 1.0.0



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.

Examples:

Write to the connection.

connection.write(data)

Parameters:

  • operations (Array<Message>)

    The database operations.

Returns:

  • (Integer)

    The number of bytes written.

Since:

  • 1.0.0



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