Sha256: b09568461038a402e8292d8f2c8b108a2daf3e5ea55f7415fa1421553ca1bbb7

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

module Invoker
  module CommandListener
    class Client
      attr_accessor :client_socket
      def initialize(client_socket)
        @client_socket = client_socket
      end

      def read_and_execute
        command_info = client_socket.gets()
        command_info && command_info.strip!

        if command_info && !command_info.empty?
          worker_command, command_label, rest_args = command_info.strip.split(" ")
          worker_command.strip!
          if worker_command
            run_command(worker_command, command_label, rest_args)
          end
        end
      end

      def run_command(worker_command, command_label, rest_args = nil)
        case worker_command
        when 'add'
          Invoker::COMMANDER.on_next_tick(command_label) { |b_command_label|
            add_command_by_label(b_command_label)
          }
        when 'list'
          json = Invoker::COMMANDER.list_commands()
          client_socket.puts(json)
        when 'remove'
          Invoker::COMMANDER.on_next_tick(command_label, rest_args) { |b_command_label,b_rest_args|
            remove_command(b_command_label, b_rest_args)
          }
        when 'reload'
          Invoker::COMMANDER.on_next_tick(command_label, rest_args) { |b_command_label, b_rest_args|
            reload_command(b_command_label, b_rest_args)
          }
        else
          Invoker::Logger.puts("\n Invalid command".red)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
invoker-0.1.2 lib/invoker/command_listener/client.rb
invoker-0.1.1 lib/invoker/command_listener/client.rb
invoker-0.1.1.pre lib/invoker/command_listener/client.rb