Class: ISO8583::MKB::SynchronousGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/iso8583-mkb/synchronous_gateway.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SynchronousGateway

Returns a new instance of SynchronousGateway.



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

def initialize(config)
  if EventMachine.reactor_running?
    @loop_is_mine = false
    @gateway = Gateway.new config
    @gateway.run
  else
    @loop_is_mine = true
    @ready = Queue.new
    @thread = Thread.new(config, &method(:thread))
    e = @ready.pop
    @ready = nil

    raise e unless e.nil?
  end
end

Instance Method Details

#execute(request) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/iso8583-mkb/synchronous_gateway.rb', line 31

def execute(request)
  queue = Queue.new

  EventMachine.schedule do
    request.submit { queue.push nil }
  end

  queue.pop
end

#stopObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/iso8583-mkb/synchronous_gateway.rb', line 19

def stop
  EventMachine.schedule do
    @gateway.stop
    EventMachine.stop_event_loop if @loop_is_mine
  end

  if @loop_is_mine
    @thread.join
    @thread = nil
  end
end

#transaction(&block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/iso8583-mkb/synchronous_gateway.rb', line 41

def transaction(&block)
  queue = Queue.new

  EventMachine.schedule { queue.push @gateway.new_transaction }

  transaction = queue.pop

  begin
    yield transaction
  ensure
    EventMachine.schedule { transaction.complete }
  end
end