Debian Codegraph @

ruby/lib/core/connection.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 # An AMQP connection.
22 class Connection < Endpoint
23 PROTON_METHOD_PREFIX = "pn_connection"
24 include Util::Wrapper
25 include Util::Deprecation
26
27 # @private
28 def self.wrap(impl)
29 return nil if impl.nil?
30 self.fetch_instance(impl, :pn_connection_attachments) || Connection.new(impl)
31 end
32
33 # @private
34 def initialize(impl = Cproton.pn_connection)
35 super()
36 @impl = impl
37 @overrides = nil
38 @session_policy = nil
39 @link_count = 0
40 @link_prefix = ""
41 self.class.store_instance(self, :pn_connection_attachments)
42 end
43
44 # @return [String] The AMQP hostname for the connection.
45 def virtual_host() Cproton.pn_connection_remote_hostname(@impl); end
46 deprecated_alias :remote_hostname, :virtual_host
47
48 # @!attribute hostname
49 # @deprecated use {#virtual_host}
50 proton_set_get :hostname
51
52 # @return [String] User name used for authentication (outgoing connection)
53 # or the authenticated user name (incoming connection)
54 def user()
55 Cproton.pn_connection_get_user(impl) or (connection.transport && connection.transport.user)
56 end
57
58 # @deprecated no replacement
59 def overrides?() deprecated __method__; false; end
60
61 # @deprecated no replacement
62 def session_policy?() deprecated __method__; false; end
63
64 # @return [Connection] self
65 def connection() self; end
66
67 # @return [Transport, nil] transport bound to this connection, or nil if unbound.
68 def transport() Transport.wrap(Cproton.pn_connection_transport(@impl)); end
69
70 # @return AMQP container ID advertised by the remote peer.
71 # To get the local container ID use {#container} and {Container#id}
72 def container_id() Cproton.pn_connection_remote_container(@impl); end
73 deprecated_alias :remote_container, :container_id
74
75 # @return [Container] the container managing this connection
76 attr_reader :container
77
78 # @return [Array<Symbol>] offered capabilities provided by the remote peer
79 def offered_capabilities
80 # Provide capabilities consistently as an array, even if encoded as a single symbol
81 Codec::Data.to_multiple(Cproton.pn_connection_remote_offered_capabilities(@impl))
82 end
83 deprecated_alias :remote_offered_capabilities, :offered_capabilities
84
85 # @return [Array<Symbol>] desired capabilities provided by the remote peer
86 def desired_capabilities
87 # Provide capabilities consistently as an array, even if encoded as a single symbol
88 Codec::Data.to_multiple(Cproton.pn_connection_remote_desired_capabilities(@impl))
89 end
90 deprecated_alias :remote_desired_capabilities, :desired_capabilities
91
92 # @return [Hash] connection-properties provided by the remote peer
93 def properties
94 Codec::Data.to_object(Cproton.pn_connection_remote_properties(@impl))
95 end
96 deprecated_alias :remote_properties, :properties
97
98 # Open the local end of the connection.
99 #
100 # @option opts [MessagingHandler] :handler handler for events related to this connection.
101 #
102 # @option opts [String] :user User name for authentication
103 # @option opts [String] :password Authentication secret
104 # @option opts [String] :virtual_host Virtual host name
105 # @option opts [String] :container_id (provided by {Container}) override advertised container-id
106 #
107 # @option opts [Hash<Symbol=>Object>] :properties Application-defined properties
108 # @option opts [Array<Symbol>] :offered_capabilities Extensions the endpoint supports
109 # @option opts [Array<Symbol>] :desired_capabilities Extensions the endpoint can use
110 #
111 # @option opts [Numeric] :idle_timeout Seconds before closing an idle connection
112 # @option opts [Integer] :max_sessions Limit the number of active sessions
113 # @option opts [Integer] :max_frame_size Limit the size of AMQP frames
114 #
115 # @option opts [Boolean] :sasl_enabled (false) Enable or disable SASL.
116 # @option opts [Boolean] :sasl_allow_insecure_mechs (false) Allow mechanisms that send secrets in cleartext
117 # @option opts [String] :sasl_allowed_mechs Specify the SASL mechanisms allowed for this connection. The value is a space-separated list of mechanism names. The mechanisms allowed by default are determined by your SASL library and system configuration, with two exceptions: GSSAPI and GSS-SPNEGO are disabled by default. To enable them, you must explicitly add them using this option. Clients must set the allowed mechanisms before the the outgoing connection is attempted. Servers must set them before the listening connection is setup.
118 #
119 # @option opts [SSLDomain] :ssl_domain SSL configuration domain.
120 #
121 def open(opts=nil)
122 return if local_active?
123 apply opts if opts
124 Cproton.pn_connection_open(@impl)
125 end
126
127 # @private
128 def apply opts
129 # NOTE: Only connection options are set here.
130 # Transport options must be applied with {Transport#apply}
131 @container = opts[:container]
132 cid = opts[:container_id] || (@container && @container.id) || SecureRandom.uuid
133 cid = cid.to_s if cid.is_a? Symbol # Allow symbols as container name
134 Cproton.pn_connection_set_container(@impl, cid)
135 Cproton.pn_connection_set_user(@impl, opts[:user]) if opts[:user]
136 Cproton.pn_connection_set_password(@impl, opts[:password]) if opts[:password]
137 Cproton.pn_connection_set_hostname(@impl, opts[:virtual_host]) if opts[:virtual_host]
138 @link_prefix = opts[:link_prefix] || cid
139 Codec::Data.from_object(Cproton.pn_connection_offered_capabilities(@impl), opts[:offered_capabilities])
140 Codec::Data.from_object(Cproton.pn_connection_desired_capabilities(@impl), opts[:desired_capabilities])
141 Codec::Data.from_object(Cproton.pn_connection_properties(@impl), opts[:properties])
142 end
143
144 # Idle-timeout advertised by the remote peer, in seconds.
145 # @return [Numeric] Idle-timeout advertised by the remote peer, in seconds.
146 # @return [nil] if the peer does not advertise an idle time-out
147 def idle_timeout()
148 if transport && (t = transport.remote_idle_timeout)
149 Rational(t, 1000) # More precise than Float
150 end
151 end
152
153 # Session limit advertised by the remote peer. See {Connection#open :max_sessions}
154 # @return [Integer] maximum number of sessions per connection allowed by remote peer.
155 # @return [nil] no specific limit is set.
156 def max_sessions()
157 raise StateError, "connection not bound to transport" unless transport
158 max = transport.remote_channel_max
159 return max.zero? ? nil : max
160 end
161
162 # Maximum frame size, in bytes, advertised by the remote peer.
163 # See {Connection#open :max_frame_size}
164 # @return [Integer] maximum frame size
165 # @return [nil] no limit
166 def max_frame_size()
167 raise StateError, "connection not bound to transport" unless transport
168 max = transport.remote_max_frame
169 return max.zero? ? nil : max
170 end
171
172 # Closes the local end of the connection. The remote end may or may not be closed.
173 # @param error [Condition] Optional error condition to send with the close.
174 def close(error=nil)
175 Condition.assign(_local_condition, error)
176 Cproton.pn_connection_close(@impl)
177 end
178
179 # Gets the endpoint current state flags
180 #
181 # @see Endpoint#LOCAL_UNINIT
182 # @see Endpoint#LOCAL_ACTIVE
183 # @see Endpoint#LOCAL_CLOSED
184 # @see Endpoint#LOCAL_MASK
185 #
186 # @return [Integer] The state flags.
187 #
188 def state
189 Cproton.pn_connection_state(@impl)
190 end
191
192 # Returns the default session for this connection.
193 #
194 # @return [Session] The session.
195 #
196 def default_session
197 @session ||= open_session
198 end
199
200 # @deprecated use #default_session()
201 deprecated_alias :session, :default_session
202
203 # Open a new session on this connection.
204 def open_session
205 s = Session.wrap(Cproton.pn_session(@impl))
206 s.open
207 return s
208 end
209
210 # Open a sender on the default_session
211 # @option opts (see Session#open_sender)
212 def open_sender(opts=nil) default_session.open_sender(opts) end
213
214 # Open a on the default_session
215 # @option opts (see Session#open_receiver)
216 def open_receiver(opts=nil) default_session.open_receiver(opts) end
217
218 # @deprecated use {#each_session}
219 def session_head(mask)
220 deprecated __method__, "#each_session"
221 Session.wrap(Cproton.pn_session_head(@impl, mask))
222 end
223
224 # Get the sessions on this connection.
225 # @overload each_session
226 # @yieldparam s [Session] pass each session to block
227 # @overload each_session
228 # @return [Enumerator] enumerator over sessions
229 def each_session(&block)
230 return enum_for(:each_session) unless block_given?
231 s = Cproton.pn_session_head(@impl, 0);
232 while s
233 yield Session.wrap(s)
234 s = Cproton.pn_session_next(s, 0)
235 end
236 self
237 end
238
239 # @deprecated use {#each_link}
240 def link_head(mask)
241 deprecated __method__, "#each_link"
242 Link.wrap(Cproton.pn_link_head(@impl, mask))
243 end
244
245 # Get the links on this connection.
246 # @overload each_link
247 # @yieldparam l [Link] pass each link to block
248 # @overload each_link
249 # @return [Enumerator] enumerator over links
250 def each_link
251 return enum_for(:each_link) unless block_given?
252 l = Cproton.pn_link_head(@impl, 0);
253 while l
254 l2 = l # get next before yield, in case yield closes l and unlinks it
255 l = Cproton.pn_link_next(l, 0)
256 yield Link.wrap(l2)
257 end
258 self
259 end
260
261 # Get the {Sender} links - see {#each_link}
262 def each_sender()
263 return enum_for(:each_sender) unless block_given?
264 each_link.select { |l| yield l if l.sender? }
265 end
266
267 # Get the {Receiver} links - see {#each_link}
268 def each_receiver()
269 return enum_for(:each_receiver) unless block_given?
270 each_link.select { |l| yield l if l.receiver? }
271 end
272
273 # @deprecated use {#MessagingHandler} to handle work
274 def work_head
275 deprecated __method__
276 Delivery.wrap(Cproton.pn_work_head(@impl))
277 end
278
279 # @deprecated use {#condition}
280 def error
281 deprecated __method__, "#condition"
282 Cproton.pn_error_code(Cproton.pn_connection_error(@impl))
283 end
284
285 # @private Generate a unique link name, internal use only.
286 def link_name()
287 @link_prefix + "/" + (@link_count += 1).to_s(32)
288 end
289
290 # @return [WorkQueue] work queue to execute code serialized correctly for this connection
291 attr_reader :work_queue
292
293 protected
294
295 def _local_condition
296 Cproton.pn_connection_condition(@impl)
297 end
298
299 def _remote_condition
300 Cproton.pn_connection_remote_condition(@impl)
301 end
302
303 proton_get :attachments
304 end
305 end