Class: RubyKpi::TCPConnection

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

Instance Method Summary collapse

Constructor Details

#initialize(ip, port) ⇒ TCPConnection

Constructor



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/TCPConnection.rb', line 12

def initialize(ip, port)

  # Read parameters
  @ip = ip
  @port = port

  # Connect
  begin
    @sib_socket = TCPSocket.new(@ip, @port)
  rescue Errno::ECONNREFUSED
    raise SIBError, 'Connection refused'
  end
end

Instance Method Details

#closeObject

Close the connection



50
51
52
# File 'lib/TCPConnection.rb', line 50

def close()
  @sib_socket.close()
end

#receive_replyObject

Receive the reply



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/TCPConnection.rb', line 32

def receive_reply()
  rmsg = ""
  while true do
    begin
      r = @sib_socket.recv(4096)
      rmsg += r
      if rmsg.include?("</SSAP_message>")
        break
      end
    rescue
      raise SIBError, 'Error while receiving a reply'
    end
  end

  return rmsg
end

#send_request(msg) ⇒ Object

Send the request message



27
28
29
# File 'lib/TCPConnection.rb', line 27

def send_request(msg)  
  @sib_socket.write(msg)
end