Class: ISO8583::MKB::ISO8583Connection

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/iso8583-mkb/iso8583_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateway) ⇒ ISO8583Connection

Returns a new instance of ISO8583Connection.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/iso8583-mkb/iso8583_connection.rb', line 6

def initialize(gateway)
  super

  @gateway = gateway
  @in_air = {}
  @buffer = ""
  @state = :connecting
  @post_sign_on = []
  @ping_timer = nil
  @open = nil
  @closed = nil
end

Instance Attribute Details

#closedObject

Returns the value of attribute closed.



4
5
6
# File 'lib/iso8583-mkb/iso8583_connection.rb', line 4

def closed
  @closed
end

#gatewayObject (readonly)

Returns the value of attribute gateway.



3
4
5
# File 'lib/iso8583-mkb/iso8583_connection.rb', line 3

def gateway
  @gateway
end

#openObject

Returns the value of attribute open.



4
5
6
# File 'lib/iso8583-mkb/iso8583_connection.rb', line 4

def open
  @open
end

Instance Method Details

#post_initObject



19
20
21
# File 'lib/iso8583-mkb/iso8583_connection.rb', line 19

def post_init
  Logging.logger.debug "Connected to DHI"
end

#receive_data(data) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/iso8583-mkb/iso8583_connection.rb', line 66

def receive_data(data)
  @buffer << data

  until @buffer.empty?
    marker = @buffer.index "\xFF"

    if marker.nil?
      @buffer.clear
      break
    end

    if marker != 0
      @buffer.slice! 0, marker
    end

    break if @buffer.length < 6

    data_length = @buffer[1..5].to_i

    break if @buffer.length < 6 + data_length

    @buffer.slice! 0, 6
    msg = @buffer.slice! 0, data_length
    begin
      receive_message MKBMessage.parse(msg)
    rescue => e
      Logging.logger.error "receive_message failed: #{e}"
      e.backtrace.each { |line| Logging.logger.error line }
    end
  end
end

#request(request) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/iso8583-mkb/iso8583_connection.rb', line 122

def request(request)
  if @state == :connecting
    @post_sign_on << request
  else

    id = request.request.reference.to_i

    Logging.logger.debug "Request #{id}: started"

    raise "duplicate request #{id}" if @in_air.include? id

    if request.timeout.nil?
      timer = nil
    else
      timer = EventMachine.add_timer(request.timeout) do
        @in_air[id][1] = nil
        forget_request id
        request.complete nil
      end
    end

    @in_air[id] = [ request, timer ]

    send_message request.request
  end
end

#sign_inObject



23
24
25
26
27
28
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
59
60
61
62
63
64
# File 'lib/iso8583-mkb/iso8583_connection.rb', line 23

def 

  transaction = @gateway.new_transaction
  request = transaction.request do |m|
    m.mti = "Network Management Request"
    m["Network Management Information Code"] = 1
  end

  @state = :ready

  request.start(30) do |response|

    transaction.complete

    if response.nil?
      Logging.logger.error "No response to Sign On"

      close_connection_after_writing
    else
      if Errors.success? response.response_code

        @state = :ready

        @post_sign_on.each do |request|
          self.request request
        end

        @post_sign_on.clear

        send_ping

        @open.call
      else
        Logging.logger.error "Sign On failed: #{Errors.error response.response_code}"

        close_connection_after_writing
      end
    end
  end

  @state = :connecting
end

#unbindObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/iso8583-mkb/iso8583_connection.rb', line 98

def unbind
  Logging.logger.debug "Disconnected from DHI"

  @state = :disconnecting

  @closed.call

  EventMachine.cancel_timer @ping_timer unless @ping_timer.nil?

  @in_air.each do |key, (request, timer)|
    Logging.logger.debug "Request #{request.request.reference} dropped"

    EventMachine.cancel_timer timer unless timer.nil?
    request.complete nil
  end

  @post_sign_on.each do |request|
    @gateway.post_request request
  end

  @in_air.clear
  @post_sign_on.clear
end