Class: Baleen::Client

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/baleen/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, debug = false) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/baleen/client.rb', line 10

def initialize(host, port, debug=false)
  Celluloid.logger = nil unless debug
  @socket = TCPSocket.open(host, port)
end

Instance Method Details

#closeObject



26
27
28
29
30
31
# File 'lib/baleen/client.rb', line 26

def close
  @socket.close if @socket
  hl_warn "Connection closed"

rescue IOError; nil
end

#handle_response(msg) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/baleen/client.rb', line 33

def handle_response(msg)
  if msg.nil?
    raise RuntimeError, 'Connection closed by server'
  end

  response = Serializable.deserialize(msg)

  if response.is_a? Message::Base
    response.print_message
  end

  if response.terminate?
    response
  else
    nil
  end
end

#request(request) ⇒ Object



15
16
17
# File 'lib/baleen/client.rb', line 15

def request(request)
  @socket.puts(request.to_json)
end

#wait_responseObject



19
20
21
22
23
24
# File 'lib/baleen/client.rb', line 19

def wait_response
  loop {
    response = handle_response(@socket.gets)
    return response if response
  }
end