Class: Bones::RPC::Connection

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

Overview

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

Since:

  • 2.0.0

Defined Under Namespace

Modules: Socket Classes: Reader, Writer

Constant Summary collapse

TIMEOUT =

The default connection timeout, in seconds.

Since:

  • 2.0.0

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Connection

Returns a new instance of Connection.

Since:

  • 2.0.0



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

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

Instance Attribute Details

#nodeObject (readonly)

Since:

  • 2.0.0



19
20
21
# File 'lib/bones/rpc/connection.rb', line 19

def node
  @node
end

#socketObject (readonly)

Since:

  • 2.0.0



19
20
21
# File 'lib/bones/rpc/connection.rb', line 19

def socket
  @socket
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:

  • 1.0.0



29
30
31
# File 'lib/bones/rpc/connection.rb', line 29

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

#cleanup_socket(socket) ⇒ Object

Since:

  • 2.0.0



33
34
35
36
37
38
39
# File 'lib/bones/rpc/connection.rb', line 33

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:

  • 1.0.0



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bones/rpc/connection.rb', line 49

def connect
  if @writer
    @writer.terminate
    @writer = nil
  end
  @socket = if !!options[:ssl]
    Socket::SSL.connect(host, port, timeout)
  else
    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:

  • 1.0.0



71
72
73
# File 'lib/bones/rpc/connection.rb', line 71

def connected?
  !!@socket
end

#disconnectnil

Disconnect from the server.

Examples:

Disconnect from the server.

connection.disconnect

Returns:

  • (nil)

    nil.

Since:

  • 1.0.0



83
84
85
86
87
88
# File 'lib/bones/rpc/connection.rb', line 83

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

#hostObject

Since:

  • 2.0.0



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

def host
  node.address.ip
end

#inspectObject

Since:

  • 2.0.0



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

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

#optionsObject

Since:

  • 2.0.0



103
104
105
# File 'lib/bones/rpc/connection.rb', line 103

def options
  node.options
end

#portObject

Since:

  • 2.0.0



107
108
109
# File 'lib/bones/rpc/connection.rb', line 107

def port
  node.address.port
end

#timeoutObject

Since:

  • 2.0.0



111
112
113
# File 'lib/bones/rpc/connection.rb', line 111

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

#write(operations) ⇒ Object

Since:

  • 2.0.0



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

def write(operations)
  with_connection do |socket|
    writer.write(operations)
  end
end