Class: Qpid::Proton::Handler::MessagingAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/handler/messaging_adapter.rb

Overview

Adapt raw proton events to MessagingHandler events.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adapter

adapt, adapter, #forward, #initialize, #proton_adapter_class

Constructor Details

This class inherits a constructor from Qpid::Proton::Handler::Adapter

Class Method Details

.open_close(endpoint)

Define repetative on_xxx_open/close methods for session and connection



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/handler/messaging_adapter.rb', line 42

def self.open_close(endpoint)
  Module.new do
    define_method(:"on_#{endpoint}_remote_open") do |event|
      begin
        delegate(:"on_#{endpoint}_open", event.context)
        event.context.open if event.context.local_uninit?
      rescue StopAutoResponse
      end
    end

    define_method(:"on_#{endpoint}_remote_close") do |event|
      delegate_error(:"on_#{endpoint}_error", event.context) if event.context.condition
      begin
        delegate(:"on_#{endpoint}_close", event.context)
        event.context.close if event.context.local_active?
      rescue StopAutoResponse
      end
    end
  end
end

Instance Method Details

#add_credit(event)



140
141
142
143
144
145
# File 'lib/handler/messaging_adapter.rb', line 140

def add_credit(event)
  return unless (r = event.receiver)
  if r.open? && (r.drained == 0) && r.credit_window && (r.credit_window > r.credit)
    r.flow(r.credit_window - r.credit)
  end
end

#delegate(method, *args)



26
27
28
# File 'lib/handler/messaging_adapter.rb', line 26

def delegate(method, *args)
  forward(method, *args) or forward(:on_unhandled, method, *args)
end

#delegate_error(method, context)



30
31
32
33
34
35
36
# File 'lib/handler/messaging_adapter.rb', line 30

def delegate_error(method, context)
  unless forward(method, context) || forward(:on_error, context.condition)
    forward(:on_unhandled, method, context)
    # Close the whole connection on an un-handled error
    context.connection.close(context.condition)
  end
end

#on_container_start(container)



38
# File 'lib/handler/messaging_adapter.rb', line 38

def on_container_start(container) delegate(:on_container_start, container); end

#on_container_stop(container)



39
# File 'lib/handler/messaging_adapter.rb', line 39

def on_container_stop(container) delegate(:on_container_stop, container); end

#on_delivery(event)



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/handler/messaging_adapter.rb', line 98

def on_delivery(event)
  if event.link.receiver?       # Incoming message
    d = event.delivery
    if d.aborted?
      delegate(:on_delivery_abort, d)
    elsif d.complete?
      if d.link.local_closed? && d.receiver.auto_accept
        d.release         # Auto release after close
      else
        begin
          delegate(:on_message, d, d.message)
          d.accept if d.receiver.auto_accept && d.local_state == 0
        rescue Reject
          d.reject
        rescue Release
          d.release
        end
      end
    end
    delegate(:on_delivery_settle, d) if d.settled?
    add_credit(event)
  else                      # Outgoing message
    t = event.tracker
    case t.state
    when Delivery::ACCEPTED then delegate(:on_tracker_accept, t)
    when Delivery::REJECTED then delegate(:on_tracker_reject, t)
    when Delivery::RELEASED then delegate(:on_tracker_release, t)
    when Delivery::MODIFIED then delegate(:on_tracker_modify, t)
    end
    if t.settled?
      delegate(:on_tracker_settle, t)
      t.settle if t.sender.auto_settle
    end
  end
end


134
135
136
137
138
# File 'lib/handler/messaging_adapter.rb', line 134

def on_link_flow(event)
  add_credit(event)
  sender = event.sender
  delegate(:on_sendable, sender) if sender && sender.open? && sender.credit > 0
end

Add flow control for local link open



96
# File 'lib/handler/messaging_adapter.rb', line 96

def on_link_local_open(event) add_credit(event); end


79
80
81
82
83
84
85
# File 'lib/handler/messaging_adapter.rb', line 79

def on_link_remote_close(event)
  s = event.link.sender?
  delegate_error(s ? :on_sender_error : :on_receiver_error, event.link) if event.link.condition
  delegate(s ? :on_sender_close : :on_receiver_close, event.link)
  event.link.close if event.link.local_active?
rescue StopAutoResponse
end

Link open/close is handled separately because links are split into sender and receiver on the messaging API



68
69
70
71
72
73
74
75
76
77
# File 'lib/handler/messaging_adapter.rb', line 68

def on_link_remote_open(event)
  if !event.link.local_active? # Copy remote terminus data to local
    event.link.source.replace(event.link.remote_source);
    event.link.target.replace(event.link.remote_target);
  end
  delegate(event.link.sender? ? :on_sender_open : :on_receiver_open, event.link)
  event.link.open if event.link.local_uninit?
  add_credit(event)
rescue StopAutoResponse
end

#on_transport_closed(event)



91
92
93
# File 'lib/handler/messaging_adapter.rb', line 91

def on_transport_closed(event)
  delegate(:on_transport_close, event.context) rescue StopAutoResponse
end

#on_transport_error(event)



87
88
89
# File 'lib/handler/messaging_adapter.rb', line 87

def on_transport_error(event)
  delegate_error(:on_transport_error, event.context)
end