Class: RubyVolt::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_volt/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(, 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=#{[:connection_id]}" if logged_in?
    end
  end
  establish!
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/ruby_volt/connection.rb', line 6

def host
  @host
end

#login_dataObject (readonly)

Returns the value of attribute login_data.



6
7
8
# File 'lib/ruby_volt/connection.rb', line 6

def 
  @login_data
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/ruby_volt/connection.rb', line 6

def port
  @port
end

#socketObject (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

Returns:

  • (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_timeoutObject



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

#inspectObject



47
48
49
# File 'lib/ruby_volt/connection.rb', line 47

def inspect
  "#<#{self.class.name} [#{host}:#{port}]: server_host_id=#{[:server_host_id]} connection_id=#{[:connection_id]} login_protocol=#{} procedure_protocol=#{procedure_protocol}>"
end

#logged_in?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/ruby_volt/connection.rb', line 85

def logged_in?
   && [:auth_result] == 0
end

#login_protocolObject



27
28
29
# File 'lib/ruby_volt/connection.rb', line 27

def 
  base.
end

#pingObject



106
107
108
# File 'lib/ruby_volt/connection.rb', line 106

def ping
  call_procedure("@Ping")
end

#procedure_protocolObject



31
32
33
# File 'lib/ruby_volt/connection.rb', line 31

def procedure_protocol
  base.procedure_protocol
end

#procedure_timeoutObject



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

#servicenameObject



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

def servicename
  base.servicename
end

#socket_open?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ruby_volt/connection.rb', line 65

def socket_open?
  socket && !socket.closed?
end

#unavailable?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/ruby_volt/connection.rb', line 89

def unavailable?
  !logged_in?||!socket_open?
end