Class: Librr::CmdClient

Inherits:
Object
  • Object
show all
Includes:
Logger::ClassLogger
Defined in:
lib/librr/cmd_client.rb

Instance Method Summary collapse

Methods included from Logger::ClassLogger

#debug, #info, #logger

Constructor Details

#initialize(host, port) ⇒ CmdClient

Returns a new instance of CmdClient.



11
12
13
14
# File 'lib/librr/cmd_client.rb', line 11

def initialize host, port
  @host = host
  @port = port
end

Instance Method Details

#check_start(sync = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/librr/cmd_client.rb', line 16

def check_start(sync=false)
  begin
    self.run_cmd(:ping)
    return true
  rescue Errno::ECONNREFUSED => e
  end

  ServerStarter.start_server(sync)
  return false
end

#cmd(cmd, params = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/librr/cmd_client.rb', line 27

def cmd cmd, params={}
  begin
    return self.run_cmd cmd, params
  rescue Errno::ECONNREFUSED => e
  end

  puts "daemon not start, starting.."
  ServerStarter.start_server(false)
  ServerStarter.wait_for_server_started do
    self.run_cmd cmd, **params
  end
end

#run_cmd(cmd, params = {}) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/librr/cmd_client.rb', line 40

def run_cmd cmd, params={}
  params[:cmd] = cmd
  url = '/cmd'
  self.debug("sending: #{params}")
  result = Net::HTTP.post_form(URI.parse("http://#{@host}:#{@port}#{url}"), params)
  JSON.load(result.body)
end