Class: Procodile::ControlClient

Inherits:
Object
  • Object
show all
Defined in:
lib/procodile/control_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock_path, &block) ⇒ ControlClient

Returns a new instance of ControlClient.



7
8
9
10
11
12
13
14
15
16
# File 'lib/procodile/control_client.rb', line 7

def initialize(sock_path, &block)
  @socket = UNIXSocket.new(sock_path)
  if block_given?
    begin
      block.call(self)
    ensure
      disconnect
    end
  end
end

Class Method Details

.run(sock_path, command, options = {}) ⇒ Object



18
19
20
21
22
23
# File 'lib/procodile/control_client.rb', line 18

def self.run(sock_path, command, options = {})
  socket = self.new(sock_path)
  socket.run(command, options)
ensure
  socket.disconnect rescue nil
end

Instance Method Details

#disconnectObject



43
44
45
# File 'lib/procodile/control_client.rb', line 43

def disconnect
  @socket.close rescue nil
end

#run(command, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/procodile/control_client.rb', line 25

def run(command, options = {})
  @socket.puts("#{command} #{options.to_json}")
  if data = @socket.gets
    code, reply = data.strip.split(/\s+/, 2)
    if code.to_i == 200
      if reply && reply.length > 0
        JSON.parse(reply)
      else
        true
      end
    else
      raise Error, "Error from control server: #{code} (#{reply.inspect})"
    end
  else
    raise Error ,"Control server disconnected."
  end
end