Class: Empp::EmppConnection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, account_id, password, service_id) ⇒ EmppConnection

Returns a new instance of EmppConnection.



12
13
14
15
16
17
18
19
20
# File 'lib/empp/empp_connection.rb', line 12

def initialize(host, port, , password, service_id)
  @host = host
  @port = port
  @account_id = 
  @password = password
  @service_id = service_id
  @tcp_connection = TcpConnection.getConnection(host, port)
  @logger = EmppLogger.instance
end

Instance Attribute Details

#aliveObject (readonly)

Returns the value of attribute alive.



10
11
12
# File 'lib/empp/empp_connection.rb', line 10

def alive
  @alive
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/empp/empp_connection.rb', line 22

def alive?
  @alive
end

#closeObject



26
27
28
29
30
31
32
33
34
# File 'lib/empp/empp_connection.rb', line 26

def close

  if @tcp_connection
    @tcp_connection.close
  end

  @alive = false
  
end

#connectObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

#receiveObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



78
79
80
81
82
83
84
85
86
# 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