Sha256: a2454b9237d9d0faa08847f3d2e93c6b593a1f9180dac7997db9bfeb9111338d

Contents?: true

Size: 1.83 KB

Versions: 14

Compression:

Stored size: 1.83 KB

Contents

module Invoker
  module IPC
    class UnixClient
      def send_command(command, message = {})
        message_object = get_message_object(command, message)
        open_client_socket do |socket|
          send_json_message(socket, message_object)
          socket.flush
          if block_given?
            response_object = Invoker::IPC.message_from_io(socket)
            yield response_object
          end
        end
      end

      def send_and_receive(command, message = {})
        response = nil
        message_object = get_message_object(command, message)
        open_client_socket(false) do |socket|
          send_json_message(socket, message_object)
          socket.flush
          response = Invoker::IPC.message_from_io(socket)
        end
        response
      end

      def send_and_wait(command, message = {})
        begin
          socket = Socket.unix(Invoker::IPC::Server::SOCKET_PATH)
        rescue
          abort("Invoker does not seem to be running".color(:red))
        end
        message_object = get_message_object(command, message)
        send_json_message(socket, message_object)
        socket.flush
        socket
      end

      def self.send_command(command, message_arguments = {}, &block)
        new.send_command(command, message_arguments, &block)
      end

      private

      def get_message_object(command, message_arguments)
        Invoker::IPC::Message.const_get(Invoker::IPC.camelize(command)).new(message_arguments)
      end

      def open_client_socket(abort_if_not_running = true)
        Socket.unix(Invoker::IPC::Server::SOCKET_PATH) { |socket| yield socket }
      rescue
        abort_if_not_running && abort("Invoker does not seem to be running".color(:red))
      end

      def send_json_message(socket, message_object)
        socket.write(message_object.encoded_message)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
invoker-1.5.4 lib/invoker/ipc/unix_client.rb
invoker-1.5.3 lib/invoker/ipc/unix_client.rb
meepo-1.5.2 lib/invoker/ipc/unix_client.rb
invoker-1.5.1 lib/invoker/ipc/unix_client.rb
invoker-1.4.1 lib/invoker/ipc/unix_client.rb
invoker-1.4.0 lib/invoker/ipc/unix_client.rb
invoker-1.3.2 lib/invoker/ipc/unix_client.rb
invoker-1.3.1 lib/invoker/ipc/unix_client.rb
invoker-1.3.0 lib/invoker/ipc/unix_client.rb
invoker-1.2.0 lib/invoker/ipc/unix_client.rb
invoker-1.2.0.pre2 lib/invoker/ipc/unix_client.rb
invoker-1.2.0.pre1 lib/invoker/ipc/unix_client.rb
invoker-1.2.0.pre lib/invoker/ipc/unix_client.rb
invoker-1.1.0 lib/invoker/ipc/unix_client.rb