| 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 |
from cproton import PN_ACCEPTED, PN_MODIFIED, PN_RECEIVED, PN_REJECTED, PN_RELEASED, pn_delivery_abort, \ |
| 21 |
pn_delivery_aborted, pn_delivery_attachments, pn_delivery_link, pn_delivery_local, pn_delivery_local_state, \ |
| 22 |
pn_delivery_partial, pn_delivery_pending, pn_delivery_readable, pn_delivery_remote, pn_delivery_remote_state, \ |
| 23 |
pn_delivery_settle, pn_delivery_settled, pn_delivery_tag, pn_delivery_update, pn_delivery_updated, \ |
| 24 |
pn_delivery_writable, pn_disposition_annotations, pn_disposition_condition, pn_disposition_data, \ |
| 25 |
pn_disposition_get_section_number, pn_disposition_get_section_offset, pn_disposition_is_failed, \ |
| 26 |
pn_disposition_is_undeliverable, pn_disposition_set_failed, pn_disposition_set_section_number, \ |
| 27 |
pn_disposition_set_section_offset, pn_disposition_set_undeliverable, pn_disposition_type, pn_work_next |
| 28 |
|
| 29 |
from ._condition import cond2obj, obj2cond |
| 30 |
from ._data import dat2obj, obj2dat |
| 31 |
from ._wrapper import Wrapper |
| 32 |
|
| 33 |
from typing import Dict, List, Optional, Type, Union, TYPE_CHECKING, Any |
| 34 |
|
| 35 |
if TYPE_CHECKING: |
| 36 |
from ._condition import Condition |
| 37 |
from ._data import PythonAMQPData, symbol |
| 38 |
from ._endpoints import Receiver, Sender # circular import |
| 39 |
from ._reactor import Connection, Session, Transport |
| 40 |
|
| 41 |
|
| 42 |
class NamedInt(int): |
| 43 |
values: Dict[int, 'DispositionType'] = {} |
| 44 |
|
| 45 |
def __new__(cls: Type['DispositionType'], i: int, name: str) -> 'DispositionType': |
| 46 |
ni = super(NamedInt, cls).__new__(cls, i) |
| 47 |
cls.values[i] = ni |
| 48 |
return ni |
| 49 |
|
| 50 |
def __init__(self, i: int, name: str) -> None: |
| 51 |
self.name = name |
| 52 |
|
| 53 |
def __repr__(self) -> str: |
| 54 |
return self.name |
| 55 |
|
| 56 |
def __str__(self) -> str: |
| 57 |
return self.name |
| 58 |
|
| 59 |
@classmethod |
| 60 |
def get(cls, i: int) -> Union[int, 'DispositionType']: |
| 61 |
return cls.values.get(i, i) |
| 62 |
|
| 63 |
|
| 64 |
class DispositionType(NamedInt): |
| 65 |
values = {} |
| 66 |
|
| 67 |
|
| 68 |
class Disposition(object): |
| 69 |
""" |
| 70 |
A delivery state. |
| 71 |
|
| 72 |
Dispositions record the current state or final outcome of a |
| 73 |
transfer. Every delivery contains both a local and remote |
| 74 |
disposition. The local disposition holds the local state of the |
| 75 |
delivery, and the remote disposition holds the last known remote |
| 76 |
state of the delivery. |
| 77 |
""" |
| 78 |
|
| 79 |
RECEIVED = DispositionType(PN_RECEIVED, "RECEIVED") |
| 80 |
""" |
| 81 |
A non terminal state indicating how much (if any) message data |
| 82 |
has been received for a delivery. |
| 83 |
""" |
| 84 |
|
| 85 |
ACCEPTED = DispositionType(PN_ACCEPTED, "ACCEPTED") |
| 86 |
""" |
| 87 |
A terminal state indicating that the delivery was successfully |
| 88 |
processed. Once in this state there will be no further state |
| 89 |
changes prior to the delivery being settled. |
| 90 |
""" |
| 91 |
|
| 92 |
REJECTED = DispositionType(PN_REJECTED, "REJECTED") |
| 93 |
""" |
| 94 |
A terminal state indicating that the delivery could not be |
| 95 |
processed due to some error condition. Once in this state |
| 96 |
there will be no further state changes prior to the delivery |
| 97 |
being settled. |
| 98 |
""" |
| 99 |
|
| 100 |
RELEASED = DispositionType(PN_RELEASED, "RELEASED") |
| 101 |
""" |
| 102 |
A terminal state indicating that the delivery is being |
| 103 |
returned to the sender. Once in this state there will be no |
| 104 |
further state changes prior to the delivery being settled. |
| 105 |
""" |
| 106 |
|
| 107 |
MODIFIED = DispositionType(PN_MODIFIED, "MODIFIED") |
| 108 |
""" |
| 109 |
A terminal state indicating that the delivery is being |
| 110 |
returned to the sender and should be annotated by the |
| 111 |
sender prior to further delivery attempts. Once in this |
| 112 |
state there will be no further state changes prior to the |
| 113 |
delivery being settled. |
| 114 |
""" |
| 115 |
|
| 116 |
def __init__(self, impl, local): |
| 117 |
self._impl = impl |
| 118 |
self.local = local |
| 119 |
self._data = None |
| 120 |
self._condition = None |
| 121 |
self._annotations = None |
| 122 |
|
| 123 |
@property |
| 124 |
def type(self) -> Union[int, DispositionType]: |
| 125 |
""" |
| 126 |
Get the type of this disposition object. |
| 127 |
|
| 128 |
Defined values are: |
| 129 |
|
| 130 |
* :const:`RECEIVED` |
| 131 |
* :const:`ACCEPTED` |
| 132 |
* :const:`REJECTED` |
| 133 |
* :const:`RELEASED` |
| 134 |
* :const:`MODIFIED` |
| 135 |
""" |
| 136 |
return DispositionType.get(pn_disposition_type(self._impl)) |
| 137 |
|
| 138 |
@property |
| 139 |
def section_number(self) -> int: |
| 140 |
"""The section number associated with a disposition.""" |
| 141 |
return pn_disposition_get_section_number(self._impl) |
| 142 |
|
| 143 |
@section_number.setter |
| 144 |
def section_number(self, n: int) -> None: |
| 145 |
pn_disposition_set_section_number(self._impl, n) |
| 146 |
|
| 147 |
@property |
| 148 |
def section_offset(self) -> int: |
| 149 |
"""The section offset associated with a disposition.""" |
| 150 |
return pn_disposition_get_section_offset(self._impl) |
| 151 |
|
| 152 |
@section_offset.setter |
| 153 |
def section_offset(self, n: int) -> None: |
| 154 |
pn_disposition_set_section_offset(self._impl, n) |
| 155 |
|
| 156 |
@property |
| 157 |
def failed(self) -> bool: |
| 158 |
"""The failed flag for this disposition.""" |
| 159 |
return pn_disposition_is_failed(self._impl) |
| 160 |
|
| 161 |
@failed.setter |
| 162 |
def failed(self, b: bool) -> None: |
| 163 |
pn_disposition_set_failed(self._impl, b) |
| 164 |
|
| 165 |
@property |
| 166 |
def undeliverable(self) -> bool: |
| 167 |
"""The undeliverable flag for this disposition.""" |
| 168 |
return pn_disposition_is_undeliverable(self._impl) |
| 169 |
|
| 170 |
@undeliverable.setter |
| 171 |
def undeliverable(self, b: bool) -> None: |
| 172 |
pn_disposition_set_undeliverable(self._impl, b) |
| 173 |
|
| 174 |
@property |
| 175 |
def data(self) -> Optional[List[int]]: |
| 176 |
"""Access the disposition as a :class:`Data` object. |
| 177 |
|
| 178 |
Dispositions are an extension point in the AMQP protocol. The |
| 179 |
disposition interface provides setters/getters for those |
| 180 |
dispositions that are predefined by the specification, however |
| 181 |
access to the raw disposition data is provided so that other |
| 182 |
dispositions can be used. |
| 183 |
|
| 184 |
The :class:`Data` object returned by this operation is valid until |
| 185 |
the parent delivery is settled. |
| 186 |
""" |
| 187 |
if self.local: |
| 188 |
return self._data |
| 189 |
else: |
| 190 |
return dat2obj(pn_disposition_data(self._impl)) |
| 191 |
|
| 192 |
@data.setter |
| 193 |
def data(self, obj: List[int]) -> None: |
| 194 |
if self.local: |
| 195 |
self._data = obj |
| 196 |
else: |
| 197 |
raise AttributeError("data attribute is read-only") |
| 198 |
|
| 199 |
@property |
| 200 |
def annotations(self) -> Optional[Dict['symbol', 'PythonAMQPData']]: |
| 201 |
"""The annotations associated with a disposition. |
| 202 |
|
| 203 |
The :class:`Data` object retrieved by this operation may be modified |
| 204 |
prior to updating a delivery. When a delivery is updated, the |
| 205 |
annotations described by the :class:`Data` are reported to the peer |
| 206 |
if applicable to the current delivery state, e.g. states such as |
| 207 |
:const:`MODIFIED`. The :class:`Data` must be empty or contain a symbol |
| 208 |
keyed map. |
| 209 |
|
| 210 |
The :class:`Data` object returned by this operation is valid until |
| 211 |
the parent delivery is settled. |
| 212 |
""" |
| 213 |
if self.local: |
| 214 |
return self._annotations |
| 215 |
else: |
| 216 |
return dat2obj(pn_disposition_annotations(self._impl)) |
| 217 |
|
| 218 |
@annotations.setter |
| 219 |
def annotations(self, obj: Dict[str, 'PythonAMQPData']) -> None: |
| 220 |
if self.local: |
| 221 |
self._annotations = obj |
| 222 |
else: |
| 223 |
raise AttributeError("annotations attribute is read-only") |
| 224 |
|
| 225 |
@property |
| 226 |
def condition(self) -> Optional['Condition']: |
| 227 |
"""The condition object associated with a disposition. |
| 228 |
|
| 229 |
The :class:`Condition` object retrieved by this operation may be |
| 230 |
modified prior to updating a delivery. When a delivery is updated, |
| 231 |
the condition described by the disposition is reported to the peer |
| 232 |
if applicable to the current delivery state, e.g. states such as |
| 233 |
:const:`REJECTED`. |
| 234 |
""" |
| 235 |
if self.local: |
| 236 |
return self._condition |
| 237 |
else: |
| 238 |
return cond2obj(pn_disposition_condition(self._impl)) |
| 239 |
|
| 240 |
@condition.setter |
| 241 |
def condition(self, obj: 'Condition') -> None: |
| 242 |
if self.local: |
| 243 |
self._condition = obj |
| 244 |
else: |
| 245 |
raise AttributeError("condition attribute is read-only") |
| 246 |
|
| 247 |
|
| 248 |
class Delivery(Wrapper): |
| 249 |
""" |
| 250 |
Tracks and/or records the delivery of a message over a link. |
| 251 |
""" |
| 252 |
|
| 253 |
RECEIVED = Disposition.RECEIVED |
| 254 |
""" |
| 255 |
A non terminal state indicating how much (if any) message data |
| 256 |
has been received for a delivery. |
| 257 |
""" |
| 258 |
|
| 259 |
ACCEPTED = Disposition.ACCEPTED |
| 260 |
""" |
| 261 |
A terminal state indicating that the delivery was successfully |
| 262 |
processed. Once in this state there will be no further state |
| 263 |
changes prior to the delivery being settled. |
| 264 |
""" |
| 265 |
|
| 266 |
REJECTED = Disposition.REJECTED |
| 267 |
""" |
| 268 |
A terminal state indicating that the delivery could not be |
| 269 |
processed due to some error condition. Once in this state |
| 270 |
there will be no further state changes prior to the delivery |
| 271 |
being settled. |
| 272 |
""" |
| 273 |
|
| 274 |
RELEASED = Disposition.RELEASED |
| 275 |
""" |
| 276 |
A terminal state indicating that the delivery is being |
| 277 |
returned to the sender. Once in this state there will be no |
| 278 |
further state changes prior to the delivery being settled. |
| 279 |
""" |
| 280 |
|
| 281 |
MODIFIED = Disposition.MODIFIED |
| 282 |
""" |
| 283 |
A terminal state indicating that the delivery is being |
| 284 |
returned to the sender and should be annotated by the |
| 285 |
sender prior to further delivery attempts. Once in this |
| 286 |
state there will be no further state changes prior to the |
| 287 |
delivery being settled. |
| 288 |
""" |
| 289 |
|
| 290 |
@staticmethod |
| 291 |
def wrap(impl): |
| 292 |
if impl is None: |
| 293 |
return None |
| 294 |
else: |
| 295 |
return Delivery(impl) |
| 296 |
|
| 297 |
def __init__(self, impl): |
| 298 |
Wrapper.__init__(self, impl, pn_delivery_attachments) |
| 299 |
|
| 300 |
def _init(self) -> None: |
| 301 |
self.local = Disposition(pn_delivery_local(self._impl), True) |
| 302 |
self.remote = Disposition(pn_delivery_remote(self._impl), False) |
| 303 |
|
| 304 |
@property |
| 305 |
def tag(self) -> str: |
| 306 |
""" |
| 307 |
The identifier for the delivery. |
| 308 |
""" |
| 309 |
return pn_delivery_tag(self._impl) |
| 310 |
|
| 311 |
@property |
| 312 |
def writable(self) -> bool: |
| 313 |
""" |
| 314 |
``True`` for an outgoing delivery to which data can now be written, |
| 315 |
``False`` otherwise.. |
| 316 |
""" |
| 317 |
return pn_delivery_writable(self._impl) |
| 318 |
|
| 319 |
@property |
| 320 |
def readable(self) -> bool: |
| 321 |
""" |
| 322 |
``True`` for an incoming delivery that has data to read, |
| 323 |
``False`` otherwise.. |
| 324 |
""" |
| 325 |
return pn_delivery_readable(self._impl) |
| 326 |
|
| 327 |
@property |
| 328 |
def updated(self) -> bool: |
| 329 |
""" |
| 330 |
``True`` if the state of the delivery has been updated |
| 331 |
(e.g. it has been settled and/or accepted, rejected etc), |
| 332 |
``False`` otherwise. |
| 333 |
""" |
| 334 |
return pn_delivery_updated(self._impl) |
| 335 |
|
| 336 |
def update(self, state: Union[int, DispositionType]) -> None: |
| 337 |
""" |
| 338 |
Set the local state of the delivery e.g. :const:`ACCEPTED`, |
| 339 |
:const:`REJECTED`, :const:`RELEASED`. |
| 340 |
|
| 341 |
:param state: State of delivery |
| 342 |
""" |
| 343 |
obj2dat(self.local._data, pn_disposition_data(self.local._impl)) |
| 344 |
obj2dat(self.local._annotations, pn_disposition_annotations(self.local._impl)) |
| 345 |
obj2cond(self.local._condition, pn_disposition_condition(self.local._impl)) |
| 346 |
pn_delivery_update(self._impl, state) |
| 347 |
|
| 348 |
@property |
| 349 |
def pending(self) -> int: |
| 350 |
""" |
| 351 |
The amount of pending message data for a delivery. |
| 352 |
""" |
| 353 |
return pn_delivery_pending(self._impl) |
| 354 |
|
| 355 |
@property |
| 356 |
def partial(self) -> bool: |
| 357 |
""" |
| 358 |
``True`` for an incoming delivery if not all the data is |
| 359 |
yet available, ``False`` otherwise. |
| 360 |
""" |
| 361 |
return pn_delivery_partial(self._impl) |
| 362 |
|
| 363 |
@property |
| 364 |
def local_state(self) -> DispositionType: |
| 365 |
"""A local state of the delivery.""" |
| 366 |
return DispositionType.get(pn_delivery_local_state(self._impl)) |
| 367 |
|
| 368 |
@property |
| 369 |
def remote_state(self) -> Union[int, DispositionType]: |
| 370 |
"""A remote state of the delivery as indicated by the remote peer.""" |
| 371 |
return DispositionType.get(pn_delivery_remote_state(self._impl)) |
| 372 |
|
| 373 |
@property |
| 374 |
def settled(self) -> bool: |
| 375 |
""" |
| 376 |
``True`` if the delivery has been settled by the remote peer, |
| 377 |
``False`` otherwise. |
| 378 |
""" |
| 379 |
return pn_delivery_settled(self._impl) |
| 380 |
|
| 381 |
def settle(self) -> None: |
| 382 |
""" |
| 383 |
Settles the delivery locally. This indicates the application |
| 384 |
considers the delivery complete and does not wish to receive any |
| 385 |
further events about it. Every delivery should be settled locally. |
| 386 |
""" |
| 387 |
pn_delivery_settle(self._impl) |
| 388 |
|
| 389 |
@property |
| 390 |
def aborted(self) -> bool: |
| 391 |
""" |
| 392 |
``True`` if the delivery has been aborted, ``False`` otherwise. |
| 393 |
""" |
| 394 |
return pn_delivery_aborted(self._impl) |
| 395 |
|
| 396 |
def abort(self) -> None: |
| 397 |
""" |
| 398 |
Aborts the delivery. This indicates the application wishes to |
| 399 |
invalidate any data that may have already been sent on this delivery. |
| 400 |
The delivery cannot be aborted after it has been completely delivered. |
| 401 |
""" |
| 402 |
pn_delivery_abort(self._impl) |
| 403 |
|
| 404 |
@property |
| 405 |
def work_next(self) -> Optional['Delivery']: |
| 406 |
"""Deprecated: use on_message(), on_accepted(), on_rejected(), |
| 407 |
on_released(), and on_settled() instead. |
| 408 |
|
| 409 |
The next :class:`Delivery` on the connection that has pending |
| 410 |
operations. |
| 411 |
""" |
| 412 |
return Delivery.wrap(pn_work_next(self._impl)) |
| 413 |
|
| 414 |
@property |
| 415 |
def link(self) -> Union['Receiver', 'Sender']: |
| 416 |
""" |
| 417 |
The :class:`Link` on which the delivery was sent or received. |
| 418 |
""" |
| 419 |
from . import _endpoints |
| 420 |
return _endpoints.Link.wrap(pn_delivery_link(self._impl)) |
| 421 |
|
| 422 |
@property |
| 423 |
def session(self) -> 'Session': |
| 424 |
""" |
| 425 |
The :class:`Session` over which the delivery was sent or received. |
| 426 |
""" |
| 427 |
return self.link.session |
| 428 |
|
| 429 |
@property |
| 430 |
def connection(self) -> 'Connection': |
| 431 |
""" |
| 432 |
The :class:`Connection` over which the delivery was sent or received. |
| 433 |
""" |
| 434 |
return self.session.connection |
| 435 |
|
| 436 |
@property |
| 437 |
def transport(self) -> 'Transport': |
| 438 |
""" |
| 439 |
The :class:`Transport` bound to the :class:`Connection` over which |
| 440 |
the delivery was sent or received. |
| 441 |
""" |
| 442 |
return self.connection.transport |