| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
""" |
| 21 |
The proton.endpoints module |
| 22 |
""" |
| 23 |
|
| 24 |
import weakref |
| 25 |
|
| 26 |
from cproton import PN_CONFIGURATION, PN_COORDINATOR, PN_DELIVERIES, PN_DIST_MODE_COPY, PN_DIST_MODE_MOVE, \ |
| 27 |
PN_DIST_MODE_UNSPECIFIED, PN_EOS, PN_EXPIRE_NEVER, PN_EXPIRE_WITH_CONNECTION, PN_EXPIRE_WITH_LINK, \ |
| 28 |
PN_EXPIRE_WITH_SESSION, PN_LOCAL_ACTIVE, PN_LOCAL_CLOSED, PN_LOCAL_UNINIT, PN_NONDURABLE, PN_RCV_FIRST, \ |
| 29 |
PN_RCV_SECOND, PN_REMOTE_ACTIVE, PN_REMOTE_CLOSED, PN_REMOTE_UNINIT, PN_SND_MIXED, PN_SND_SETTLED, PN_SND_UNSETTLED, \ |
| 30 |
PN_SOURCE, PN_TARGET, PN_UNSPECIFIED, pn_connection, pn_connection_attachments, pn_connection_close, \ |
| 31 |
pn_connection_collect, pn_connection_condition, pn_connection_desired_capabilities, pn_connection_error, \ |
| 32 |
pn_connection_get_authorization, pn_connection_get_container, pn_connection_get_hostname, pn_connection_get_user, \ |
| 33 |
pn_connection_offered_capabilities, \ |
| 34 |
pn_connection_open, pn_connection_properties, pn_connection_release, pn_connection_remote_condition, \ |
| 35 |
pn_connection_remote_container, pn_connection_remote_desired_capabilities, pn_connection_remote_hostname, \ |
| 36 |
pn_connection_remote_offered_capabilities, pn_connection_remote_properties, \ |
| 37 |
pn_connection_set_authorization, pn_connection_set_container, \ |
| 38 |
pn_connection_set_hostname, pn_connection_set_password, pn_connection_set_user, pn_connection_state, \ |
| 39 |
pn_connection_transport, pn_delivery, pn_error_code, pn_error_text, pn_link_advance, pn_link_attachments, \ |
| 40 |
pn_link_available, pn_link_close, pn_link_condition, pn_link_credit, pn_link_current, pn_link_detach, pn_link_drain, \ |
| 41 |
pn_link_drained, pn_link_draining, pn_link_error, pn_link_flow, pn_link_free, pn_link_get_drain, pn_link_head, \ |
| 42 |
pn_link_is_receiver, pn_link_is_sender, pn_link_max_message_size, pn_link_name, pn_link_next, pn_link_offered, \ |
| 43 |
pn_link_open, pn_link_queued, pn_link_rcv_settle_mode, pn_link_recv, pn_link_remote_condition, \ |
| 44 |
pn_link_remote_max_message_size, pn_link_remote_rcv_settle_mode, pn_link_remote_snd_settle_mode, \ |
| 45 |
pn_link_remote_source, pn_link_remote_target, pn_link_send, pn_link_session, pn_link_set_drain, \ |
| 46 |
pn_link_set_max_message_size, pn_link_set_rcv_settle_mode, pn_link_set_snd_settle_mode, pn_link_snd_settle_mode, \ |
| 47 |
pn_link_source, pn_link_state, pn_link_target, pn_link_unsettled, pn_receiver, pn_sender, pn_session, \ |
| 48 |
pn_session_attachments, pn_session_close, pn_session_condition, pn_session_connection, pn_session_free, \ |
| 49 |
pn_session_get_incoming_capacity, pn_session_get_outgoing_window, pn_session_head, pn_session_incoming_bytes, \ |
| 50 |
pn_session_next, pn_session_open, pn_session_outgoing_bytes, pn_session_remote_condition, \ |
| 51 |
pn_session_set_incoming_capacity, pn_session_set_outgoing_window, pn_session_state, pn_terminus_capabilities, \ |
| 52 |
pn_terminus_copy, pn_terminus_filter, pn_terminus_get_address, pn_terminus_get_distribution_mode, \ |
| 53 |
pn_terminus_get_durability, pn_terminus_get_expiry_policy, pn_terminus_get_timeout, pn_terminus_get_type, \ |
| 54 |
pn_terminus_is_dynamic, pn_terminus_outcomes, pn_terminus_properties, pn_terminus_set_address, \ |
| 55 |
pn_terminus_set_distribution_mode, pn_terminus_set_durability, pn_terminus_set_dynamic, \ |
| 56 |
pn_terminus_set_expiry_policy, pn_terminus_set_timeout, pn_terminus_set_type, pn_work_head, \ |
| 57 |
pn_link_properties, pn_link_remote_properties |
| 58 |
|
| 59 |
from ._common import unicode2utf8, utf82unicode |
| 60 |
from ._condition import cond2obj, obj2cond |
| 61 |
from ._data import Data, dat2obj, obj2dat, PropertyDict, SymbolList |
| 62 |
from ._delivery import Delivery |
| 63 |
from ._exceptions import ConnectionException, EXCEPTIONS, LinkException, SessionException |
| 64 |
from ._transport import Transport |
| 65 |
from ._wrapper import Wrapper |
| 66 |
from typing import Callable, Dict, List, Optional, Union, TYPE_CHECKING, Any |
| 67 |
|
| 68 |
if TYPE_CHECKING: |
| 69 |
from ._condition import Condition |
| 70 |
from ._data import Array, PythonAMQPData, symbol |
| 71 |
from ._events import Collector, Handler |
| 72 |
from ._message import Message |
| 73 |
|
| 74 |
|
| 75 |
class Endpoint(object): |
| 76 |
""" |
| 77 |
Abstract class from which :class:`Connection`, :class:`Session` |
| 78 |
and :class:`Link` are derived, and which defines the state |
| 79 |
of these classes. |
| 80 |
|
| 81 |
The :class:`Endpoint` state is an integral value with flags that |
| 82 |
encode both the local and remote state of an AMQP Endpoint |
| 83 |
(:class:`Connection`, :class:`Link`, or :class:`Session`). |
| 84 |
The individual bits may be accessed using :const:`LOCAL_UNINIT`, |
| 85 |
:const:`LOCAL_ACTIVE`, :const:`LOCAL_CLOSED`, and |
| 86 |
:const:`REMOTE_UNINIT`, :const:`REMOTE_ACTIVE`, :const:`REMOTE_CLOSED`. |
| 87 |
|
| 88 |
Every AMQP endpoint (:class:`Connection`, :class:`Link`, or |
| 89 |
:class:`Session`) starts out in an uninitialized state and then |
| 90 |
proceeds linearly to an active and then closed state. This |
| 91 |
lifecycle occurs at both endpoints involved, and so the state |
| 92 |
model for an endpoint includes not only the known local state, |
| 93 |
but also the last known state of the remote endpoint. |
| 94 |
""" |
| 95 |
|
| 96 |
LOCAL_UNINIT = PN_LOCAL_UNINIT |
| 97 |
""" The local endpoint state is uninitialized. """ |
| 98 |
|
| 99 |
REMOTE_UNINIT = PN_REMOTE_UNINIT |
| 100 |
""" The local endpoint state is active. """ |
| 101 |
|
| 102 |
LOCAL_ACTIVE = PN_LOCAL_ACTIVE |
| 103 |
""" The local endpoint state is closed. """ |
| 104 |
|
| 105 |
REMOTE_ACTIVE = PN_REMOTE_ACTIVE |
| 106 |
""" The remote endpoint state is uninitialized. """ |
| 107 |
|
| 108 |
LOCAL_CLOSED = PN_LOCAL_CLOSED |
| 109 |
""" The remote endpoint state is active. """ |
| 110 |
|
| 111 |
REMOTE_CLOSED = PN_REMOTE_CLOSED |
| 112 |
""" The remote endpoint state is closed. """ |
| 113 |
|
| 114 |
def _init(self) -> None: |
| 115 |
self.condition: Optional['Condition'] = None |
| 116 |
self._handler: Optional['Handler'] = None |
| 117 |
|
| 118 |
def _update_cond(self) -> None: |
| 119 |
obj2cond(self.condition, self._get_cond_impl()) |
| 120 |
|
| 121 |
@property |
| 122 |
def remote_condition(self) -> Optional['Condition']: |
| 123 |
""" |
| 124 |
The remote condition associated with the connection endpoint. |
| 125 |
See :class:`Condition` for more information. |
| 126 |
""" |
| 127 |
return cond2obj(self._get_remote_cond_impl()) |
| 128 |
|
| 129 |
# the following must be provided by subclasses |
| 130 |
def _get_cond_impl(self): |
| 131 |
assert False, "Subclass must override this!" |
| 132 |
|
| 133 |
def _get_remote_cond_impl(self): |
| 134 |
assert False, "Subclass must override this!" |
| 135 |
|
| 136 |
@property |
| 137 |
def handler(self) -> Optional['Handler']: |
| 138 |
"""Handler for events. |
| 139 |
|
| 140 |
:getter: Get the event handler, or return ``None`` if no handler has been set. |
| 141 |
:setter: Set the event handler.""" |
| 142 |
return self._handler |
| 143 |
|
| 144 |
@handler.setter |
| 145 |
def handler(self, handler: Optional['Handler']) -> None: |
| 146 |
# TODO Hack This is here for some very odd (IMO) backwards compat behaviour |
| 147 |
from ._events import Handler |
| 148 |
if handler is None: |
| 149 |
self._handler = None |
| 150 |
elif issubclass(type(handler), Handler): |
| 151 |
self._handler = handler |
| 152 |
else: |
| 153 |
self._handler = Handler() |
| 154 |
self._handler.add(handler) |
| 155 |
|
| 156 |
|
| 157 |
class Connection(Wrapper, Endpoint): |
| 158 |
""" |
| 159 |
A representation of an AMQP connection. |
| 160 |
""" |
| 161 |
|
| 162 |
@staticmethod |
| 163 |
def wrap(impl): |
| 164 |
if impl is None: |
| 165 |
return None |
| 166 |
else: |
| 167 |
return Connection(impl) |
| 168 |
|
| 169 |
def __init__(self, impl: Callable[[], Any] = pn_connection) -> None: |
| 170 |
Wrapper.__init__(self, impl, pn_connection_attachments) |
| 171 |
|
| 172 |
def _init(self) -> None: |
| 173 |
Endpoint._init(self) |
| 174 |
self.offered_capabilities = None |
| 175 |
self.desired_capabilities = None |
| 176 |
self.properties = None |
| 177 |
self.url = None |
| 178 |
self._acceptor = None |
| 179 |
|
| 180 |
def _get_attachments(self): |
| 181 |
return pn_connection_attachments(self._impl) |
| 182 |
|
| 183 |
@property |
| 184 |
def connection(self) -> 'Connection': |
| 185 |
""" |
| 186 |
Get this connection. |
| 187 |
""" |
| 188 |
return self |
| 189 |
|
| 190 |
@property |
| 191 |
def transport(self) -> Optional[Transport]: |
| 192 |
""" |
| 193 |
The transport bound to this connection. If the connection |
| 194 |
is unbound, then this operation will return ``None``. |
| 195 |
""" |
| 196 |
return Transport.wrap(pn_connection_transport(self._impl)) |
| 197 |
|
| 198 |
def _check(self, err: int) -> int: |
| 199 |
if err < 0: |
| 200 |
exc = EXCEPTIONS.get(err, ConnectionException) |
| 201 |
raise exc("[%s]: %s" % (err, pn_connection_error(self._impl))) |
| 202 |
else: |
| 203 |
return err |
| 204 |
|
| 205 |
def _get_cond_impl(self): |
| 206 |
return pn_connection_condition(self._impl) |
| 207 |
|
| 208 |
def _get_remote_cond_impl(self): |
| 209 |
return pn_connection_remote_condition(self._impl) |
| 210 |
|
| 211 |
# TODO: Blacklisted API call |
| 212 |
def collect(self, collector: 'Collector') -> None: |
| 213 |
if collector is None: |
| 214 |
pn_connection_collect(self._impl, None) |
| 215 |
else: |
| 216 |
pn_connection_collect(self._impl, collector._impl) |
| 217 |
self._collector = weakref.ref(collector) |
| 218 |
|
| 219 |
@property |
| 220 |
def container(self) -> str: |
| 221 |
"""The container name for this connection object.""" |
| 222 |
return utf82unicode(pn_connection_get_container(self._impl)) |
| 223 |
|
| 224 |
@container.setter |
| 225 |
def container(self, name: str) -> None: |
| 226 |
pn_connection_set_container(self._impl, unicode2utf8(name)) |
| 227 |
|
| 228 |
@property |
| 229 |
def hostname(self) -> Optional[str]: |
| 230 |
"""Set the name of the host (either fully qualified or relative) to which this |
| 231 |
connection is connecting to. This information may be used by the remote |
| 232 |
peer to determine the correct back-end service to connect the client to. |
| 233 |
This value will be sent in the Open performative, and will be used by SSL |
| 234 |
and SASL layers to identify the peer. |
| 235 |
""" |
| 236 |
return utf82unicode(pn_connection_get_hostname(self._impl)) |
| 237 |
|
| 238 |
@hostname.setter |
| 239 |
def hostname(self, name: str) -> None: |
| 240 |
pn_connection_set_hostname(self._impl, unicode2utf8(name)) |
| 241 |
|
| 242 |
@property |
| 243 |
def user(self) -> Optional[str]: |
| 244 |
"""The authentication username for a client connection. |
| 245 |
|
| 246 |
It is necessary to set the username and password before binding |
| 247 |
the connection to a transport and it isn't allowed to change |
| 248 |
after the binding. |
| 249 |
|
| 250 |
If not set then no authentication will be negotiated unless the |
| 251 |
client sasl layer is explicitly created (this would be for something |
| 252 |
like Kerberos where the credentials are implicit in the environment, |
| 253 |
or to explicitly use the ``ANONYMOUS`` SASL mechanism) |
| 254 |
""" |
| 255 |
return utf82unicode(pn_connection_get_user(self._impl)) |
| 256 |
|
| 257 |
@user.setter |
| 258 |
def user(self, name: str) -> None: |
| 259 |
pn_connection_set_user(self._impl, unicode2utf8(name)) |
| 260 |
|
| 261 |
@property |
| 262 |
def authorization(self) -> str: |
| 263 |
"""The authorization username for a client connection. |
| 264 |
|
| 265 |
It is necessary to set the authorization before binding |
| 266 |
the connection to a transport and it isn't allowed to change |
| 267 |
after the binding. |
| 268 |
|
| 269 |
If not set then implicitly the requested authorization is the same as the |
| 270 |
authentication user. |
| 271 |
""" |
| 272 |
return utf82unicode(pn_connection_get_authorization(self._impl)) |
| 273 |
|
| 274 |
@authorization.setter |
| 275 |
def authorization(self, name: str) -> None: |
| 276 |
pn_connection_set_authorization(self._impl, unicode2utf8(name)) |
| 277 |
|
| 278 |
@property |
| 279 |
def password(self) -> None: |
| 280 |
"""Set the authentication password for a client connection. |
| 281 |
|
| 282 |
It is necessary to set the username and password before binding the connection |
| 283 |
to a transport and it isn't allowed to change after the binding. |
| 284 |
|
| 285 |
.. note:: Getting the password always returns ``None``. |
| 286 |
""" |
| 287 |
return None |
| 288 |
|
| 289 |
@password.setter |
| 290 |
def password(self, name: str) -> None: |
| 291 |
pn_connection_set_password(self._impl, unicode2utf8(name)) |
| 292 |
|
| 293 |
@property |
| 294 |
def remote_container(self) -> Optional[str]: |
| 295 |
""" |
| 296 |
The container identifier specified by the remote peer for this connection. |
| 297 |
|
| 298 |
This will return ``None`` until the :const:'REMOTE_ACTIVE` state is |
| 299 |
reached. See :class:`Endpoint` for more details on endpoint state. |
| 300 |
|
| 301 |
Any (non ``None``) name returned by this operation will be valid until |
| 302 |
the connection object is unbound from a transport or freed, |
| 303 |
whichever happens sooner. |
| 304 |
""" |
| 305 |
return pn_connection_remote_container(self._impl) |
| 306 |
|
| 307 |
@property |
| 308 |
def remote_hostname(self) -> Optional[str]: |
| 309 |
""" |
| 310 |
The hostname specified by the remote peer for this connection. |
| 311 |
|
| 312 |
This will return ``None`` until the :const:`REMOTE_ACTIVE` state is |
| 313 |
reached. See :class:`Endpoint` for more details on endpoint state. |
| 314 |
|
| 315 |
Any (non ``None``) name returned by this operation will be valid until |
| 316 |
the connection object is unbound from a transport or freed, |
| 317 |
whichever happens sooner. |
| 318 |
""" |
| 319 |
return pn_connection_remote_hostname(self._impl) |
| 320 |
|
| 321 |
@property |
| 322 |
def remote_offered_capabilities(self): |
| 323 |
""" |
| 324 |
The capabilities offered by the remote peer for this connection. |
| 325 |
|
| 326 |
This operation will return a :class:`Data` object that |
| 327 |
is valid until the connection object is freed. This :class:`Data` |
| 328 |
object will be empty until the remote connection is opened as |
| 329 |
indicated by the :const:`REMOTE_ACTIVE` flag. |
| 330 |
|
| 331 |
:type: :class:`Data` |
| 332 |
""" |
| 333 |
return dat2obj(pn_connection_remote_offered_capabilities(self._impl)) |
| 334 |
|
| 335 |
@property |
| 336 |
def remote_desired_capabilities(self): |
| 337 |
""" |
| 338 |
The capabilities desired by the remote peer for this connection. |
| 339 |
|
| 340 |
This operation will return a :class:`Data` object that |
| 341 |
is valid until the connection object is freed. This :class:`Data` |
| 342 |
object will be empty until the remote connection is opened as |
| 343 |
indicated by the :const:`REMOTE_ACTIVE` flag. |
| 344 |
|
| 345 |
:type: :class:`Data` |
| 346 |
""" |
| 347 |
return dat2obj(pn_connection_remote_desired_capabilities(self._impl)) |
| 348 |
|
| 349 |
@property |
| 350 |
def remote_properties(self): |
| 351 |
""" |
| 352 |
The properties specified by the remote peer for this connection. |
| 353 |
|
| 354 |
This operation will return a :class:`Data` object that |
| 355 |
is valid until the connection object is freed. This :class:`Data` |
| 356 |
object will be empty until the remote connection is opened as |
| 357 |
indicated by the :const:`REMOTE_ACTIVE` flag. |
| 358 |
|
| 359 |
:type: :class:`Data` |
| 360 |
""" |
| 361 |
return dat2obj(pn_connection_remote_properties(self._impl)) |
| 362 |
|
| 363 |
@property |
| 364 |
def connected_address(self) -> str: |
| 365 |
"""The address for this connection.""" |
| 366 |
return self.url and str(self.url) |
| 367 |
|
| 368 |
def open(self) -> None: |
| 369 |
""" |
| 370 |
Opens the connection. |
| 371 |
|
| 372 |
In more detail, this moves the local state of the connection to |
| 373 |
the ``ACTIVE`` state and triggers an open frame to be sent to the |
| 374 |
peer. A connection is fully active once both peers have opened it. |
| 375 |
""" |
| 376 |
obj2dat(self.offered_capabilities, |
| 377 |
pn_connection_offered_capabilities(self._impl)) |
| 378 |
obj2dat(self.desired_capabilities, |
| 379 |
pn_connection_desired_capabilities(self._impl)) |
| 380 |
obj2dat(self.properties, pn_connection_properties(self._impl)) |
| 381 |
pn_connection_open(self._impl) |
| 382 |
|
| 383 |
def close(self) -> None: |
| 384 |
""" |
| 385 |
Closes the connection. |
| 386 |
|
| 387 |
In more detail, this moves the local state of the connection to |
| 388 |
the ``CLOSED`` state and triggers a close frame to be sent to the |
| 389 |
peer. A connection is fully closed once both peers have closed it. |
| 390 |
""" |
| 391 |
self._update_cond() |
| 392 |
pn_connection_close(self._impl) |
| 393 |
if hasattr(self, '_session_policy'): |
| 394 |
# break circular ref |
| 395 |
del self._session_policy |
| 396 |
t = self.transport |
| 397 |
if t and t._connect_selectable: |
| 398 |
# close() requested before TCP connect handshake completes on socket. |
| 399 |
# Dismantle connection setup logic. |
| 400 |
s = t._connect_selectable |
| 401 |
t._connect_selectable = None |
| 402 |
t.close_head() |
| 403 |
t.close_tail() |
| 404 |
s._transport = None |
| 405 |
t._selectable = None |
| 406 |
s.terminate() |
| 407 |
s.update() |
| 408 |
|
| 409 |
@property |
| 410 |
def state(self) -> int: |
| 411 |
""" |
| 412 |
The state of the connection as a bit field. The state has a local |
| 413 |
and a remote component. Each of these can be in one of three |
| 414 |
states: ``UNINIT``, ``ACTIVE`` or ``CLOSED``. These can be tested by masking |
| 415 |
against :const:`LOCAL_UNINIT`, :const:`LOCAL_ACTIVE`, :const:`LOCAL_CLOSED`, :const:`REMOTE_UNINIT`, |
| 416 |
:const:`REMOTE_ACTIVE` and :const:`REMOTE_CLOSED`. |
| 417 |
""" |
| 418 |
return pn_connection_state(self._impl) |
| 419 |
|
| 420 |
def session(self) -> 'Session': |
| 421 |
""" |
| 422 |
Returns a new session on this connection. |
| 423 |
|
| 424 |
:return: New session |
| 425 |
:raises: :class:`SessionException` |
| 426 |
""" |
| 427 |
ssn = pn_session(self._impl) |
| 428 |
if ssn is None: |
| 429 |
raise (SessionException("Session allocation failed.")) |
| 430 |
else: |
| 431 |
return Session(ssn) |
| 432 |
|
| 433 |
def session_head(self, mask: int) -> Optional['Session']: |
| 434 |
""" |
| 435 |
Retrieve the first session from a given connection that matches the |
| 436 |
specified state mask. |
| 437 |
|
| 438 |
Examines the state of each session owned by the connection, and |
| 439 |
returns the first session that matches the given state mask. If |
| 440 |
state contains both local and remote flags, then an exact match |
| 441 |
against those flags is performed. If state contains only local or |
| 442 |
only remote flags, then a match occurs if any of the local or |
| 443 |
remote flags are set respectively. |
| 444 |
|
| 445 |
:param mask: State mask to match |
| 446 |
:return: The first session owned by the connection that matches the |
| 447 |
mask, else ``None`` if no sessions matches. |
| 448 |
""" |
| 449 |
return Session.wrap(pn_session_head(self._impl, mask)) |
| 450 |
|
| 451 |
def link_head(self, mask: int) -> Optional[Union['Sender', 'Receiver']]: |
| 452 |
""" |
| 453 |
Retrieve the first link that matches the given state mask. |
| 454 |
|
| 455 |
Examines the state of each link owned by the connection and returns |
| 456 |
the first link that matches the given state mask. If state contains |
| 457 |
both local and remote flags, then an exact match against those |
| 458 |
flags is performed. If state contains only local or only remote |
| 459 |
flags, then a match occurs if any of the local or remote flags are |
| 460 |
set respectively. ``state==0`` matches all links. |
| 461 |
|
| 462 |
:param mask: State mask to match |
| 463 |
:return: The first link owned by the connection that matches the |
| 464 |
mask, else ``None`` if no link matches. |
| 465 |
""" |
| 466 |
return Link.wrap(pn_link_head(self._impl, mask)) |
| 467 |
|
| 468 |
@property |
| 469 |
def work_head(self) -> Optional[Delivery]: |
| 470 |
"""Deprecated: use on_message(), on_accepted(), on_rejected(), |
| 471 |
on_released(), and on_settled() instead. |
| 472 |
|
| 473 |
Extracts the first delivery on the connection that has pending |
| 474 |
operations. |
| 475 |
|
| 476 |
Retrieves the first delivery on the Connection that has pending |
| 477 |
operations. A readable delivery indicates message data is waiting |
| 478 |
to be read. A writable delivery indicates that message data may be |
| 479 |
sent. An updated delivery indicates that the delivery's disposition |
| 480 |
has changed. A delivery will never be both readable and writable, |
| 481 |
but it may be both readable and updated or both writable and |
| 482 |
updated. |
| 483 |
|
| 484 |
:return: The first delivery object that needs to be serviced, or ``None`` if none. |
| 485 |
:rtype: :class:`Delivery` or ``None`` |
| 486 |
""" |
| 487 |
return Delivery.wrap(pn_work_head(self._impl)) |
| 488 |
|
| 489 |
@property |
| 490 |
def error(self): |
| 491 |
""" |
| 492 |
Additional error information associated with the connection. |
| 493 |
|
| 494 |
Whenever a connection operation fails (i.e. returns an error code), |
| 495 |
additional error details can be obtained using this property. The |
| 496 |
returned value is the error code defined by Proton in ``pn_error_t`` |
| 497 |
(see ``error.h``). |
| 498 |
|
| 499 |
:type: ``int`` |
| 500 |
""" |
| 501 |
return pn_error_code(pn_connection_error(self._impl)) |
| 502 |
|
| 503 |
def free(self) -> None: |
| 504 |
""" |
| 505 |
Releases this connection object. |
| 506 |
|
| 507 |
When a connection object is released, all :class:`Session` and |
| 508 |
:class:`Link` objects associated with this connection are also |
| 509 |
released and all :class:`Delivery` objects are settled. |
| 510 |
""" |
| 511 |
pn_connection_release(self._impl) |
| 512 |
|
| 513 |
@property |
| 514 |
def offered_capabilities(self) -> Optional[Union['Array', SymbolList]]: |
| 515 |
"""Offered capabilities as a list of symbols. The AMQP 1.0 specification |
| 516 |
restricts this list to symbol elements only. It is possible to use |
| 517 |
the special ``list`` subclass :class:`SymbolList` as it will by |
| 518 |
default enforce this restriction on construction. In addition, if a |
| 519 |
string type is used, it will be silently converted into the required |
| 520 |
symbol. |
| 521 |
""" |
| 522 |
return self.offered_capabilities_list |
| 523 |
|
| 524 |
@offered_capabilities.setter |
| 525 |
def offered_capabilities( |
| 526 |
self, |
| 527 |
offered_capability_list: Optional[Union['Array', List['symbol'], SymbolList, List[str]]] |
| 528 |
) -> None: |
| 529 |
if isinstance(offered_capability_list, list): |
| 530 |
self.offered_capabilities_list = SymbolList(offered_capability_list, raise_on_error=False) |
| 531 |
else: |
| 532 |
self.offered_capabilities_list = offered_capability_list |
| 533 |
|
| 534 |
@property |
| 535 |
def desired_capabilities(self) -> Optional[Union['Array', SymbolList]]: |
| 536 |
"""Desired capabilities as a list of symbols. The AMQP 1.0 specification |
| 537 |
restricts this list to symbol elements only. It is possible to use |
| 538 |
the special ``list`` subclass :class:`SymbolList` which will by |
| 539 |
default enforce this restriction on construction. In addition, if string |
| 540 |
types are used, this class will be silently convert them into symbols. |
| 541 |
""" |
| 542 |
return self.desired_capabilities_list |
| 543 |
|
| 544 |
@desired_capabilities.setter |
| 545 |
def desired_capabilities( |
| 546 |
self, |
| 547 |
desired_capability_list: Optional[Union['Array', List['symbol'], SymbolList, List[str]]] |
| 548 |
) -> None: |
| 549 |
if isinstance(desired_capability_list, list): |
| 550 |
self.desired_capabilities_list = SymbolList(desired_capability_list, raise_on_error=False) |
| 551 |
else: |
| 552 |
self.desired_capabilities_list = desired_capability_list |
| 553 |
|
| 554 |
@property |
| 555 |
def properties(self) -> Optional[PropertyDict]: |
| 556 |
"""Connection properties as a dictionary of key/values. The AMQP 1.0 |
| 557 |
specification restricts this dictionary to have keys that are only |
| 558 |
:class:`symbol` types. It is possible to use the special ``dict`` |
| 559 |
subclass :class:`PropertyDict` which will by default enforce this |
| 560 |
restrictions on construction. In addition, if strings type are used, |
| 561 |
this will silently convert them into symbols. |
| 562 |
""" |
| 563 |
return self.properties_dict |
| 564 |
|
| 565 |
@properties.setter |
| 566 |
def properties(self, properties_dict: Optional[Union[PropertyDict, Dict[str, 'PythonAMQPData']]]) -> None: |
| 567 |
if isinstance(properties_dict, dict): |
| 568 |
self.properties_dict = PropertyDict(properties_dict, raise_on_error=False) |
| 569 |
else: |
| 570 |
self.properties_dict = properties_dict |
| 571 |
|
| 572 |
|
| 573 |
class Session(Wrapper, Endpoint): |
| 574 |
"""A container of links""" |
| 575 |
@staticmethod |
| 576 |
def wrap(impl): |
| 577 |
if impl is None: |
| 578 |
return None |
| 579 |
else: |
| 580 |
return Session(impl) |
| 581 |
|
| 582 |
def __init__(self, impl): |
| 583 |
Wrapper.__init__(self, impl, pn_session_attachments) |
| 584 |
|
| 585 |
def _get_attachments(self): |
| 586 |
return pn_session_attachments(self._impl) |
| 587 |
|
| 588 |
def _get_cond_impl(self): |
| 589 |
return pn_session_condition(self._impl) |
| 590 |
|
| 591 |
def _get_remote_cond_impl(self): |
| 592 |
return pn_session_remote_condition(self._impl) |
| 593 |
|
| 594 |
@property |
| 595 |
def incoming_capacity(self) -> int: |
| 596 |
"""The incoming capacity of this session in bytes. The incoming capacity |
| 597 |
of a session determines how much incoming message data the session |
| 598 |
can buffer. |
| 599 |
|
| 600 |
.. note:: If set, this value must be greater than or equal to the negotiated |
| 601 |
frame size of the transport. The window is computed as a whole number of |
| 602 |
frames when dividing remaining capacity at a given time by the connection |
| 603 |
max frame size. As such, capacity and max frame size should be chosen so |
| 604 |
as to ensure the frame window isn't unduly small and limiting performance. |
| 605 |
""" |
| 606 |
return pn_session_get_incoming_capacity(self._impl) |
| 607 |
|
| 608 |
@incoming_capacity.setter |
| 609 |
def incoming_capacity(self, capacity: int) -> None: |
| 610 |
pn_session_set_incoming_capacity(self._impl, capacity) |
| 611 |
|
| 612 |
@property |
| 613 |
def outgoing_window(self) -> int: |
| 614 |
"""The outgoing window for this session.""" |
| 615 |
return pn_session_get_outgoing_window(self._impl) |
| 616 |
|
| 617 |
@outgoing_window.setter |
| 618 |
def outgoing_window(self, window: int) -> None: |
| 619 |
pn_session_set_outgoing_window(self._impl, window) |
| 620 |
|
| 621 |
@property |
| 622 |
def outgoing_bytes(self) -> int: |
| 623 |
""" |
| 624 |
The number of outgoing bytes currently buffered.""" |
| 625 |
return pn_session_outgoing_bytes(self._impl) |
| 626 |
|
| 627 |
@property |
| 628 |
def incoming_bytes(self) -> int: |
| 629 |
""" |
| 630 |
The number of incoming bytes currently buffered. |
| 631 |
""" |
| 632 |
return pn_session_incoming_bytes(self._impl) |
| 633 |
|
| 634 |
def open(self) -> None: |
| 635 |
""" |
| 636 |
Open a session. Once this operation has completed, the |
| 637 |
:const:`LOCAL_ACTIVE` state flag will be set. |
| 638 |
""" |
| 639 |
pn_session_open(self._impl) |
| 640 |
|
| 641 |
def close(self) -> None: |
| 642 |
""" |
| 643 |
Close a session. |
| 644 |
|
| 645 |
Once this operation has completed, the :const:`LOCAL_CLOSED` state flag |
| 646 |
will be set. This may be called without calling |
| 647 |
:meth:`open`, in this case it is equivalent to calling |
| 648 |
:meth:`open` followed by :meth:`close`. |
| 649 |
|
| 650 |
""" |
| 651 |
self._update_cond() |
| 652 |
pn_session_close(self._impl) |
| 653 |
|
| 654 |
def next(self, mask): |
| 655 |
""" |
| 656 |
Retrieve the next session for this connection that matches the |
| 657 |
specified state mask. |
| 658 |
|
| 659 |
When used with :meth:`Connection.session_head`, application can |
| 660 |
access all sessions on the connection that match the given state. |
| 661 |
See :meth:`Connection.session_head` for description of match |
| 662 |
behavior. |
| 663 |
|
| 664 |
:param mask: Mask to match. |
| 665 |
:return: The next session owned by this connection that matches the |
| 666 |
mask, else ``None`` if no sessions match. |
| 667 |
:rtype: :class:`Session` or ``None`` |
| 668 |
""" |
| 669 |
return Session.wrap(pn_session_next(self._impl, mask)) |
| 670 |
|
| 671 |
@property |
| 672 |
def state(self) -> int: |
| 673 |
""" |
| 674 |
The endpoint state flags for this session. See :class:`Endpoint` for |
| 675 |
details of the flags. |
| 676 |
""" |
| 677 |
return pn_session_state(self._impl) |
| 678 |
|
| 679 |
@property |
| 680 |
def connection(self) -> Connection: |
| 681 |
""" |
| 682 |
The parent connection for this session. |
| 683 |
""" |
| 684 |
return Connection.wrap(pn_session_connection(self._impl)) |
| 685 |
|
| 686 |
@property |
| 687 |
def transport(self) -> Transport: |
| 688 |
""" |
| 689 |
The transport bound to the parent connection for this session. |
| 690 |
""" |
| 691 |
return self.connection.transport |
| 692 |
|
| 693 |
def sender(self, name: str) -> 'Sender': |
| 694 |
""" |
| 695 |
Create a new :class:`Sender` on this session. |
| 696 |
|
| 697 |
:param name: Name of sender |
| 698 |
""" |
| 699 |
return Sender(pn_sender(self._impl, unicode2utf8(name))) |
| 700 |
|
| 701 |
def receiver(self, name: str) -> 'Receiver': |
| 702 |
""" |
| 703 |
Create a new :class:`Receiver` on this session. |
| 704 |
|
| 705 |
:param name: Name of receiver |
| 706 |
""" |
| 707 |
return Receiver(pn_receiver(self._impl, unicode2utf8(name))) |
| 708 |
|
| 709 |
def free(self) -> None: |
| 710 |
""" |
| 711 |
Free this session. When a session is freed it will no |
| 712 |
longer be retained by the connection once any internal |
| 713 |
references to the session are no longer needed. Freeing |
| 714 |
a session will free all links on that session and settle |
| 715 |
any deliveries on those links. |
| 716 |
""" |
| 717 |
pn_session_free(self._impl) |
| 718 |
|
| 719 |
|
| 720 |
class Link(Wrapper, Endpoint): |
| 721 |
""" |
| 722 |
A representation of an AMQP link (a unidirectional channel for |
| 723 |
transferring messages), of which there are two concrete |
| 724 |
implementations, :class:`Sender` and :class:`Receiver`. |
| 725 |
""" |
| 726 |
|
| 727 |
SND_UNSETTLED = PN_SND_UNSETTLED |
| 728 |
"""The sender will send all deliveries initially unsettled.""" |
| 729 |
SND_SETTLED = PN_SND_SETTLED |
| 730 |
"""The sender will send all deliveries settled to the receiver.""" |
| 731 |
SND_MIXED = PN_SND_MIXED |
| 732 |
"""The sender may send a mixture of settled and unsettled deliveries.""" |
| 733 |
|
| 734 |
RCV_FIRST = PN_RCV_FIRST |
| 735 |
"""The receiver will settle deliveries regardless of what the sender does.""" |
| 736 |
RCV_SECOND = PN_RCV_SECOND |
| 737 |
"""The receiver will only settle deliveries after the sender settles.""" |
| 738 |
|
| 739 |
@staticmethod |
| 740 |
def wrap(impl): |
| 741 |
if impl is None: |
| 742 |
return None |
| 743 |
if pn_link_is_sender(impl): |
| 744 |
return Sender(impl) |
| 745 |
else: |
| 746 |
return Receiver(impl) |
| 747 |
|
| 748 |
def __init__(self, impl): |
| 749 |
Wrapper.__init__(self, impl, pn_link_attachments) |
| 750 |
|
| 751 |
def _init(self) -> None: |
| 752 |
Endpoint._init(self) |
| 753 |
self.properties = None |
| 754 |
|
| 755 |
def _get_attachments(self): |
| 756 |
return pn_link_attachments(self._impl) |
| 757 |
|
| 758 |
def _check(self, err: int) -> int: |
| 759 |
if err < 0: |
| 760 |
exc = EXCEPTIONS.get(err, LinkException) |
| 761 |
raise exc("[%s]: %s" % (err, pn_error_text(pn_link_error(self._impl)))) |
| 762 |
else: |
| 763 |
return err |
| 764 |
|
| 765 |
def _get_cond_impl(self): |
| 766 |
return pn_link_condition(self._impl) |
| 767 |
|
| 768 |
def _get_remote_cond_impl(self): |
| 769 |
return pn_link_remote_condition(self._impl) |
| 770 |
|
| 771 |
def open(self) -> None: |
| 772 |
""" |
| 773 |
Opens the link. |
| 774 |
|
| 775 |
In more detail, this moves the local state of the link to the |
| 776 |
:const:`LOCAL_ACTIVE` state and triggers an attach frame to be |
| 777 |
sent to the peer. A link is fully active once both peers have |
| 778 |
attached it. |
| 779 |
""" |
| 780 |
obj2dat(self.properties, pn_link_properties(self._impl)) |
| 781 |
pn_link_open(self._impl) |
| 782 |
|
| 783 |
def close(self) -> None: |
| 784 |
""" |
| 785 |
Closes the link. |
| 786 |
|
| 787 |
In more detail, this moves the local state of the link to the |
| 788 |
:const:`LOCAL_CLOSED` state and triggers an detach frame (with |
| 789 |
the closed flag set) to be sent to the peer. A link is fully |
| 790 |
closed once both peers have detached it. |
| 791 |
|
| 792 |
This may be called without calling :meth:`open`, in this case it |
| 793 |
is equivalent to calling :meth:`open` followed by :meth:`close`. |
| 794 |
""" |
| 795 |
self._update_cond() |
| 796 |
pn_link_close(self._impl) |
| 797 |
|
| 798 |
@property |
| 799 |
def state(self) -> int: |
| 800 |
""" |
| 801 |
The state of the link as a bit field. The state has a local |
| 802 |
and a remote component. Each of these can be in one of three |
| 803 |
states: ``UNINIT``, ``ACTIVE`` or ``CLOSED``. These can be |
| 804 |
tested by masking against :const:`LOCAL_UNINIT`, |
| 805 |
:const:`LOCAL_ACTIVE`, :const:`LOCAL_CLOSED`, |
| 806 |
:const:`REMOTE_UNINIT`, :const:`REMOTE_ACTIVE` and |
| 807 |
:const:`REMOTE_CLOSED`. |
| 808 |
""" |
| 809 |
return pn_link_state(self._impl) |
| 810 |
|
| 811 |
@property |
| 812 |
def source(self) -> 'Terminus': |
| 813 |
""" |
| 814 |
The source of the link as described by the local peer. The |
| 815 |
returned object is valid until the link is freed. |
| 816 |
""" |
| 817 |
return Terminus(pn_link_source(self._impl)) |
| 818 |
|
| 819 |
@property |
| 820 |
def target(self) -> 'Terminus': |
| 821 |
""" |
| 822 |
The target of the link as described by the local peer. The |
| 823 |
returned object is valid until the link is freed. |
| 824 |
""" |
| 825 |
return Terminus(pn_link_target(self._impl)) |
| 826 |
|
| 827 |
@property |
| 828 |
def remote_source(self) -> 'Terminus': |
| 829 |
""" |
| 830 |
The source of the link as described by the remote peer. The |
| 831 |
returned object is valid until the link is freed. The remote |
| 832 |
:class:`Terminus` object will be empty until the link is |
| 833 |
remotely opened as indicated by the :const:`REMOTE_ACTIVE` |
| 834 |
flag. |
| 835 |
""" |
| 836 |
return Terminus(pn_link_remote_source(self._impl)) |
| 837 |
|
| 838 |
@property |
| 839 |
def remote_target(self) -> 'Terminus': |
| 840 |
""" |
| 841 |
The target of the link as described by the remote peer. The |
| 842 |
returned object is valid until the link is freed. The remote |
| 843 |
:class:`Terminus` object will be empty until the link is |
| 844 |
remotely opened as indicated by the :const:`REMOTE_ACTIVE` |
| 845 |
flag. |
| 846 |
""" |
| 847 |
return Terminus(pn_link_remote_target(self._impl)) |
| 848 |
|
| 849 |
@property |
| 850 |
def session(self) -> Session: |
| 851 |
""" |
| 852 |
The parent session for this link. |
| 853 |
""" |
| 854 |
return Session.wrap(pn_link_session(self._impl)) |
| 855 |
|
| 856 |
@property |
| 857 |
def connection(self) -> Connection: |
| 858 |
""" |
| 859 |
The connection on which this link was attached. |
| 860 |
""" |
| 861 |
return self.session.connection |
| 862 |
|
| 863 |
@property |
| 864 |
def transport(self) -> Transport: |
| 865 |
""" |
| 866 |
The transport bound to the connection on which this link was attached. |
| 867 |
""" |
| 868 |
return self.session.transport |
| 869 |
|
| 870 |
def delivery(self, tag: str) -> Delivery: |
| 871 |
""" |
| 872 |
Create a delivery. Every delivery object within a |
| 873 |
link must be supplied with a unique tag. Links |
| 874 |
maintain a sequence of delivery object in the order that |
| 875 |
they are created. |
| 876 |
|
| 877 |
:param tag: Delivery tag unique for this link. |
| 878 |
""" |
| 879 |
return Delivery(pn_delivery(self._impl, tag)) |
| 880 |
|
| 881 |
@property |
| 882 |
def current(self) -> Optional[Delivery]: |
| 883 |
""" |
| 884 |
The current delivery for this link. |
| 885 |
|
| 886 |
Each link maintains a sequence of deliveries in the order |
| 887 |
they were created, along with a pointer to the *current* |
| 888 |
delivery. All send/recv operations on a link take place |
| 889 |
on the *current* delivery. If a link has no current delivery, |
| 890 |
the current delivery is automatically initialized to the |
| 891 |
next delivery created on the link. Once initialized, the |
| 892 |
current delivery remains the same until it is changed through |
| 893 |
use of :meth:`advance` or until it is settled via |
| 894 |
:meth:`Delivery.settle`. |
| 895 |
""" |
| 896 |
return Delivery.wrap(pn_link_current(self._impl)) |
| 897 |
|
| 898 |
def advance(self) -> bool: |
| 899 |
""" |
| 900 |
Advance the current delivery of this link to the next delivery. |
| 901 |
|
| 902 |
For sending links this operation is used to finish sending message |
| 903 |
data for the current outgoing delivery and move on to the next |
| 904 |
outgoing delivery (if any). |
| 905 |
|
| 906 |
For receiving links, this operation is used to finish accessing |
| 907 |
message data from the current incoming delivery and move on to the |
| 908 |
next incoming delivery (if any). |
| 909 |
|
| 910 |
Each link maintains a sequence of deliveries in the order they were |
| 911 |
created, along with a pointer to the *current* delivery. The |
| 912 |
:meth:`advance` operation will modify the *current* delivery on the |
| 913 |
link to point to the next delivery in the sequence. If there is no |
| 914 |
next delivery in the sequence, the current delivery will be set to |
| 915 |
``NULL``. |
| 916 |
|
| 917 |
:return: ``True`` if the value of the current delivery changed (even |
| 918 |
if it was set to ``NULL``, ``False`` otherwise. |
| 919 |
""" |
| 920 |
return pn_link_advance(self._impl) |
| 921 |
|
| 922 |
@property |
| 923 |
def unsettled(self) -> int: |
| 924 |
""" |
| 925 |
The number of unsettled deliveries for this link. |
| 926 |
""" |
| 927 |
return pn_link_unsettled(self._impl) |
| 928 |
|
| 929 |
@property |
| 930 |
def credit(self) -> int: |
| 931 |
""" |
| 932 |
The amount of outstanding credit on this link. |
| 933 |
|
| 934 |
Links use a credit based flow control scheme. Every receiver |
| 935 |
maintains a credit balance that corresponds to the number of |
| 936 |
deliveries that the receiver can accept at any given moment. As |
| 937 |
more capacity becomes available at the receiver (see |
| 938 |
:meth:`Receiver.flow`), it adds credit to this balance and |
| 939 |
communicates the new balance to the sender. Whenever a delivery |
| 940 |
is sent/received, the credit balance maintained by the link is |
| 941 |
decremented by one. Once the credit balance at the sender reaches |
| 942 |
zero, the sender must pause sending until more credit is obtained |
| 943 |
from the receiver. |
| 944 |
|
| 945 |
.. note:: A sending link may still be used to send deliveries even |
| 946 |
if :attr:`credit` reaches zero, however those deliveries will end |
| 947 |
up being buffered by the link until enough credit is obtained from |
| 948 |
the receiver to send them over the wire. In this case the balance |
| 949 |
reported by :attr:`credit` will go negative. |
| 950 |
""" |
| 951 |
return pn_link_credit(self._impl) |
| 952 |
|
| 953 |
@property |
| 954 |
def available(self) -> int: |
| 955 |
""" |
| 956 |
The available deliveries hint for this link. |
| 957 |
|
| 958 |
The available count for a link provides a hint as to the number of |
| 959 |
deliveries that might be able to be sent if sufficient credit were |
| 960 |
issued by the receiving link endpoint. See :meth:`Sender.offered` for |
| 961 |
more details. |
| 962 |
""" |
| 963 |
return pn_link_available(self._impl) |
| 964 |
|
| 965 |
@property |
| 966 |
def queued(self) -> int: |
| 967 |
""" |
| 968 |
The number of queued deliveries for a link. |
| 969 |
|
| 970 |
Links may queue deliveries for a number of reasons, for example |
| 971 |
there may be insufficient credit to send them to the receiver (see |
| 972 |
:meth:`credit`), or they simply may not have yet had a chance to |
| 973 |
be written to the wire. This operation will return the number of |
| 974 |
queued deliveries on a link. |
| 975 |
""" |
| 976 |
return pn_link_queued(self._impl) |
| 977 |
|
| 978 |
def next(self, mask: int) -> Optional[Union['Sender', 'Receiver']]: |
| 979 |
""" |
| 980 |
Retrieve the next link that matches the given state mask. |
| 981 |
|
| 982 |
When used with :meth:`Connection.link_head`, the application |
| 983 |
can access all links on the connection that match the given |
| 984 |
state. See :meth:`Connection.link_head` for a description of |
| 985 |
match behavior. |
| 986 |
|
| 987 |
:param mask: State mask to match |
| 988 |
:return: The next link that matches the given state mask, or |
| 989 |
``None`` if no link matches. |
| 990 |
""" |
| 991 |
return Link.wrap(pn_link_next(self._impl, mask)) |
| 992 |
|
| 993 |
@property |
| 994 |
def name(self) -> str: |
| 995 |
""" |
| 996 |
The name of the link. |
| 997 |
""" |
| 998 |
return utf82unicode(pn_link_name(self._impl)) |
| 999 |
|
| 1000 |
@property |
| 1001 |
def is_sender(self) -> bool: |
| 1002 |
""" |
| 1003 |
``True`` if this link is a sender, ``False`` otherwise. |
| 1004 |
""" |
| 1005 |
return pn_link_is_sender(self._impl) |
| 1006 |
|
| 1007 |
@property |
| 1008 |
def is_receiver(self) -> bool: |
| 1009 |
""" |
| 1010 |
``True`` if this link is a receiver, ``False`` otherwise. |
| 1011 |
""" |
| 1012 |
return pn_link_is_receiver(self._impl) |
| 1013 |
|
| 1014 |
@property |
| 1015 |
def remote_snd_settle_mode(self) -> int: |
| 1016 |
""" |
| 1017 |
The remote sender settle mode for this link. One of |
| 1018 |
:const:`SND_UNSETTLED`, :const:`SND_SETTLED` or |
| 1019 |
:const:`SND_MIXED`. |
| 1020 |
""" |
| 1021 |
return pn_link_remote_snd_settle_mode(self._impl) |
| 1022 |
|
| 1023 |
@property |
| 1024 |
def remote_rcv_settle_mode(self) -> int: |
| 1025 |
""" |
| 1026 |
The remote receiver settle mode for this link. One of |
| 1027 |
:const:`RCV_FIRST` or :const:`RCV_SECOND`. |
| 1028 |
""" |
| 1029 |
return pn_link_remote_rcv_settle_mode(self._impl) |
| 1030 |
|
| 1031 |
@property |
| 1032 |
def snd_settle_mode(self) -> int: |
| 1033 |
"""The local sender settle mode for this link. One of |
| 1034 |
:const:`SND_UNSETTLED`, :const:`SND_SETTLED` or |
| 1035 |
:const:`SND_MIXED`. |
| 1036 |
""" |
| 1037 |
return pn_link_snd_settle_mode(self._impl) |
| 1038 |
|
| 1039 |
@snd_settle_mode.setter |
| 1040 |
def snd_settle_mode(self, mode: int) -> None: |
| 1041 |
pn_link_set_snd_settle_mode(self._impl, mode) |
| 1042 |
|
| 1043 |
@property |
| 1044 |
def rcv_settle_mode(self) -> int: |
| 1045 |
"""The local receiver settle mode for this link. One of |
| 1046 |
:const:`RCV_FIRST` or :const:`RCV_SECOND`.""" |
| 1047 |
return pn_link_rcv_settle_mode(self._impl) |
| 1048 |
|
| 1049 |
@rcv_settle_mode.setter |
| 1050 |
def rcv_settle_mode(self, mode: int) -> None: |
| 1051 |
pn_link_set_rcv_settle_mode(self._impl, mode) |
| 1052 |
|
| 1053 |
@property |
| 1054 |
def drain_mode(self) -> bool: |
| 1055 |
"""The drain mode on this link. |
| 1056 |
|
| 1057 |
If a link is in drain mode (``True``), then the sending |
| 1058 |
endpoint of a link must immediately use up all available |
| 1059 |
credit on the link. If this is not possible, the excess |
| 1060 |
credit must be returned by invoking :meth:`drained`. Only |
| 1061 |
the receiving endpoint can set the drain mode. |
| 1062 |
|
| 1063 |
When ``False``, this link is not in drain mode. |
| 1064 |
""" |
| 1065 |
return pn_link_get_drain(self._impl) |
| 1066 |
|
| 1067 |
@drain_mode.setter |
| 1068 |
def drain_mode(self, b: bool): |
| 1069 |
pn_link_set_drain(self._impl, bool(b)) |
| 1070 |
|
| 1071 |
def drained(self) -> int: |
| 1072 |
""" |
| 1073 |
Drain excess credit for this link. |
| 1074 |
|
| 1075 |
When a link is in drain mode (see :attr:`drain_mode`), the |
| 1076 |
sender must use all excess credit immediately, and release |
| 1077 |
any excess credit back to the receiver if there are no |
| 1078 |
deliveries available to send. |
| 1079 |
|
| 1080 |
When invoked on a sending link that is in drain mode, this |
| 1081 |
operation will release all excess credit back to the receiver |
| 1082 |
and return the number of credits released back to the sender. |
| 1083 |
If the link is not in drain mode, this operation is a noop. |
| 1084 |
|
| 1085 |
When invoked on a receiving link, this operation will return |
| 1086 |
and reset the number of credits the sender has released back |
| 1087 |
to the receiver. |
| 1088 |
|
| 1089 |
:return: The number of credits drained. |
| 1090 |
""" |
| 1091 |
return pn_link_drained(self._impl) |
| 1092 |
|
| 1093 |
@property |
| 1094 |
def remote_max_message_size(self) -> int: |
| 1095 |
""" |
| 1096 |
Get the remote view of the maximum message size for this link. |
| 1097 |
|
| 1098 |
.. warning:: **Unsettled API** |
| 1099 |
|
| 1100 |
A zero value means the size is unlimited. |
| 1101 |
""" |
| 1102 |
return pn_link_remote_max_message_size(self._impl) |
| 1103 |
|
| 1104 |
@property |
| 1105 |
def max_message_size(self) -> int: |
| 1106 |
"""The maximum message size for this link. A zero value means the |
| 1107 |
size is unlimited. |
| 1108 |
|
| 1109 |
.. warning:: **Unsettled API** |
| 1110 |
""" |
| 1111 |
return pn_link_max_message_size(self._impl) |
| 1112 |
|
| 1113 |
@max_message_size.setter |
| 1114 |
def max_message_size(self, mode: int) -> None: |
| 1115 |
pn_link_set_max_message_size(self._impl, mode) |
| 1116 |
|
| 1117 |
def detach(self) -> None: |
| 1118 |
""" |
| 1119 |
Detach this link. |
| 1120 |
""" |
| 1121 |
return pn_link_detach(self._impl) |
| 1122 |
|
| 1123 |
def free(self) -> None: |
| 1124 |
""" |
| 1125 |
Free this link object. When a link object is freed, |
| 1126 |
all :class:`Delivery` objects associated with the session (**<-- CHECK THIS**) |
| 1127 |
are also freed. Freeing a link will settle any unsettled |
| 1128 |
deliveries on the link. |
| 1129 |
""" |
| 1130 |
pn_link_free(self._impl) |
| 1131 |
|
| 1132 |
@property |
| 1133 |
def remote_properties(self): |
| 1134 |
""" |
| 1135 |
The properties specified by the remote peer for this link. |
| 1136 |
|
| 1137 |
This operation will return a :class:`Data` object that |
| 1138 |
is valid until the link object is freed. This :class:`Data` |
| 1139 |
object will be empty until the remote link is opened as |
| 1140 |
indicated by the :const:`REMOTE_ACTIVE` flag. |
| 1141 |
|
| 1142 |
:type: :class:`Data` |
| 1143 |
""" |
| 1144 |
return dat2obj(pn_link_remote_properties(self._impl)) |
| 1145 |
|
| 1146 |
@property |
| 1147 |
def properties(self) -> Optional[PropertyDict]: |
| 1148 |
"""Link properties as a dictionary of key/values. The AMQP 1.0 |
| 1149 |
specification restricts this dictionary to have keys that are only |
| 1150 |
:class:`symbol` types. It is possible to use the special ``dict`` |
| 1151 |
subclass :class:`PropertyDict` which will by default enforce this |
| 1152 |
restrictions on construction. In addition, if strings type are used, |
| 1153 |
this will silently convert them into symbols. |
| 1154 |
""" |
| 1155 |
return self._properties_dict |
| 1156 |
|
| 1157 |
@properties.setter |
| 1158 |
def properties(self, properties_dict: Optional[Dict['symbol', 'PythonAMQPData']]) -> None: |
| 1159 |
if isinstance(properties_dict, dict): |
| 1160 |
self._properties_dict = PropertyDict(properties_dict, raise_on_error=False) |
| 1161 |
else: |
| 1162 |
self._properties_dict = properties_dict |
| 1163 |
|
| 1164 |
|
| 1165 |
class Sender(Link): |
| 1166 |
""" |
| 1167 |
A link over which messages are sent. |
| 1168 |
""" |
| 1169 |
|
| 1170 |
def offered(self, n: int) -> None: |
| 1171 |
""" |
| 1172 |
Signal the availability of deliveries for this Sender. |
| 1173 |
|
| 1174 |
:param n: Credit the number of deliveries potentially |
| 1175 |
available for transfer. |
| 1176 |
""" |
| 1177 |
pn_link_offered(self._impl, n) |
| 1178 |
|
| 1179 |
def stream(self, data: bytes) -> int: |
| 1180 |
""" |
| 1181 |
Send specified data as part of the current delivery. |
| 1182 |
|
| 1183 |
:param data: Data to send |
| 1184 |
""" |
| 1185 |
return self._check(pn_link_send(self._impl, data)) |
| 1186 |
|
| 1187 |
def send(self, obj: Union[bytes, 'Message'], tag: Optional[str] = None) -> Union[int, Delivery]: |
| 1188 |
""" |
| 1189 |
A convenience method to send objects as message content. |
| 1190 |
|
| 1191 |
Send specified object over this sender; the object is expected to |
| 1192 |
have a ``send()`` method on it that takes the sender and an optional |
| 1193 |
tag as arguments. |
| 1194 |
|
| 1195 |
Where the object is a :class:`Message`, this will send the message over |
| 1196 |
this link, creating a new delivery for the purpose. |
| 1197 |
""" |
| 1198 |
if hasattr(obj, 'send'): |
| 1199 |
return obj.send(self, tag=tag) |
| 1200 |
else: |
| 1201 |
# treat object as bytes |
| 1202 |
return self.stream(obj) |
| 1203 |
|
| 1204 |
def delivery_tag(self) -> str: |
| 1205 |
"""Increments and returns a counter to be used as the next message tag.""" |
| 1206 |
if not hasattr(self, 'tag_generator'): |
| 1207 |
def simple_tags(): |
| 1208 |
count = 1 |
| 1209 |
while True: |
| 1210 |
yield str(count) |
| 1211 |
count += 1 |
| 1212 |
|
| 1213 |
self.tag_generator = simple_tags() |
| 1214 |
return next(self.tag_generator) |
| 1215 |
|
| 1216 |
|
| 1217 |
class Receiver(Link): |
| 1218 |
""" |
| 1219 |
A link over which messages are received. |
| 1220 |
""" |
| 1221 |
|
| 1222 |
def flow(self, n: int) -> None: |
| 1223 |
""" |
| 1224 |
Increases the credit issued to the remote sender by the specified number of messages. |
| 1225 |
|
| 1226 |
:param n: The credit to be issued to the remote sender. |
| 1227 |
""" |
| 1228 |
pn_link_flow(self._impl, n) |
| 1229 |
|
| 1230 |
def recv(self, limit: int) -> Optional[bytes]: |
| 1231 |
""" |
| 1232 |
Receive message data for the current delivery on this receiver. |
| 1233 |
|
| 1234 |
.. note:: The link API can be used to stream large messages across |
| 1235 |
the network, so just because there is no data to read does not |
| 1236 |
imply the message is complete. To ensure the entirety of the |
| 1237 |
message data has been read, either invoke :meth:`recv` until |
| 1238 |
``None`` is returned. |
| 1239 |
|
| 1240 |
:param limit: the max data size to receive of this message |
| 1241 |
:return: The received message data, or ``None`` if the message |
| 1242 |
has been completely received. |
| 1243 |
:raise: * :class:`Timeout` if timed out |
| 1244 |
* :class:`Interrupt` if interrupted |
| 1245 |
* :class:`LinkException` for all other exceptions |
| 1246 |
""" |
| 1247 |
n, binary = pn_link_recv(self._impl, limit) |
| 1248 |
if n == PN_EOS: |
| 1249 |
return None |
| 1250 |
else: |
| 1251 |
self._check(n) |
| 1252 |
return binary |
| 1253 |
|
| 1254 |
def drain(self, n: int) -> None: |
| 1255 |
""" |
| 1256 |
Grant credit for incoming deliveries on this receiver, and |
| 1257 |
set drain mode to true. |
| 1258 |
|
| 1259 |
Use :attr:`drain_mode` to set the drain mode explicitly. |
| 1260 |
|
| 1261 |
:param n: The amount by which to increment the link credit |
| 1262 |
""" |
| 1263 |
pn_link_drain(self._impl, n) |
| 1264 |
|
| 1265 |
def draining(self) -> bool: |
| 1266 |
""" |
| 1267 |
Check if a link is currently draining. A link is defined |
| 1268 |
to be draining when drain mode is set to ``True``, and the |
| 1269 |
sender still has excess credit. |
| 1270 |
|
| 1271 |
:return: ``True`` if the link is currently draining, ``False`` otherwise. |
| 1272 |
""" |
| 1273 |
return pn_link_draining(self._impl) |
| 1274 |
|
| 1275 |
|
| 1276 |
class Terminus(object): |
| 1277 |
""" |
| 1278 |
A source or target for messages. |
| 1279 |
""" |
| 1280 |
UNSPECIFIED = PN_UNSPECIFIED |
| 1281 |
"""A nonexistent terminus, may used as a source or target.""" |
| 1282 |
SOURCE = PN_SOURCE |
| 1283 |
"""A source of messages.""" |
| 1284 |
TARGET = PN_TARGET |
| 1285 |
"""A target for messages.""" |
| 1286 |
COORDINATOR = PN_COORDINATOR |
| 1287 |
"""A special target identifying a transaction coordinator.""" |
| 1288 |
|
| 1289 |
NONDURABLE = PN_NONDURABLE |
| 1290 |
"""A non durable terminus.""" |
| 1291 |
CONFIGURATION = PN_CONFIGURATION |
| 1292 |
"""A terminus with durably held configuration, but not delivery state.""" |
| 1293 |
DELIVERIES = PN_DELIVERIES |
| 1294 |
"""A terminus with both durably held configuration and durably held delivery state.""" |
| 1295 |
|
| 1296 |
DIST_MODE_UNSPECIFIED = PN_DIST_MODE_UNSPECIFIED |
| 1297 |
"""The behavior is defined by the node.""" |
| 1298 |
DIST_MODE_COPY = PN_DIST_MODE_COPY |
| 1299 |
"""The receiver gets all messages.""" |
| 1300 |
DIST_MODE_MOVE = PN_DIST_MODE_MOVE |
| 1301 |
"""The receiver competes for messages.""" |
| 1302 |
|
| 1303 |
EXPIRE_WITH_LINK = PN_EXPIRE_WITH_LINK |
| 1304 |
"""The terminus is orphaned when the parent link is closed.""" |
| 1305 |
EXPIRE_WITH_SESSION = PN_EXPIRE_WITH_SESSION |
| 1306 |
"""The terminus is orphaned when the parent session is closed""" |
| 1307 |
EXPIRE_WITH_CONNECTION = PN_EXPIRE_WITH_CONNECTION |
| 1308 |
"""The terminus is orphaned when the parent connection is closed""" |
| 1309 |
EXPIRE_NEVER = PN_EXPIRE_NEVER |
| 1310 |
"""The terminus is never considered orphaned""" |
| 1311 |
|
| 1312 |
def __init__(self, impl): |
| 1313 |
self._impl = impl |
| 1314 |
|
| 1315 |
def _check(self, err: int) -> int: |
| 1316 |
if err < 0: |
| 1317 |
exc = EXCEPTIONS.get(err, LinkException) |
| 1318 |
raise exc("[%s]" % err) |
| 1319 |
else: |
| 1320 |
return err |
| 1321 |
|
| 1322 |
@property |
| 1323 |
def type(self) -> int: |
| 1324 |
"""The terminus type, must be one of :const:`UNSPECIFIED`, |
| 1325 |
:const:`SOURCE`, :const:`TARGET` or :const:`COORDINATOR`. |
| 1326 |
""" |
| 1327 |
return pn_terminus_get_type(self._impl) |
| 1328 |
|
| 1329 |
@type.setter |
| 1330 |
def type(self, type: int) -> None: |
| 1331 |
self._check(pn_terminus_set_type(self._impl, type)) |
| 1332 |
|
| 1333 |
@property |
| 1334 |
def address(self) -> Optional[str]: |
| 1335 |
"""The address that identifies the source or target node""" |
| 1336 |
return utf82unicode(pn_terminus_get_address(self._impl)) |
| 1337 |
|
| 1338 |
@address.setter |
| 1339 |
def address(self, address: str) -> None: |
| 1340 |
self._check(pn_terminus_set_address(self._impl, unicode2utf8(address))) |
| 1341 |
|
| 1342 |
@property |
| 1343 |
def durability(self) -> int: |
| 1344 |
"""The terminus durability mode, must be one of :const:`NONDURABLE`, |
| 1345 |
:const:`CONFIGURATION` or :const:`DELIVERIES`. |
| 1346 |
""" |
| 1347 |
return pn_terminus_get_durability(self._impl) |
| 1348 |
|
| 1349 |
@durability.setter |
| 1350 |
def durability(self, mode: int): |
| 1351 |
self._check(pn_terminus_set_durability(self._impl, mode)) |
| 1352 |
|
| 1353 |
@property |
| 1354 |
def expiry_policy(self) -> int: |
| 1355 |
"""The terminus expiry policy, must be one of :const:`EXPIRE_WITH_LINK`, |
| 1356 |
:const:`EXPIRE_WITH_SESSION`, :const:`EXPIRE_WITH_CONNECTION` or |
| 1357 |
:const:`EXPIRE_NEVER`. |
| 1358 |
""" |
| 1359 |
return pn_terminus_get_expiry_policy(self._impl) |
| 1360 |
|
| 1361 |
@expiry_policy.setter |
| 1362 |
def expiry_policy(self, policy: int): |
| 1363 |
self._check(pn_terminus_set_expiry_policy(self._impl, policy)) |
| 1364 |
|
| 1365 |
@property |
| 1366 |
def timeout(self) -> int: |
| 1367 |
"""The terminus timeout in seconds.""" |
| 1368 |
return pn_terminus_get_timeout(self._impl) |
| 1369 |
|
| 1370 |
@timeout.setter |
| 1371 |
def timeout(self, seconds: int) -> None: |
| 1372 |
self._check(pn_terminus_set_timeout(self._impl, seconds)) |
| 1373 |
|
| 1374 |
@property |
| 1375 |
def dynamic(self) -> bool: |
| 1376 |
"""Indicates whether the source or target node was dynamically |
| 1377 |
created""" |
| 1378 |
return pn_terminus_is_dynamic(self._impl) |
| 1379 |
|
| 1380 |
@dynamic.setter |
| 1381 |
def dynamic(self, dynamic: bool) -> None: |
| 1382 |
self._check(pn_terminus_set_dynamic(self._impl, dynamic)) |
| 1383 |
|
| 1384 |
@property |
| 1385 |
def distribution_mode(self) -> int: |
| 1386 |
"""The terminus distribution mode, must be one of :const:`DIST_MODE_UNSPECIFIED`, |
| 1387 |
:const:`DIST_MODE_COPY` or :const:`DIST_MODE_MOVE`. |
| 1388 |
""" |
| 1389 |
return pn_terminus_get_distribution_mode(self._impl) |
| 1390 |
|
| 1391 |
@distribution_mode.setter |
| 1392 |
def distribution_mode(self, mode: int) -> None: |
| 1393 |
self._check(pn_terminus_set_distribution_mode(self._impl, mode)) |
| 1394 |
|
| 1395 |
@property |
| 1396 |
def properties(self): |
| 1397 |
""" |
| 1398 |
Properties of a dynamic source or target. |
| 1399 |
|
| 1400 |
:type: :class:`Data` containing a map with :class:`symbol` keys. |
| 1401 |
""" |
| 1402 |
return Data(pn_terminus_properties(self._impl)) |
| 1403 |
|
| 1404 |
@property |
| 1405 |
def capabilities(self): |
| 1406 |
""" |
| 1407 |
Capabilities of the source or target. |
| 1408 |
|
| 1409 |
:type: :class:`Data` containing an array of :class:`symbol`. |
| 1410 |
""" |
| 1411 |
return Data(pn_terminus_capabilities(self._impl)) |
| 1412 |
|
| 1413 |
@property |
| 1414 |
def outcomes(self): |
| 1415 |
""" |
| 1416 |
Outcomes of the source or target. |
| 1417 |
|
| 1418 |
:type: :class:`Data` containing an array of :class:`symbol`. |
| 1419 |
""" |
| 1420 |
return Data(pn_terminus_outcomes(self._impl)) |
| 1421 |
|
| 1422 |
@property |
| 1423 |
def filter(self): |
| 1424 |
""" |
| 1425 |
A filter on a source allows the set of messages transferred over |
| 1426 |
the link to be restricted. The symbol-keyed map represents a' |
| 1427 |
filter set. |
| 1428 |
|
| 1429 |
:type: :class:`Data` containing a map with :class:`symbol` keys. |
| 1430 |
""" |
| 1431 |
return Data(pn_terminus_filter(self._impl)) |
| 1432 |
|
| 1433 |
def copy(self, src: 'Terminus') -> None: |
| 1434 |
""" |
| 1435 |
Copy another terminus object. |
| 1436 |
|
| 1437 |
:param src: The terminus to be copied from |
| 1438 |
:raises: :class:`LinkException` if there is an error |
| 1439 |
""" |
| 1440 |
self._check(pn_terminus_copy(self._impl, src._impl)) |