Class: Bones::RPC::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/bones/rpc/connection.rb,
lib/bones/rpc/connection/socket/connectable.rb

Overview

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

Since:

  • 0.0.1

Direct Known Subclasses

Synchronous::Connection

Defined Under Namespace

Modules: Socket

Constant Summary collapse

TIMEOUT =

The default connection timeout, in seconds.

Since:

  • 0.0.1

5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Connection

Returns a new instance of Connection.

Since:

  • 0.0.1



100
101
102
103
# File 'lib/bones/rpc/connection.rb', line 100

def initialize(node)
  @node = node
  @socket = nil
end

Instance Attribute Details

#nodeObject (readonly)

Since:

  • 0.0.1



17
18
19
# File 'lib/bones/rpc/connection.rb', line 17

def node
  @node
end

#socketObject (readonly)

Since:

  • 0.0.1



17
18
19
# File 'lib/bones/rpc/connection.rb', line 17

def socket
  @socket
end

Class Method Details

.writer_class(klass = nil) ⇒ Object

Since:

  • 0.0.1



19
20
21
22
23
24
25
# 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

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:

  • 0.0.1



35
36
37
# File 'lib/bones/rpc/connection.rb', line 35

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

#cleanup_socket(socket) ⇒ Object

Since:

  • 0.0.1



39
40
41
42
43
44
45
# 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

#connectTCPSocket

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

Examples:

Open the connection

connection.connect

Returns:

  • (TCPSocket)

    The socket.

Since:

  • 0.0.1



55
56
57
58
59
60
61
62
63
64
65
66
67
# 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?true, false

Is the connection connected?

Examples:

Is the connection connected?

connection.connected?

Returns:

  • (true, false)

    If the connection is connected.

Since:

  • 0.0.1



77
78
79
# File 'lib/bones/rpc/connection.rb', line 77

def connected?
  !!@socket
end

#disconnectnil

Disconnect from the server.

Examples:

Disconnect from the server.

connection.disconnect

Returns:

  • (nil)

    nil.

Since:

  • 0.0.1



89
90
91
92
93
94
# File 'lib/bones/rpc/connection.rb', line 89

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

#hostObject

Since:

  • 0.0.1



96
97
98
# File 'lib/bones/rpc/connection.rb', line 96

def host
  node.address.ip
end

#inspectObject

Since:

  • 0.0.1



105
106
107
# File 'lib/bones/rpc/connection.rb', line 105

def inspect
  "<#{self.class} \"#{node.address.resolved}\">"
end

#optionsObject

Since:

  • 0.0.1



109
110
111
# File 'lib/bones/rpc/connection.rb', line 109

def options
  node.options
end

#portObject

Since:

  • 0.0.1



113
114
115
# File 'lib/bones/rpc/connection.rb', line 113

def port
  node.address.port
end

#timeoutObject

Since:

  • 0.0.1



117
118
119
# File 'lib/bones/rpc/connection.rb', line 117

def timeout
  options[:timeout] || Connection::TIMEOUT
end

#write(operations) ⇒ Object

Raises:

  • (NotImplementedError)

Since:

  • 0.0.1



121
122
123
# File 'lib/bones/rpc/connection.rb', line 121

def write(operations)
  raise NotImplementedError, "Connection#write not implemented for this backend"
end