Class: Explorer::Server::IPC
- Inherits:
-
Object
- Object
- Explorer::Server::IPC
- Includes:
- Celluloid::IO
- Defined in:
- lib/explorer/server/ipc.rb
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#socket_path ⇒ Object
readonly
Returns the value of attribute socket_path.
Instance Method Summary collapse
- #handle_connection(socket) ⇒ Object
-
#initialize(socket_path, options = {}) ⇒ IPC
constructor
A new instance of IPC.
- #run ⇒ Object
- #shutdown ⇒ Object
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, ={}) @socket_path = socket_path @server = UNIXServer.new(socket_path) @hostmap = .fetch(:hostmap) { Explorer.hostmap } @log_watcher = .fetch(:log_watcher) { Explorer.log_watcher } @process_manager = .fetch(:process_manager) { Explorer.process_manager } async.run end |
Instance Attribute Details
#server ⇒ Object (readonly)
Returns the value of attribute server.
9 10 11 |
# File 'lib/explorer/server/ipc.rb', line 9 def server @server end |
#socket_path ⇒ Object (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 |
#run ⇒ Object
27 28 29 |
# File 'lib/explorer/server/ipc.rb', line 27 def run loop { async.handle_connection @server.accept } end |
#shutdown ⇒ Object
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 |