class Explorer::Server::IPC

Attributes

server[R]
socket_path[R]

Public Class Methods

new(socket_path, options={}) click to toggle source
# 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

Public Instance Methods

handle_connection(socket) click to toggle source
# 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
run() click to toggle source
# File lib/explorer/server/ipc.rb, line 27
def run
  loop { async.handle_connection @server.accept }
end
shutdown() click to toggle source
# File lib/explorer/server/ipc.rb, line 20
def shutdown
  if @server
    @server.close
    File.delete @socket_path
  end
end