class Empp::EmppConnection

Attributes

alive[R]

Public Class Methods

new(host, port, account_id, password, service_id) click to toggle source
# File lib/empp/empp_connection.rb, line 12
def initialize(host, port, account_id, password, service_id)
  @host = host
  @port = port
  @account_id = account_id
  @password = password
  @service_id = service_id
  @tcp_connection = TcpConnection.getConnection(host, port)
  @logger = EmppLogger.instance
end

Public Instance Methods

alive?() click to toggle source
# File lib/empp/empp_connection.rb, line 22
def alive?
  @alive
end
close() click to toggle source
# File lib/empp/empp_connection.rb, line 26
def close

  if @tcp_connection
    @tcp_connection.close
  end

  @alive = false
  
end
connect() click to toggle source
# File lib/empp/empp_connection.rb, line 36
def connect
  @logger.debug("Enter EmppConnection::connect")

  @tcp_connection.connect
  
  msgConn = MsgConnect.new(@account_id, @password)
  connReq = msgConn.package

  @tcp_connection.send(connReq)

  object = receive()
  if object
    @alive = true if object.status == Constants::EMPP_CONNECT_OK
  end

  @logger.debug("Leave EmppConnection::connect with status=#{@alive}")
  @alive
end
receive() click to toggle source
# File lib/empp/empp_connection.rb, line 55
def receive
  @logger.debug("Enter EmppConnection::receive")
  # read header
  header = @tcp_connection.receive(12)
  body = ''
  object = nil

  if header
    object = EmppParser.parseHeader(header)

    if object.total_length - 12 > 0
      body = @tcp_connection.receive(object.total_length - 12)
      EmppParser.parseBody(object, body)
    end
      @logger.debug( "EmppConnection::receive bytes:" + (header + body).unpack("H*").to_s )
      @logger.debug("EmppConnection::receive object=#{object}")
      # @logger.info("EmppConnection::receive object:" + object.to_s)
  end

  @logger.debug("Leave EmppConnection::receive")
  object
end
send(emppObject) click to toggle source
# File lib/empp/empp_connection.rb, line 78
def send(emppObject)
  @logger.debug("Enter EmppConnection::send")

  @logger.info("EmppConnection::send object=#{emppObject}")
  bytes = @tcp_connection.send(emppObject.package)

  @logger.debug("Leave EmppConnection::send")
  bytes
end