Class: Armada::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(host, port, password) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
# File 'lib/armada/connection.rb', line 7

def initialize(host, port, password)
  @host     = host
  @port     = port
  @password = password
  connect
end

Instance Method Details

#connectObject



14
15
16
17
18
19
20
21
22
# File 'lib/armada/connection.rb', line 14

def connect
  begin
    @socket = TCPSocket.new(@host, @port)
    @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
    query(["auth", @password]) if @password
  rescue
    raise Armada::ConnectionError, "could not connect"
  end
end

#query(q) ⇒ Object



24
25
26
27
28
29
# File 'lib/armada/connection.rb', line 24

def query(q)
  request = ActiveSupport::JSON.encode(q)
  @socket.write(request << "\r\n")
  status, value = ActiveSupport::JSON.decode(@socket.gets)
  status == 0 ? value : raise(Armada::ServerError.new(request),value)
end