Class: Daemon::Utils::CommandsServer

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EventMachine::Protocols::LineProtocol
Defined in:
lib/onapp/engine/utils/commands_server.rb,
lib/onapp/engine/utils/commands_server/commands.rb,
lib/onapp/engine/utils/commands_server/unix_server_client.rb

Defined Under Namespace

Classes: Commands, UNIXServerClient

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CommandsServer) initialize(commands)

Returns a new instance of CommandsServer



21
22
23
# File 'lib/onapp/engine/utils/commands_server.rb', line 21

def initialize(commands)
  @commands = commands
end

Instance Attribute Details

- (Object) commands (readonly)

Returns the value of attribute commands



13
14
15
# File 'lib/onapp/engine/utils/commands_server.rb', line 13

def commands
  @commands
end

Class Method Details

+ (Object) start!(server, port = nil) {|commands| ... }

Yields:

  • (commands)


15
16
17
18
19
# File 'lib/onapp/engine/utils/commands_server.rb', line 15

def self.start!(server, port = nil, &block)
  commands = Commands.new
  yield commands
  ::EM.start_server(server, port, self, commands)
end

Instance Method Details

- (Object) receive_line(line)



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/onapp/engine/utils/commands_server.rb', line 25

def receive_line(line)
  response =
    begin
      if commands.exists?(line)
        {response: commands.call(line) }
      else
        {error: "Unknown command: #{line.strip}"}
      end
    rescue => ex
      {error: "#{ex.class}: #{ex.message}\n#{ex.backtrace.join("\n")}"}
    end
  send_data(response.to_json.gsub(/\r?\n/, "") + "\n")
end