Class: Cognizant::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cognizant/client.rb

Defined Under Namespace

Modules: Transport

Constant Summary collapse

@@responseless_commands =
["shutdown"]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Client

Returns a new instance of Client


44
45
46
# File 'lib/cognizant/client.rb', line 44

def initialize(socket)
  @socket = socket
end

Class Method Details

.for_path(path_to_socket) ⇒ Object


39
40
41
42
# File 'lib/cognizant/client.rb', line 39

def self.for_path(path_to_socket)
  socket = UNIXSocket.open(path_to_socket)
  self.new(socket)
end

.for_port(hostname = "127.0.0.1", port) ⇒ Object


34
35
36
37
# File 'lib/cognizant/client.rb', line 34

def self.for_port(hostname = "127.0.0.1", port)
  socket = TCPSocket.open(hostname, port)
  self.new(socket)
end

Instance Method Details

#closeObject


57
58
59
# File 'lib/cognizant/client.rb', line 57

def close
  @socket.close
end

#command(command_hash) ⇒ Object


48
49
50
51
# File 'lib/cognizant/client.rb', line 48

def command(command_hash)
  Transport.send_message(@socket, command_hash)
  Transport.receive_message(@socket) if expect_response?(command_hash)
end

#expect_response?(command_hash) ⇒ Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/cognizant/client.rb', line 53

def expect_response?(command_hash)
  !@@responseless_commands.include?(command_hash['command'])
end