class Bones::RPC::Connection

This class contains behaviour of Bones::RPC socket connections.

@since 0.0.1

Constants

TIMEOUT

The default connection timeout, in seconds.

@since 0.0.1

Attributes

node[R]
socket[R]

Public Class Methods

new(node) click to toggle source
# File lib/bones/rpc/connection.rb, line 100
def initialize(node)
  @node = node
  @socket = nil
end
writer_class(klass = nil) click to toggle source
# File lib/bones/rpc/connection.rb, line 19
def self.writer_class(klass = nil)
  if klass.nil?
    @writer_class
  else
    @writer_class = klass
  end
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 0.0.1

# File lib/bones/rpc/connection.rb, line 35
def alive?
  connected? ? @socket.alive? : false
end
cleanup_socket(socket) click to toggle source
# File lib/bones/rpc/connection.rb, line 39
def cleanup_socket(socket)
  if @writer
    @writer.async.terminate if @writer.alive?
    @writer = nil
  end
  @node.cleanup_socket(socket)
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 0.0.1

# File lib/bones/rpc/connection.rb, line 55
def connect
  if @writer
    @writer.terminate
    @writer = nil
  end
  @socket = if !!options[:ssl]
    self.class::Socket::SSL.connect(host, port, timeout)
  else
    self.class::Socket::TCP.connect(host, port, timeout)
  end
  writer
  return true
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 0.0.1

# File lib/bones/rpc/connection.rb, line 77
def connected?
  !!@socket
end
disconnect() click to toggle source

Disconnect from the server.

@example Disconnect from the server.

connection.disconnect

@return [ nil ] nil.

@since 0.0.1

# File lib/bones/rpc/connection.rb, line 89
def disconnect
  @socket.close
rescue
ensure
  @socket = nil
end
host() click to toggle source
# File lib/bones/rpc/connection.rb, line 96
def host
  node.address.ip
end
inspect() click to toggle source
# File lib/bones/rpc/connection.rb, line 105
def inspect
  "<#{self.class} \"#{node.address.resolved}\">"
end
options() click to toggle source
# File lib/bones/rpc/connection.rb, line 109
def options
  node.options
end
port() click to toggle source
# File lib/bones/rpc/connection.rb, line 113
def port
  node.address.port
end
timeout() click to toggle source
# File lib/bones/rpc/connection.rb, line 117
def timeout
  options[:timeout] || Connection::TIMEOUT
end
write(operations) click to toggle source
# File lib/bones/rpc/connection.rb, line 121
def write(operations)
  raise NotImplementedError, "Connection#write not implemented for this backend"
end

Private Instance Methods

with_connection() { |socket| ... } click to toggle source

Yields a connected socket to the calling back. It will attempt to reconnect the socket if it is not connected.

@api private

@example Write to the connection.

with_connection do |socket|
  socket.write(buf)
end

@return The yielded block

@since 0.0.1

# File lib/bones/rpc/connection.rb, line 140
def with_connection
  connect if @socket.nil? || !@socket.alive?
  yield @socket
end
writer() click to toggle source
# File lib/bones/rpc/connection.rb, line 145
def writer
  @writer ||= self.class.writer_class.new(self, @socket, node.adapter)
end