Class: Bones::RPC::Connection
- Inherits:
-
Object
- Object
- Bones::RPC::Connection
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.
Defined Under Namespace
Modules: Socket
Classes: Reader, Writer
Constant Summary
collapse
- TIMEOUT =
The default connection timeout, in seconds.
5
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Connection.
94
95
96
97
|
# File 'lib/bones/rpc/connection.rb', line 94
def initialize(node)
@node = node
@socket = nil
end
|
Instance Attribute Details
#node ⇒ Object
19
20
21
|
# File 'lib/bones/rpc/connection.rb', line 19
def node
@node
end
|
#socket ⇒ Object
19
20
21
|
# File 'lib/bones/rpc/connection.rb', line 19
def socket
@socket
end
|
Instance Method Details
#alive? ⇒ true, false
29
30
31
|
# File 'lib/bones/rpc/connection.rb', line 29
def alive?
connected? ? @socket.alive? : false
end
|
#cleanup_socket(socket) ⇒ Object
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
|
#connect ⇒ TCPSocket
Connect to the server defined by @host, @port without timeout @timeout.
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?
71
72
73
|
# File 'lib/bones/rpc/connection.rb', line 71
def connected?
!!@socket
end
|
#disconnect ⇒ nil
Disconnect from the server.
83
84
85
86
87
88
|
# File 'lib/bones/rpc/connection.rb', line 83
def disconnect
@socket.close
rescue
ensure
@socket = nil
end
|
#host ⇒ Object
90
91
92
|
# File 'lib/bones/rpc/connection.rb', line 90
def host
node.address.ip
end
|
#inspect ⇒ Object
99
100
101
|
# File 'lib/bones/rpc/connection.rb', line 99
def inspect
"<#{self.class} \"#{node.address.resolved}\">"
end
|
#options ⇒ Object
103
104
105
|
# File 'lib/bones/rpc/connection.rb', line 103
def options
node.options
end
|
#port ⇒ Object
107
108
109
|
# File 'lib/bones/rpc/connection.rb', line 107
def port
node.address.port
end
|
#timeout ⇒ Object
111
112
113
|
# File 'lib/bones/rpc/connection.rb', line 111
def timeout
options[:timeout] || Connection::TIMEOUT
end
|
#write(operations) ⇒ Object
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
|