Module: Orion6Rep::Communication

Includes:
Timeout
Defined in:
lib/orion6_rep/communication.rb

Class Method Summary collapse

Class Method Details

.communicate(host_address, port, payload, expected_response_size, timeout_time = 60, max_attempts = 3) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/orion6_rep/communication.rb', line 29

def communicate(host_address, port, payload, expected_response_size, timeout_time=60, max_attempts = 3)
  attempt = 0

  while attempt < max_attempts do
    socket = nil
    begin
      timeout(timeout_time) {
        socket = TCPSocket.open(host_address, port)
      }
    rescue Timeout::Error => e
      socket = nil
    end

    received_data = nil

    if socket
      begin
        received_data = send_receive_data(socket, payload, expected_response_size, timeout_time)
      rescue Timeout::Error => e
        socket.close
      end
    end

    break unless received_data.nil?
    attempt += 1
  end

  raise "Timeout error" if attempt >= max_attempts
  received_data
end