Debian Codegraph @

ruby/lib/core/event.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18
19 module Qpid::Proton
20
21 # @deprecated Only used with the deprecated {Handler::MessagingHandler} API.
22 class Event
23 private
24 include Util::Deprecation
25
26 PROTON_METHOD_PREFIX = "pn_disposition"
27 include Util::Wrapper
28
29 EVENT_TYPE_NAMES = [:PN_EVENT_NONE,
30 :PN_CONNECTION_INIT,
31 :PN_CONNECTION_BOUND,
32 :PN_CONNECTION_UNBOUND,
33 :PN_CONNECTION_LOCAL_OPEN,
34 :PN_CONNECTION_REMOTE_OPEN,
35 :PN_CONNECTION_LOCAL_CLOSE,
36 :PN_CONNECTION_REMOTE_CLOSE,
37 :PN_CONNECTION_FINAL,
38 :PN_SESSION_INIT,
39 :PN_SESSION_LOCAL_OPEN,
40 :PN_SESSION_REMOTE_OPEN,
41 :PN_SESSION_LOCAL_CLOSE,
42 :PN_SESSION_REMOTE_CLOSE,
43 :PN_SESSION_FINAL,
44 :PN_LINK_INIT,
45 :PN_LINK_LOCAL_OPEN,
46 :PN_LINK_REMOTE_OPEN,
47 :PN_LINK_LOCAL_CLOSE,
48 :PN_LINK_REMOTE_CLOSE,
49 :PN_LINK_LOCAL_DETACH,
50 :PN_LINK_REMOTE_DETACH,
51 :PN_LINK_FLOW,
52 :PN_LINK_FINAL,
53 :PN_DELIVERY,
54 :PN_TRANSPORT,
55 :PN_TRANSPORT_AUTHENTICATED,
56 :PN_TRANSPORT_ERROR,
57 :PN_TRANSPORT_HEAD_CLOSED,
58 :PN_TRANSPORT_TAIL_CLOSED,
59 :PN_TRANSPORT_CLOSED]
60
61 TYPE_METHODS = EVENT_TYPE_NAMES.each_with_object({}) do |n, h|
62 type = Cproton.const_get(n)
63 h[type] = "on_#{Cproton.pn_event_type_name(type)[3..-1]}".downcase.to_sym
64 end
65
66 # Use Event.new(impl) to wrap a C event, or Event.new(nil, method, context)
67 # to create a pure-ruby event.
68 def initialize(impl, method=nil, context=nil)
69 @impl, @method, @context = impl, method, context
70 @method ||= TYPE_METHODS[Cproton.pn_event_type(@impl)] if @impl
71 end
72
73 # Get the context if it is_a?(clazz), else call method on the context
74 def get(clazz, method=nil)
75 (ctx = context).is_a?(clazz) ? ctx : ctx.__send__(method) rescue nil
76 end
77
78 def _context
79 x = Cproton.pn_event_context(@impl)
80 case Cproton.pn_class_id(Cproton.pn_event_class(@impl))
81 when Cproton::CID_pn_transport then Transport.wrap(Cproton.pn_cast_pn_transport(x))
82 when Cproton::CID_pn_connection then Connection.wrap(Cproton.pn_cast_pn_connection(x))
83 when Cproton::CID_pn_session then Session.wrap(Cproton.pn_cast_pn_session(x))
84 when Cproton::CID_pn_link then Link.wrap(Cproton.pn_cast_pn_link(x))
85 when Cproton::CID_pn_delivery then Delivery.wrap(Cproton.pn_cast_pn_delivery(x))
86 end
87 end
88
89 public
90
91 # Call handler.{#method}(self) if handler.respond_to? {#method}
92 # @return [Boolean] true if handler responded to the method, nil if not.
93 def dispatch(handler)
94 (handler.__send__(@method, self); true) if handler.respond_to? @method
95 end
96
97 # @return [Symbol] method name that this event will call in {#dispatch}
98 attr_accessor :method
99
100 alias type method
101
102 # @return [Object] the event context object
103 def context; return @context ||= _context; end
104
105 # @return [Container, nil] container for this event
106 def container() @container ||= get(Container, :container); end
107
108 # @return [Transport, nil] transport for this event
109 def transport() @transport ||= get(Transport, :transport); end
110
111 # @return [Connection, nil] the connection for this event
112 def connection() @connection ||= get(Connection, :connection); end
113
114 # @return [Session, nil] session for this event
115 def session() @session ||= get(Session, :session); end
116
117 # @return [Link, nil] link for this event
118 def link() @link ||= get(Link, :link); end
119
120 # @return [Sender, nil] sender associated with this event
121 def sender() link if link && link.sender?; end
122
123 # @return [Receiver, nil] receiver associated with this event
124 def receiver() link if link && link.receiver?; end
125
126 # @return [Delivery, nil] delivery for this event
127 def delivery()
128 @delivery ||= case context
129 when Delivery then @delivery = @context
130 # deprecated: for backwards compat allow a Tracker to be treated as a Delivery
131 when Tracker then @delivery = Delivery.new(context.impl)
132 end
133 end
134
135 # @return [Tracker, nil] delivery for this event
136 def tracker() @tracker ||= get(Tracker); end
137
138 # @return [Message, nil] message for this event
139 def message() @message ||= delivery.message if delivery; end
140
141 def to_s() "#{self.class}(#{method}, #{context})"; end
142 def inspect() "#{self.class}(#{method.inspect}, #{context.inspect})"; end
143
144 # @return [Condition] Error condition associated with this event or nil if none.
145 def condition
146 (context.remote_condition if context.respond_to? :remote_condition) ||
147 (context.condition if context.respond_to? :condition)
148 end
149
150 # @deprecated use {#container}
151 deprecated_alias :reactor, :container
152
153 # @private
154 Event = self
155 end
156 end