Class: Explorer::Server::IPC

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO
Defined in:
lib/explorer/server/ipc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket_path = '/tmp/explorer_ipc', servers) ⇒ IPC

Returns a new instance of IPC.



11
12
13
14
15
16
# File 'lib/explorer/server/ipc.rb', line 11

def initialize(socket_path = '/tmp/explorer_ipc', servers)
  @socket_path = socket_path
  @server = UNIXServer.new(socket_path)
  @servers = servers
  async.run
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



9
10
11
# File 'lib/explorer/server/ipc.rb', line 9

def server
  @server
end

#socket_pathObject (readonly)

Returns the value of attribute socket_path.



9
10
11
# File 'lib/explorer/server/ipc.rb', line 9

def socket_path
  @socket_path
end

Instance Method Details

#handle_connection(socket) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/explorer/server/ipc.rb', line 29

def handle_connection(socket)
  loop do
    json = JSON.parse socket.readline
    case json['command']
    when 'map-list'
      socket.puts @servers.hostmap.to_json
    when 'map-add'
      @servers.hostmap[json['map']] = { host: json['host'], port: json['port'].to_i }
    when 'map-remove'
      @servers.hostmap.delete json['map']
    when 'cmd-tail'
      @servers.log_watcher.add(socket)
    when 'cmd-add'
      @servers.process_manager.add(json['label'], json['cmd'], working_dir: json['dir'] || ENV['PWD'])
    when 'cmd-start'
      @servers.process_manager.start(json['label'])
    when 'cmd-stop'
      @servers.process_manager.stop(json['label'])
    when 'cmd-remove'
      @servers.process_manager.remove(json['label'])
    when 'cmd-list'
      socket.puts @servers.process_manager.processes.map { |p|
        {
          label: p.label,
          cmd: p.command,
          dir: p.working_dir,
          state: p.state,
          pid: p.pid,
          status: p.status.nil? ? nil : p.status.to_i,
        }
      }.to_json
    end
  end
rescue EOFError, JSON::ParserError
ensure
  socket.close
end

#runObject



25
26
27
# File 'lib/explorer/server/ipc.rb', line 25

def run
  loop { async.handle_connection @server.accept }
end

#shutdownObject



18
19
20
21
22
23
# File 'lib/explorer/server/ipc.rb', line 18

def shutdown
  if @server
    @server.close
    File.delete @socket_path
  end
end