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, options = {}) ⇒ IPC

Returns a new instance of IPC.



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

def initialize(socket_path, options={})
  @socket_path = socket_path
  @server = UNIXServer.new(socket_path)
  @hostmap = options.fetch(:hostmap) { Explorer.hostmap }
  @log_watcher = options.fetch(:log_watcher) { Explorer.log_watcher }
  @process_manager = options.fetch(:process_manager) { Explorer.process_manager }
  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



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
66
67
68
69
70
71
72
73
# File 'lib/explorer/server/ipc.rb', line 31

def handle_connection(socket)
  loop do
    json = JSON.parse socket.readline
    case json['command']
    when 'map-list'
      socket.puts @hostmap.mappings.to_json
    when 'map-add'
      @hostmap.add json['map'], json['host'], json['port'].to_i
      @hostmap.save File.join(Explorer::CONFIGDIR, 'hostmap.yaml') # TODO: Refactor filename
    when 'map-remove'
      @hostmap.remove json['map']
      @hostmap.save File.join(Explorer::CONFIGDIR, 'hostmap.yaml')
    when 'cmd-tail'
      @log_watcher.add(socket)
    when 'cmd-add'
      @process_manager.add(json['label'], json['cmd'], working_dir: json['dir'] || ENV['PWD'])
      @process_manager.save File.join(Explorer::CONFIGDIR, 'process.yaml')
    when 'cmd-start'
      @process_manager.start(json['label'])
      @process_manager.save File.join(Explorer::CONFIGDIR, 'process.yaml')
    when 'cmd-stop'
      @process_manager.stop(json['label'])
      @process_manager.save File.join(Explorer::CONFIGDIR, 'process.yaml')
    when 'cmd-remove'
      @process_manager.remove(json['label'])
      @process_manager.save File.join(Explorer::CONFIGDIR, 'process.yaml')
    when 'cmd-list'
      socket.puts @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



27
28
29
# File 'lib/explorer/server/ipc.rb', line 27

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

#shutdownObject



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

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