Class: CodinRep::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/codin_rep/command.rb

Direct Known Subclasses

EmployeeCommand, GetRecords, GetTime, SetTime

Defined Under Namespace

Classes: MalformedResponsePayload, UnknownHeader

Constant Summary collapse

COMMAND_PREFIX =
"PGREP".freeze

Instance Method Summary collapse

Constructor Details

#initialize(host_address, tcp_port) ⇒ Command

Returns a new instance of Command.



50
51
52
53
54
# File 'lib/codin_rep/command.rb', line 50

def initialize(host_address, tcp_port)
  @host_address = host_address
  @tcp_port = tcp_port
  @communication = Communication.new(@host_address, @tcp_port, get_timeout_time, get_max_attempts)
end

Instance Method Details

#check_response_headerObject



87
88
89
# File 'lib/codin_rep/command.rb', line 87

def check_response_header
  raise_not_implemented_error
end

#executeObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/codin_rep/command.rb', line 56

def execute
  @command_data = generate_header
  @command_data += generate_command_payload
  @response = communicate!
  check_response_header
  @payload = get_response_payload
  return get_data_from_response_payload
rescue
  @communication.close
  raise
ensure
  @communication.close if should_close_connection?
end

#generate_command_payloadObject



83
84
85
# File 'lib/codin_rep/command.rb', line 83

def generate_command_payload
  raise_not_implemented_error
end

#generate_headerObject



78
79
80
81
# File 'lib/codin_rep/command.rb', line 78

def generate_header
  # COMMAND_CODE should be set on the specific command class:
  COMMAND_PREFIX.dup + self.class::COMMAND_CODE.dup
end

#get_data_from_response_payloadObject



91
92
93
# File 'lib/codin_rep/command.rb', line 91

def get_data_from_response_payload
  raise_not_implemented_error
end

#get_expected_response_sizeObject



95
96
97
# File 'lib/codin_rep/command.rb', line 95

def get_expected_response_size
  raise_not_implemented_error
end

#get_max_attemptsObject



74
75
76
# File 'lib/codin_rep/command.rb', line 74

def get_max_attempts
  3
end

#get_timeout_timeObject



70
71
72
# File 'lib/codin_rep/command.rb', line 70

def get_timeout_time
  3
end

#should_close_connection?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/codin_rep/command.rb', line 99

def should_close_connection?
  !!@response
end