Class: RubyVolt::Connection
- Inherits:
-
Object
- Object
- RubyVolt::Connection
- Defined in:
- lib/ruby_volt/connection.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#login_data ⇒ Object
readonly
Returns the value of attribute login_data.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #benchmark(cycle = 1000) ⇒ Object
- #call_procedure(procedure, client_data = @opaque, *parameters) ⇒ Object
- #close! ⇒ Object
- #connect_timeout ⇒ Object
- #establish! ⇒ Object
-
#initialize(base, host, port, username, password) ⇒ Connection
constructor
A new instance of Connection.
- #inspect ⇒ Object
- #logged_in? ⇒ Boolean
- #login_protocol ⇒ Object
- #ping ⇒ Object
- #procedure_protocol ⇒ Object
- #procedure_timeout ⇒ Object
- #require_login! ⇒ Object
- #servicename ⇒ Object
- #socket_open? ⇒ Boolean
- #unavailable? ⇒ Boolean
Constructor Details
#initialize(base, host, port, username, password) ⇒ Connection
Returns a new instance of Connection.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ruby_volt/connection.rb', line 8 def initialize(base, host, port, username, password) @host, @port = host, port @mutex = Mutex.new # Semaphore @opaque = Helper.uniq_bytes(8) define_singleton_method :base do base end define_singleton_method :login! do request = LoginMessage.new(login_protocol, servicename, username, password) response_msg = dialog!(request, connect_timeout) response = LoginResponse.new(response_msg).unpack! @login_data = response.data if logged_in? puts "=== VoltDB wired [#{host}:#{port}]: connection_id=#{login_data[:connection_id]}" if logged_in? end end establish! end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
6 7 8 |
# File 'lib/ruby_volt/connection.rb', line 6 def host @host end |
#login_data ⇒ Object (readonly)
Returns the value of attribute login_data.
6 7 8 |
# File 'lib/ruby_volt/connection.rb', line 6 def login_data @login_data end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
6 7 8 |
# File 'lib/ruby_volt/connection.rb', line 6 def port @port end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
6 7 8 |
# File 'lib/ruby_volt/connection.rb', line 6 def socket @socket end |
Instance Method Details
#available? ⇒ Boolean
93 94 95 |
# File 'lib/ruby_volt/connection.rb', line 93 def available? !unavailable? end |
#benchmark(cycle = 1000) ⇒ Object
110 111 112 |
# File 'lib/ruby_volt/connection.rb', line 110 def benchmark(cycle = 1000) Helper.benchmark(cycle) {ping} # call @Ping - system stored procedure end |
#call_procedure(procedure, client_data = @opaque, *parameters) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/ruby_volt/connection.rb', line 97 def call_procedure(procedure, client_data = @opaque, *parameters) # The procedure invocation request contains the procedure to be called by name, and the serialized parameters to the procedure. The message also includes an opaque 8 byte client data value which will be returned with the response, and can be used by the client to correlate requests with responses. @mutex.synchronize do require_login! request = InvocationRequest.new(procedure_protocol, procedure, client_data, *parameters) dialog!(request, procedure_timeout) # Byte string end end |
#close! ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ruby_volt/connection.rb', line 69 def close! @login_data = nil if socket begin socket.close rescue ensure @socket = nil end end end |
#connect_timeout ⇒ Object
39 40 41 |
# File 'lib/ruby_volt/connection.rb', line 39 def connect_timeout base.connect_timeout end |
#establish! ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ruby_volt/connection.rb', line 51 def establish! close! attempts = 0 begin @socket = ::Socket.tcp(host, port, nil, nil, {:connect_timeout => connect_timeout}) rescue ::SystemCallError => e warn "#{e.class}: #{e}. Reinitializing connection..." unless attempts > 0 attempts += 1 retry else login! end end |
#inspect ⇒ Object
47 48 49 |
# File 'lib/ruby_volt/connection.rb', line 47 def inspect "#<#{self.class.name} [#{host}:#{port}]: server_host_id=#{login_data[:server_host_id]} connection_id=#{login_data[:connection_id]} login_protocol=#{login_protocol} procedure_protocol=#{procedure_protocol}>" end |
#logged_in? ⇒ Boolean
85 86 87 |
# File 'lib/ruby_volt/connection.rb', line 85 def logged_in? login_data && login_data[:auth_result] == 0 end |
#login_protocol ⇒ Object
27 28 29 |
# File 'lib/ruby_volt/connection.rb', line 27 def login_protocol base.login_protocol end |
#ping ⇒ Object
106 107 108 |
# File 'lib/ruby_volt/connection.rb', line 106 def ping call_procedure("@Ping") end |
#procedure_protocol ⇒ Object
31 32 33 |
# File 'lib/ruby_volt/connection.rb', line 31 def procedure_protocol base.procedure_protocol end |
#procedure_timeout ⇒ Object
43 44 45 |
# File 'lib/ruby_volt/connection.rb', line 43 def procedure_timeout base.procedure_timeout end |
#require_login! ⇒ Object
81 82 83 |
# File 'lib/ruby_volt/connection.rb', line 81 def require_login! establish! unless logged_in? end |
#servicename ⇒ Object
35 36 37 |
# File 'lib/ruby_volt/connection.rb', line 35 def servicename base.servicename end |
#socket_open? ⇒ Boolean
65 66 67 |
# File 'lib/ruby_volt/connection.rb', line 65 def socket_open? socket && !socket.closed? end |
#unavailable? ⇒ Boolean
89 90 91 |
# File 'lib/ruby_volt/connection.rb', line 89 def unavailable? !logged_in?||!socket_open? end |