| 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 |
import errno |
| 21 |
import logging |
| 22 |
import socket |
| 23 |
import time |
| 24 |
import weakref |
| 25 |
|
| 26 |
from ._condition import Condition |
| 27 |
from ._delivery import Delivery |
| 28 |
from ._endpoints import Endpoint |
| 29 |
from ._events import Event, Handler, _dispatch |
| 30 |
from ._exceptions import ProtonException |
| 31 |
from ._io import IO |
| 32 |
from ._message import Message |
| 33 |
from ._selectable import Selectable |
| 34 |
from ._transport import Transport |
| 35 |
from ._url import Url |
| 36 |
from typing import Any, List, Optional, Tuple, Union, TYPE_CHECKING, TypeVar |
| 37 |
|
| 38 |
if TYPE_CHECKING: |
| 39 |
from ._delivery import DispositionType |
| 40 |
from ._reactor import Container, Transaction |
| 41 |
from ._endpoints import Sender, Receiver |
| 42 |
|
| 43 |
log = logging.getLogger("proton") |
| 44 |
|
| 45 |
|
| 46 |
class OutgoingMessageHandler(Handler): |
| 47 |
""" |
| 48 |
A utility for simpler and more intuitive handling of delivery |
| 49 |
events related to outgoing i.e. sent messages. |
| 50 |
|
| 51 |
:param auto_settle: If ``True`` (default), automatically settle messages |
| 52 |
upon receiving a settled disposition for that delivery. Otherwise |
| 53 |
messages must be explicitly settled. |
| 54 |
:type auto_settle: ``bool`` |
| 55 |
:param delegate: A client handler for the endpoint event |
| 56 |
""" |
| 57 |
|
| 58 |
def __init__(self, auto_settle=True, delegate=None): |
| 59 |
self.auto_settle = auto_settle |
| 60 |
self.delegate = delegate |
| 61 |
|
| 62 |
def on_link_flow(self, event: Event): |
| 63 |
if event.link.is_sender and event.link.credit \ |
| 64 |
and event.link.state & Endpoint.LOCAL_ACTIVE \ |
| 65 |
and event.link.state & Endpoint.REMOTE_ACTIVE: |
| 66 |
self.on_sendable(event) |
| 67 |
|
| 68 |
def on_delivery(self, event: Event): |
| 69 |
dlv = event.delivery |
| 70 |
if dlv.link.is_sender and dlv.updated: |
| 71 |
if dlv.remote_state == Delivery.ACCEPTED: |
| 72 |
self.on_accepted(event) |
| 73 |
elif dlv.remote_state == Delivery.REJECTED: |
| 74 |
self.on_rejected(event) |
| 75 |
elif dlv.remote_state == Delivery.RELEASED or dlv.remote_state == Delivery.MODIFIED: |
| 76 |
self.on_released(event) |
| 77 |
if dlv.settled: |
| 78 |
self.on_settled(event) |
| 79 |
if self.auto_settle: |
| 80 |
dlv.settle() |
| 81 |
|
| 82 |
def on_sendable(self, event: Event): |
| 83 |
""" |
| 84 |
Called when the sender link has credit and messages can |
| 85 |
therefore be transferred. |
| 86 |
|
| 87 |
:param event: The underlying event object. Use this to obtain further |
| 88 |
information on the event. |
| 89 |
""" |
| 90 |
if self.delegate is not None: |
| 91 |
_dispatch(self.delegate, 'on_sendable', event) |
| 92 |
|
| 93 |
def on_accepted(self, event: Event): |
| 94 |
""" |
| 95 |
Called when the remote peer accepts an outgoing message. |
| 96 |
|
| 97 |
:param event: The underlying event object. Use this to obtain further |
| 98 |
information on the event. |
| 99 |
""" |
| 100 |
if self.delegate is not None: |
| 101 |
_dispatch(self.delegate, 'on_accepted', event) |
| 102 |
|
| 103 |
def on_rejected(self, event: Event): |
| 104 |
""" |
| 105 |
Called when the remote peer rejects an outgoing message. |
| 106 |
|
| 107 |
:param event: The underlying event object. Use this to obtain further |
| 108 |
information on the event. |
| 109 |
""" |
| 110 |
if self.delegate is not None: |
| 111 |
_dispatch(self.delegate, 'on_rejected', event) |
| 112 |
|
| 113 |
def on_released(self, event: Event): |
| 114 |
""" |
| 115 |
Called when the remote peer releases an outgoing message. Note |
| 116 |
that this may be in response to either the ``RELEASE`` or ``MODIFIED`` |
| 117 |
state as defined by the AMQP specification. |
| 118 |
|
| 119 |
:param event: The underlying event object. Use this to obtain further |
| 120 |
information on the event. |
| 121 |
""" |
| 122 |
if self.delegate is not None: |
| 123 |
_dispatch(self.delegate, 'on_released', event) |
| 124 |
|
| 125 |
def on_settled(self, event: Event): |
| 126 |
""" |
| 127 |
Called when the remote peer has settled the outgoing |
| 128 |
message. This is the point at which it should never be |
| 129 |
retransmitted. |
| 130 |
|
| 131 |
:param event: The underlying event object. Use this to obtain further |
| 132 |
information on the event. |
| 133 |
""" |
| 134 |
if self.delegate is not None: |
| 135 |
_dispatch(self.delegate, 'on_settled', event) |
| 136 |
|
| 137 |
|
| 138 |
def recv_msg(delivery: Delivery) -> Message: |
| 139 |
msg = Message() |
| 140 |
msg.decode(delivery.link.recv(delivery.pending)) |
| 141 |
delivery.link.advance() |
| 142 |
return msg |
| 143 |
|
| 144 |
|
| 145 |
class Reject(ProtonException): |
| 146 |
""" |
| 147 |
An exception that indicates a message should be rejected. |
| 148 |
""" |
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
class Release(ProtonException): |
| 153 |
""" |
| 154 |
An exception that indicates a message should be released. |
| 155 |
""" |
| 156 |
pass |
| 157 |
|
| 158 |
|
| 159 |
class Acking(object): |
| 160 |
""" |
| 161 |
A class containing methods for handling received messages. |
| 162 |
""" |
| 163 |
|
| 164 |
def accept(self, delivery: Delivery) -> None: |
| 165 |
""" |
| 166 |
Accepts a received message. |
| 167 |
|
| 168 |
.. note:: This method cannot currently be used in combination |
| 169 |
with transactions. See :class:`proton.reactor.Transaction` |
| 170 |
for transactional methods. |
| 171 |
|
| 172 |
:param delivery: The message delivery tracking object |
| 173 |
""" |
| 174 |
self.settle(delivery, Delivery.ACCEPTED) |
| 175 |
|
| 176 |
def reject(self, delivery: Delivery) -> None: |
| 177 |
""" |
| 178 |
Rejects a received message that is considered invalid or |
| 179 |
unprocessable. |
| 180 |
|
| 181 |
.. note:: This method cannot currently be used in combination |
| 182 |
with transactions. See :class:`proton.reactor.Transaction` |
| 183 |
for transactional methods. |
| 184 |
|
| 185 |
:param delivery: The message delivery tracking object |
| 186 |
""" |
| 187 |
self.settle(delivery, Delivery.REJECTED) |
| 188 |
|
| 189 |
def release(self, delivery: Delivery, delivered: bool = True) -> None: |
| 190 |
""" |
| 191 |
Releases a received message, making it available at the source |
| 192 |
for any (other) interested receiver. The ``delivered`` |
| 193 |
parameter indicates whether this should be considered a |
| 194 |
delivery attempt (and the delivery count updated) or not. |
| 195 |
|
| 196 |
.. note:: This method cannot currently be used in combination |
| 197 |
with transactions. See :class:`proton.reactor.Transaction` |
| 198 |
for transactional methods. |
| 199 |
|
| 200 |
:param delivery: The message delivery tracking object |
| 201 |
:param delivered: If ``True``, the message will be annotated |
| 202 |
with a delivery attempt (setting delivery flag |
| 203 |
:const:`proton.Delivery.MODIFIED`). Otherwise, the message |
| 204 |
will be returned without the annotation and released (setting |
| 205 |
delivery flag :const:`proton.Delivery.RELEASED` |
| 206 |
""" |
| 207 |
if delivered: |
| 208 |
self.settle(delivery, Delivery.MODIFIED) |
| 209 |
else: |
| 210 |
self.settle(delivery, Delivery.RELEASED) |
| 211 |
|
| 212 |
def settle(self, delivery: Delivery, state: Optional['DispositionType'] = None) -> None: |
| 213 |
""" |
| 214 |
Settles the message delivery, and optionally updating the |
| 215 |
delivery state. |
| 216 |
|
| 217 |
:param delivery: The message delivery tracking object |
| 218 |
:param state: The delivery state, or ``None`` if no update |
| 219 |
is to be performed. |
| 220 |
""" |
| 221 |
if state: |
| 222 |
delivery.update(state) |
| 223 |
delivery.settle() |
| 224 |
|
| 225 |
|
| 226 |
class IncomingMessageHandler(Handler, Acking): |
| 227 |
""" |
| 228 |
A utility for simpler and more intuitive handling of delivery |
| 229 |
events related to incoming i.e. received messages. |
| 230 |
|
| 231 |
:param auto_accept: If ``True``, accept all messages (default). Otherwise |
| 232 |
messages must be individually accepted or rejected. |
| 233 |
:param delegate: A client handler for the endpoint event |
| 234 |
""" |
| 235 |
|
| 236 |
def __init__(self, auto_accept: bool = True, delegate: Optional[Handler] = None) -> None: |
| 237 |
self.delegate = delegate |
| 238 |
self.auto_accept = auto_accept |
| 239 |
|
| 240 |
def on_delivery(self, event: Event) -> None: |
| 241 |
dlv = event.delivery |
| 242 |
if not dlv.link.is_receiver: |
| 243 |
return |
| 244 |
if dlv.aborted: |
| 245 |
self.on_aborted(event) |
| 246 |
dlv.settle() |
| 247 |
elif dlv.readable and not dlv.partial: |
| 248 |
event.message = recv_msg(dlv) |
| 249 |
if event.link.state & Endpoint.LOCAL_CLOSED: |
| 250 |
if self.auto_accept: |
| 251 |
dlv.update(Delivery.RELEASED) |
| 252 |
dlv.settle() |
| 253 |
else: |
| 254 |
try: |
| 255 |
self.on_message(event) |
| 256 |
if self.auto_accept: |
| 257 |
dlv.update(Delivery.ACCEPTED) |
| 258 |
dlv.settle() |
| 259 |
except Reject: |
| 260 |
dlv.update(Delivery.REJECTED) |
| 261 |
dlv.settle() |
| 262 |
except Release: |
| 263 |
dlv.update(Delivery.MODIFIED) |
| 264 |
dlv.settle() |
| 265 |
elif dlv.updated and dlv.settled: |
| 266 |
self.on_settled(event) |
| 267 |
|
| 268 |
def on_message(self, event: Event): |
| 269 |
""" |
| 270 |
Called when a message is received. The message itself can be |
| 271 |
obtained as a property on the event. For the purpose of |
| 272 |
referring to this message in further actions (e.g. if |
| 273 |
explicitly accepting it, the ``delivery`` should be used, also |
| 274 |
obtainable via a property on the event. |
| 275 |
|
| 276 |
:param event: The underlying event object. Use this to obtain further |
| 277 |
information on the event. |
| 278 |
""" |
| 279 |
if self.delegate is not None: |
| 280 |
_dispatch(self.delegate, 'on_message', event) |
| 281 |
|
| 282 |
def on_settled(self, event: Event): |
| 283 |
""" |
| 284 |
Callback for when a message delivery is settled by the remote peer. |
| 285 |
|
| 286 |
:param event: The underlying event object. Use this to obtain further |
| 287 |
information on the event. |
| 288 |
""" |
| 289 |
if self.delegate is not None: |
| 290 |
_dispatch(self.delegate, 'on_settled', event) |
| 291 |
|
| 292 |
def on_aborted(self, event: Event): |
| 293 |
""" |
| 294 |
Callback for when a message delivery is aborted by the remote peer. |
| 295 |
|
| 296 |
:param event: The underlying event object. Use this to obtain further |
| 297 |
information on the event. |
| 298 |
""" |
| 299 |
if self.delegate is not None: |
| 300 |
_dispatch(self.delegate, 'on_aborted', event) |
| 301 |
|
| 302 |
|
| 303 |
class EndpointStateHandler(Handler): |
| 304 |
""" |
| 305 |
A utility that exposes 'endpoint' events - ie the open/close for |
| 306 |
links, sessions and connections in a more intuitive manner. A |
| 307 |
``XXX_opened()`` method will be called when both local and remote peers |
| 308 |
have opened the link, session or connection. This can be used to |
| 309 |
confirm a locally initiated action for example. A ``XXX_opening()`` |
| 310 |
method will be called when the remote peer has requested an open |
| 311 |
that was not initiated locally. By default this will simply open |
| 312 |
locally, which then triggers the ``XXX_opened()`` call. The same applies |
| 313 |
to close. |
| 314 |
|
| 315 |
:param peer_close_is_error: If ``True``, a peer endpoint closing will be |
| 316 |
treated as an error with an error callback. Otherwise (default), the |
| 317 |
normal callbacks for the closing will occur. |
| 318 |
:param delegate: A client handler for the endpoint event |
| 319 |
""" |
| 320 |
|
| 321 |
def __init__(self, peer_close_is_error: bool = False, delegate: Optional[Handler] = None) -> None: |
| 322 |
self.delegate = delegate |
| 323 |
self.peer_close_is_error = peer_close_is_error |
| 324 |
|
| 325 |
@classmethod |
| 326 |
def is_local_open(cls, endpoint: Endpoint) -> bool: |
| 327 |
""" |
| 328 |
Test if local ``endpoint`` is open (ie has state |
| 329 |
:const:`proton.Endpoint.LOCAL_ACTIVE`). |
| 330 |
|
| 331 |
:param endpoint: The local endpoint to be tested. |
| 332 |
:return: ``True`` if local endpoint is in state |
| 333 |
:const:`proton.Endpoint.LOCAL_ACTIVE`, ``False`` otherwise. |
| 334 |
""" |
| 335 |
return bool(endpoint.state & Endpoint.LOCAL_ACTIVE) |
| 336 |
|
| 337 |
@classmethod |
| 338 |
def is_local_uninitialised(cls, endpoint: Endpoint) -> bool: |
| 339 |
""" |
| 340 |
Test if local ``endpoint`` is uninitialised (ie has state |
| 341 |
:const:`proton.Endpoint.LOCAL_UNINIT`). |
| 342 |
|
| 343 |
:param endpoint: The local endpoint to be tested. |
| 344 |
:return: ``True`` if local endpoint is in state |
| 345 |
:const:`proton.Endpoint.LOCAL_UNINIT`, ``False`` otherwise. |
| 346 |
""" |
| 347 |
return bool(endpoint.state & Endpoint.LOCAL_UNINIT) |
| 348 |
|
| 349 |
@classmethod |
| 350 |
def is_local_closed(cls, endpoint: Endpoint) -> bool: |
| 351 |
""" |
| 352 |
Test if local ``endpoint`` is closed (ie has state |
| 353 |
:const:`proton.Endpoint.LOCAL_CLOSED`). |
| 354 |
|
| 355 |
:param endpoint: The local endpoint to be tested. |
| 356 |
:return: ``True`` if local endpoint is in state |
| 357 |
:const:`proton.Endpoint.LOCAL_CLOSED`, ``False`` otherwise. |
| 358 |
""" |
| 359 |
return bool(endpoint.state & Endpoint.LOCAL_CLOSED) |
| 360 |
|
| 361 |
@classmethod |
| 362 |
def is_remote_open(cls, endpoint: Endpoint) -> bool: |
| 363 |
""" |
| 364 |
Test if remote ``endpoint`` is open (ie has state |
| 365 |
:const:`proton.Endpoint.LOCAL_ACTIVE`). |
| 366 |
|
| 367 |
:param endpoint: The remote endpoint to be tested. |
| 368 |
:return: ``True`` if remote endpoint is in state |
| 369 |
:const:`proton.Endpoint.LOCAL_ACTIVE`, ``False`` otherwise. |
| 370 |
""" |
| 371 |
return bool(endpoint.state & Endpoint.REMOTE_ACTIVE) |
| 372 |
|
| 373 |
@classmethod |
| 374 |
def is_remote_closed(cls, endpoint: Endpoint) -> bool: |
| 375 |
""" |
| 376 |
Test if remote ``endpoint`` is closed (ie has state |
| 377 |
:const:`proton.Endpoint.REMOTE_CLOSED`). |
| 378 |
|
| 379 |
:param endpoint: The remote endpoint to be tested. |
| 380 |
:return: ``True`` if remote endpoint is in state |
| 381 |
:const:`proton.Endpoint.REMOTE_CLOSED`, ``False`` otherwise. |
| 382 |
""" |
| 383 |
return bool(endpoint.state & Endpoint.REMOTE_CLOSED) |
| 384 |
|
| 385 |
@classmethod |
| 386 |
def print_error(cls, endpoint: Endpoint, endpoint_type: str) -> None: |
| 387 |
""" |
| 388 |
Logs an error message related to an error condition at an endpoint. |
| 389 |
|
| 390 |
:param endpoint: The endpoint to be tested |
| 391 |
:param endpoint_type: The endpoint type as a string to be printed |
| 392 |
in the log message. |
| 393 |
""" |
| 394 |
if endpoint.remote_condition: |
| 395 |
log.error(endpoint.remote_condition.description or endpoint.remote_condition.name) |
| 396 |
elif cls.is_local_open(endpoint) and cls.is_remote_closed(endpoint): |
| 397 |
log.error("%s closed by peer" % endpoint_type) |
| 398 |
|
| 399 |
def on_link_remote_close(self, event: Event) -> None: |
| 400 |
if event.link.remote_condition: |
| 401 |
self.on_link_error(event) |
| 402 |
elif self.is_local_closed(event.link): |
| 403 |
self.on_link_closed(event) |
| 404 |
else: |
| 405 |
self.on_link_closing(event) |
| 406 |
event.link.close() |
| 407 |
|
| 408 |
def on_session_remote_close(self, event: Event) -> None: |
| 409 |
if event.session.remote_condition: |
| 410 |
self.on_session_error(event) |
| 411 |
elif self.is_local_closed(event.session): |
| 412 |
self.on_session_closed(event) |
| 413 |
else: |
| 414 |
self.on_session_closing(event) |
| 415 |
event.session.close() |
| 416 |
|
| 417 |
def on_connection_remote_close(self, event: Event) -> None: |
| 418 |
if event.connection.remote_condition: |
| 419 |
if event.connection.remote_condition.name == "amqp:connection:forced": |
| 420 |
# Treat this the same as just having the transport closed by the peer without |
| 421 |
# sending any events. Allow reconnection to happen transparently. |
| 422 |
return |
| 423 |
self.on_connection_error(event) |
| 424 |
elif self.is_local_closed(event.connection): |
| 425 |
self.on_connection_closed(event) |
| 426 |
else: |
| 427 |
self.on_connection_closing(event) |
| 428 |
event.connection.close() |
| 429 |
|
| 430 |
def on_connection_local_open(self, event: Event) -> None: |
| 431 |
if self.is_remote_open(event.connection): |
| 432 |
self.on_connection_opened(event) |
| 433 |
|
| 434 |
def on_connection_remote_open(self, event: Event) -> None: |
| 435 |
if self.is_local_open(event.connection): |
| 436 |
self.on_connection_opened(event) |
| 437 |
elif self.is_local_uninitialised(event.connection): |
| 438 |
self.on_connection_opening(event) |
| 439 |
event.connection.open() |
| 440 |
|
| 441 |
def on_session_local_open(self, event: Event) -> None: |
| 442 |
if self.is_remote_open(event.session): |
| 443 |
self.on_session_opened(event) |
| 444 |
|
| 445 |
def on_session_remote_open(self, event: Event) -> None: |
| 446 |
if self.is_local_open(event.session): |
| 447 |
self.on_session_opened(event) |
| 448 |
elif self.is_local_uninitialised(event.session): |
| 449 |
self.on_session_opening(event) |
| 450 |
event.session.open() |
| 451 |
|
| 452 |
def on_link_local_open(self, event: Event) -> None: |
| 453 |
if self.is_remote_open(event.link): |
| 454 |
self.on_link_opened(event) |
| 455 |
|
| 456 |
def on_link_remote_open(self, event: Event) -> None: |
| 457 |
if self.is_local_open(event.link): |
| 458 |
self.on_link_opened(event) |
| 459 |
elif self.is_local_uninitialised(event.link): |
| 460 |
self.on_link_opening(event) |
| 461 |
event.link.open() |
| 462 |
|
| 463 |
def on_connection_opened(self, event: Event) -> None: |
| 464 |
""" |
| 465 |
Callback for when both the local and remote endpoints of a |
| 466 |
connection have opened. |
| 467 |
|
| 468 |
:param event: The underlying event object. Use this to obtain further |
| 469 |
information on the event. |
| 470 |
""" |
| 471 |
if self.delegate is not None: |
| 472 |
_dispatch(self.delegate, 'on_connection_opened', event) |
| 473 |
|
| 474 |
def on_session_opened(self, event: Event) -> None: |
| 475 |
""" |
| 476 |
Callback for when both the local and remote endpoints of a |
| 477 |
session have opened. |
| 478 |
|
| 479 |
:param event: The underlying event object. Use this to obtain further |
| 480 |
information on the event. |
| 481 |
""" |
| 482 |
if self.delegate is not None: |
| 483 |
_dispatch(self.delegate, 'on_session_opened', event) |
| 484 |
|
| 485 |
def on_link_opened(self, event: Event) -> None: |
| 486 |
""" |
| 487 |
Callback for when both the local and remote endpoints of a |
| 488 |
link have opened. |
| 489 |
|
| 490 |
:param event: The underlying event object. Use this to obtain further |
| 491 |
information on the event. |
| 492 |
""" |
| 493 |
if self.delegate is not None: |
| 494 |
_dispatch(self.delegate, 'on_link_opened', event) |
| 495 |
|
| 496 |
def on_connection_opening(self, event: Event) -> None: |
| 497 |
""" |
| 498 |
Callback for when a remote peer initiates the opening of |
| 499 |
a connection. |
| 500 |
|
| 501 |
:param event: The underlying event object. Use this to obtain further |
| 502 |
information on the event. |
| 503 |
""" |
| 504 |
if self.delegate is not None: |
| 505 |
_dispatch(self.delegate, 'on_connection_opening', event) |
| 506 |
|
| 507 |
def on_session_opening(self, event: Event) -> None: |
| 508 |
""" |
| 509 |
Callback for when a remote peer initiates the opening of |
| 510 |
a session. |
| 511 |
|
| 512 |
:param event: The underlying event object. Use this to obtain further |
| 513 |
information on the event. |
| 514 |
""" |
| 515 |
if self.delegate is not None: |
| 516 |
_dispatch(self.delegate, 'on_session_opening', event) |
| 517 |
|
| 518 |
def on_link_opening(self, event: Event) -> None: |
| 519 |
""" |
| 520 |
Callback for when a remote peer initiates the opening of |
| 521 |
a link. |
| 522 |
|
| 523 |
:param event: The underlying event object. Use this to obtain further |
| 524 |
information on the event. |
| 525 |
""" |
| 526 |
if self.delegate is not None: |
| 527 |
_dispatch(self.delegate, 'on_link_opening', event) |
| 528 |
|
| 529 |
def on_connection_error(self, event: Event) -> None: |
| 530 |
""" |
| 531 |
Callback for when an initiated connection open fails. |
| 532 |
|
| 533 |
:param event: The underlying event object. Use this to obtain further |
| 534 |
information on the event. |
| 535 |
""" |
| 536 |
if self.delegate is not None: |
| 537 |
_dispatch(self.delegate, 'on_connection_error', event) |
| 538 |
else: |
| 539 |
self.print_error(event.connection, "connection") |
| 540 |
|
| 541 |
def on_session_error(self, event: Event) -> None: |
| 542 |
""" |
| 543 |
Callback for when an initiated session open fails. |
| 544 |
|
| 545 |
:param event: The underlying event object. Use this to obtain further |
| 546 |
information on the event. |
| 547 |
""" |
| 548 |
if self.delegate is not None: |
| 549 |
_dispatch(self.delegate, 'on_session_error', event) |
| 550 |
else: |
| 551 |
self.print_error(event.session, "session") |
| 552 |
event.connection.close() |
| 553 |
|
| 554 |
def on_link_error(self, event: Event) -> None: |
| 555 |
""" |
| 556 |
Callback for when an initiated link open fails. |
| 557 |
|
| 558 |
:param event: The underlying event object. Use this to obtain further |
| 559 |
information on the event. |
| 560 |
""" |
| 561 |
if self.delegate is not None: |
| 562 |
_dispatch(self.delegate, 'on_link_error', event) |
| 563 |
else: |
| 564 |
self.print_error(event.link, "link") |
| 565 |
event.connection.close() |
| 566 |
|
| 567 |
def on_connection_closed(self, event: Event) -> None: |
| 568 |
""" |
| 569 |
Callback for when both the local and remote endpoints of a |
| 570 |
connection have closed. |
| 571 |
|
| 572 |
:param event: The underlying event object. Use this to obtain further |
| 573 |
information on the event. |
| 574 |
""" |
| 575 |
if self.delegate is not None: |
| 576 |
_dispatch(self.delegate, 'on_connection_closed', event) |
| 577 |
|
| 578 |
def on_session_closed(self, event: Event) -> None: |
| 579 |
""" |
| 580 |
Callback for when both the local and remote endpoints of a |
| 581 |
session have closed. |
| 582 |
|
| 583 |
:param event: The underlying event object. Use this to obtain further |
| 584 |
information on the event. |
| 585 |
""" |
| 586 |
if self.delegate is not None: |
| 587 |
_dispatch(self.delegate, 'on_session_closed', event) |
| 588 |
|
| 589 |
def on_link_closed(self, event: Event) -> None: |
| 590 |
""" |
| 591 |
Callback for when both the local and remote endpoints of a |
| 592 |
link have closed. |
| 593 |
|
| 594 |
:param event: The underlying event object. Use this to obtain further |
| 595 |
information on the event. |
| 596 |
""" |
| 597 |
if self.delegate is not None: |
| 598 |
_dispatch(self.delegate, 'on_link_closed', event) |
| 599 |
|
| 600 |
def on_connection_closing(self, event: Event) -> None: |
| 601 |
""" |
| 602 |
Callback for when a remote peer initiates the closing of |
| 603 |
a connection. |
| 604 |
|
| 605 |
:param event: The underlying event object. Use this to obtain further |
| 606 |
information on the event. |
| 607 |
""" |
| 608 |
if self.delegate is not None: |
| 609 |
_dispatch(self.delegate, 'on_connection_closing', event) |
| 610 |
elif self.peer_close_is_error: |
| 611 |
self.on_connection_error(event) |
| 612 |
|
| 613 |
def on_session_closing(self, event: Event) -> None: |
| 614 |
""" |
| 615 |
Callback for when a remote peer initiates the closing of |
| 616 |
a session. |
| 617 |
|
| 618 |
:param event: The underlying event object. Use this to obtain further |
| 619 |
information on the event. |
| 620 |
""" |
| 621 |
if self.delegate is not None: |
| 622 |
_dispatch(self.delegate, 'on_session_closing', event) |
| 623 |
elif self.peer_close_is_error: |
| 624 |
self.on_session_error(event) |
| 625 |
|
| 626 |
def on_link_closing(self, event: Event) -> None: |
| 627 |
""" |
| 628 |
Callback for when a remote peer initiates the closing of |
| 629 |
a link. |
| 630 |
|
| 631 |
:param event: The underlying event object. Use this to obtain further |
| 632 |
information on the event. |
| 633 |
""" |
| 634 |
if self.delegate is not None: |
| 635 |
_dispatch(self.delegate, 'on_link_closing', event) |
| 636 |
elif self.peer_close_is_error: |
| 637 |
self.on_link_error(event) |
| 638 |
|
| 639 |
def on_transport_tail_closed(self, event: Event) -> None: |
| 640 |
""" |
| 641 |
Callback for when the transport tail has closed (ie no further input will |
| 642 |
be accepted by the transport). |
| 643 |
|
| 644 |
:param event: The underlying event object. Use this to obtain further |
| 645 |
information on the event. |
| 646 |
""" |
| 647 |
self.on_transport_closed(event) |
| 648 |
|
| 649 |
def on_transport_closed(self, event: Event) -> None: |
| 650 |
""" |
| 651 |
Callback for when the transport has closed - ie both the head (input) and |
| 652 |
tail (output) of the transport pipeline are closed. |
| 653 |
|
| 654 |
:param event: The underlying event object. Use this to obtain further |
| 655 |
information on the event. |
| 656 |
""" |
| 657 |
if self.delegate is not None and event.connection and self.is_local_open(event.connection): |
| 658 |
_dispatch(self.delegate, 'on_disconnected', event) |
| 659 |
|
| 660 |
|
| 661 |
class MessagingHandler(Handler, Acking): |
| 662 |
""" |
| 663 |
A general purpose handler that makes the proton-c events somewhat |
| 664 |
simpler to deal with and/or avoids repetitive tasks for common use |
| 665 |
cases. |
| 666 |
|
| 667 |
:param prefetch: Initial flow credit for receiving messages, defaults to 10. |
| 668 |
:param auto_accept: If ``True``, accept all messages (default). Otherwise |
| 669 |
messages must be individually accepted or rejected. |
| 670 |
:param auto_settle: If ``True`` (default), automatically settle messages |
| 671 |
upon receiving a settled disposition for that delivery. Otherwise |
| 672 |
messages must be explicitly settled. |
| 673 |
:param peer_close_is_error: If ``True``, a peer endpoint closing will be |
| 674 |
treated as an error with an error callback. Otherwise (default), the |
| 675 |
normal callbacks for the closing will occur. |
| 676 |
""" |
| 677 |
|
| 678 |
def __init__( |
| 679 |
self, |
| 680 |
prefetch: int = 10, |
| 681 |
auto_accept: bool = True, |
| 682 |
auto_settle: bool = True, |
| 683 |
peer_close_is_error: bool = False |
| 684 |
) -> None: |
| 685 |
self.handlers = [] |
| 686 |
if prefetch: |
| 687 |
self.handlers.append(FlowController(prefetch)) |
| 688 |
self.handlers.append(EndpointStateHandler(peer_close_is_error, weakref.proxy(self))) |
| 689 |
self.handlers.append(IncomingMessageHandler(auto_accept, weakref.proxy(self))) |
| 690 |
self.handlers.append(OutgoingMessageHandler(auto_settle, weakref.proxy(self))) |
| 691 |
self.fatal_conditions = ["amqp:unauthorized-access"] |
| 692 |
|
| 693 |
def on_transport_error(self, event: Event) -> None: |
| 694 |
""" |
| 695 |
Called when some error is encountered with the transport over |
| 696 |
which the AMQP connection is to be established. This includes |
| 697 |
authentication errors as well as socket errors. |
| 698 |
|
| 699 |
:param event: The underlying event object. Use this to obtain further |
| 700 |
information on the event. |
| 701 |
""" |
| 702 |
if event.transport.condition: |
| 703 |
if event.transport.condition.info: |
| 704 |
log.error("%s: %s: %s" % ( |
| 705 |
event.transport.condition.name, event.transport.condition.description, |
| 706 |
event.transport.condition.info)) |
| 707 |
else: |
| 708 |
log.error("%s: %s" % (event.transport.condition.name, event.transport.condition.description)) |
| 709 |
if event.transport.condition.name in self.fatal_conditions: |
| 710 |
event.connection.close() |
| 711 |
else: |
| 712 |
logging.error("Unspecified transport error") |
| 713 |
|
| 714 |
def on_connection_error(self, event: Event) -> None: |
| 715 |
""" |
| 716 |
Called when the peer closes the connection with an error condition. |
| 717 |
|
| 718 |
:param event: The underlying event object. Use this to obtain further |
| 719 |
information on the event. |
| 720 |
:type event: :class:`proton.Event` |
| 721 |
""" |
| 722 |
EndpointStateHandler.print_error(event.connection, "connection") |
| 723 |
|
| 724 |
def on_session_error(self, event: Event) -> None: |
| 725 |
""" |
| 726 |
Called when the peer closes the session with an error condition. |
| 727 |
|
| 728 |
:param event: The underlying event object. Use this to obtain further |
| 729 |
information on the event. |
| 730 |
""" |
| 731 |
EndpointStateHandler.print_error(event.session, "session") |
| 732 |
event.connection.close() |
| 733 |
|
| 734 |
def on_link_error(self, event: Event) -> None: |
| 735 |
""" |
| 736 |
Called when the peer closes the link with an error condition. |
| 737 |
|
| 738 |
:param event: The underlying event object. Use this to obtain further |
| 739 |
information on the event. |
| 740 |
""" |
| 741 |
EndpointStateHandler.print_error(event.link, "link") |
| 742 |
event.connection.close() |
| 743 |
|
| 744 |
def on_reactor_init(self, event: Event) -> None: |
| 745 |
""" |
| 746 |
Called when the event loop - the reactor - starts. |
| 747 |
|
| 748 |
:param event: The underlying event object. Use this to obtain further |
| 749 |
information on the event. |
| 750 |
""" |
| 751 |
if hasattr(event.reactor, 'subclass'): |
| 752 |
setattr(event, event.reactor.subclass.__name__.lower(), event.reactor) |
| 753 |
self.on_start(event) |
| 754 |
|
| 755 |
def on_start(self, event: Event) -> None: |
| 756 |
""" |
| 757 |
Called when the event loop starts. (Just an alias for on_reactor_init) |
| 758 |
|
| 759 |
:param event: The underlying event object. Use this to obtain further |
| 760 |
information on the event. |
| 761 |
""" |
| 762 |
pass |
| 763 |
|
| 764 |
def on_connection_closed(self, event: Event) -> None: |
| 765 |
""" |
| 766 |
Called when the connection is closed. |
| 767 |
|
| 768 |
:param event: The underlying event object. Use this to obtain further |
| 769 |
information on the event. |
| 770 |
""" |
| 771 |
pass |
| 772 |
|
| 773 |
def on_session_closed(self, event: Event) -> None: |
| 774 |
""" |
| 775 |
Called when the session is closed. |
| 776 |
|
| 777 |
:param event: The underlying event object. Use this to obtain further |
| 778 |
information on the event. |
| 779 |
""" |
| 780 |
pass |
| 781 |
|
| 782 |
def on_link_closed(self, event: Event) -> None: |
| 783 |
""" |
| 784 |
Called when the link is closed. |
| 785 |
|
| 786 |
:param event: The underlying event object. Use this to obtain further |
| 787 |
information on the event. |
| 788 |
""" |
| 789 |
pass |
| 790 |
|
| 791 |
def on_connection_closing(self, event: Event) -> None: |
| 792 |
""" |
| 793 |
Called when the peer initiates the closing of the connection. |
| 794 |
|
| 795 |
:param event: The underlying event object. Use this to obtain further |
| 796 |
information on the event. |
| 797 |
""" |
| 798 |
pass |
| 799 |
|
| 800 |
def on_session_closing(self, event: Event) -> None: |
| 801 |
""" |
| 802 |
Called when the peer initiates the closing of the session. |
| 803 |
|
| 804 |
:param event: The underlying event object. Use this to obtain further |
| 805 |
information on the event. |
| 806 |
""" |
| 807 |
pass |
| 808 |
|
| 809 |
def on_link_closing(self, event: Event) -> None: |
| 810 |
""" |
| 811 |
Called when the peer initiates the closing of the link. |
| 812 |
|
| 813 |
:param event: The underlying event object. Use this to obtain further |
| 814 |
information on the event. |
| 815 |
""" |
| 816 |
pass |
| 817 |
|
| 818 |
def on_disconnected(self, event: Event) -> None: |
| 819 |
""" |
| 820 |
Called when the socket is disconnected. |
| 821 |
|
| 822 |
:param event: The underlying event object. Use this to obtain further |
| 823 |
information on the event. |
| 824 |
""" |
| 825 |
pass |
| 826 |
|
| 827 |
def on_sendable(self, event: Event) -> None: |
| 828 |
""" |
| 829 |
Called when the sender link has credit and messages can |
| 830 |
therefore be transferred. |
| 831 |
|
| 832 |
:param event: The underlying event object. Use this to obtain further |
| 833 |
information on the event. |
| 834 |
""" |
| 835 |
pass |
| 836 |
|
| 837 |
def on_accepted(self, event: Event) -> None: |
| 838 |
""" |
| 839 |
Called when the remote peer accepts an outgoing message. |
| 840 |
|
| 841 |
:param event: The underlying event object. Use this to obtain further |
| 842 |
information on the event. |
| 843 |
""" |
| 844 |
pass |
| 845 |
|
| 846 |
def on_rejected(self, event: Event) -> None: |
| 847 |
""" |
| 848 |
Called when the remote peer rejects an outgoing message. |
| 849 |
|
| 850 |
:param event: The underlying event object. Use this to obtain further |
| 851 |
information on the event. |
| 852 |
""" |
| 853 |
pass |
| 854 |
|
| 855 |
def on_released(self, event: Event) -> None: |
| 856 |
""" |
| 857 |
Called when the remote peer releases an outgoing message. Note |
| 858 |
that this may be in response to either the RELEASE or MODIFIED |
| 859 |
state as defined by the AMQP specification. |
| 860 |
|
| 861 |
:param event: The underlying event object. Use this to obtain further |
| 862 |
information on the event. |
| 863 |
""" |
| 864 |
pass |
| 865 |
|
| 866 |
def on_settled(self, event: Event) -> None: |
| 867 |
""" |
| 868 |
Called when the remote peer has settled the outgoing |
| 869 |
message. This is the point at which it should never be |
| 870 |
retransmitted. |
| 871 |
|
| 872 |
:param event: The underlying event object. Use this to obtain further |
| 873 |
information on the event. |
| 874 |
""" |
| 875 |
pass |
| 876 |
|
| 877 |
def on_message(self, event: Event) -> None: |
| 878 |
""" |
| 879 |
Called when a message is received. The message itself can be |
| 880 |
obtained as a property on the event. For the purpose of |
| 881 |
referring to this message in further actions (e.g. if |
| 882 |
explicitly accepting it, the ``delivery`` should be used, also |
| 883 |
obtainable via a property on the event. |
| 884 |
|
| 885 |
:param event: The underlying event object. Use this to obtain further |
| 886 |
information on the event. In particular, the message itself may |
| 887 |
be obtained by accessing ``event.message``. |
| 888 |
""" |
| 889 |
pass |
| 890 |
|
| 891 |
|
| 892 |
class TransactionHandler(object): |
| 893 |
""" |
| 894 |
The interface for transaction handlers - ie objects that want to |
| 895 |
be notified of state changes related to a transaction. |
| 896 |
""" |
| 897 |
|
| 898 |
def on_transaction_declared(self, event: Event) -> None: |
| 899 |
""" |
| 900 |
Called when a local transaction is declared. |
| 901 |
|
| 902 |
:param event: The underlying event object. Use this to obtain further |
| 903 |
information on the event. In particular, the :class:`proton.reactor.Transaction` |
| 904 |
object may be obtained by accessing ``event.transaction``. |
| 905 |
""" |
| 906 |
pass |
| 907 |
|
| 908 |
def on_transaction_committed(self, event: Event) -> None: |
| 909 |
""" |
| 910 |
Called when a local transaction is discharged successfully |
| 911 |
(committed). |
| 912 |
|
| 913 |
:param event: The underlying event object. Use this to obtain further |
| 914 |
information on the event. |
| 915 |
""" |
| 916 |
pass |
| 917 |
|
| 918 |
def on_transaction_aborted(self, event: Event) -> None: |
| 919 |
""" |
| 920 |
Called when a local transaction is discharged unsuccessfully |
| 921 |
(aborted). |
| 922 |
|
| 923 |
:param event: The underlying event object. Use this to obtain further |
| 924 |
information on the event. |
| 925 |
""" |
| 926 |
pass |
| 927 |
|
| 928 |
def on_transaction_declare_failed(self, event: Event) -> None: |
| 929 |
""" |
| 930 |
Called when a local transaction declare fails. |
| 931 |
|
| 932 |
:param event: The underlying event object. Use this to obtain further |
| 933 |
information on the event. |
| 934 |
""" |
| 935 |
pass |
| 936 |
|
| 937 |
def on_transaction_commit_failed(self, event: Event) -> None: |
| 938 |
""" |
| 939 |
Called when the commit of a local transaction fails. |
| 940 |
|
| 941 |
:param event: The underlying event object. Use this to obtain further |
| 942 |
information on the event. |
| 943 |
""" |
| 944 |
pass |
| 945 |
|
| 946 |
|
| 947 |
class TransactionalClientHandler(MessagingHandler, TransactionHandler): |
| 948 |
""" |
| 949 |
An extension to the MessagingHandler for applications using |
| 950 |
transactions. This handler provides all of the callbacks found |
| 951 |
in :class:`MessagingHandler` and :class:`TransactionHandler`, |
| 952 |
and provides a convenience method :meth:`accept` for performing |
| 953 |
a transactional acceptance of received messages. |
| 954 |
|
| 955 |
:param prefetch: Initial flow credit for receiving messages, defaults to 10. |
| 956 |
:param auto_accept: If ``True``, accept all messages (default). Otherwise |
| 957 |
messages must be individually accepted or rejected. |
| 958 |
:param auto_settle: If ``True`` (default), automatically settle messages |
| 959 |
upon receiving a settled disposition for that delivery. Otherwise |
| 960 |
messages must be explicitly settled. |
| 961 |
:param peer_close_is_error: If ``True``, a peer endpoint closing will be |
| 962 |
treated as an error with an error callback. Otherwise (default), the |
| 963 |
normal callbacks for the closing will occur. |
| 964 |
""" |
| 965 |
|
| 966 |
def __init__( |
| 967 |
self, |
| 968 |
prefetch: int = 10, |
| 969 |
auto_accept: bool = False, |
| 970 |
auto_settle: bool = True, |
| 971 |
peer_close_is_error: bool = False |
| 972 |
) -> None: |
| 973 |
super(TransactionalClientHandler, self).__init__(prefetch, auto_accept, auto_settle, peer_close_is_error) |
| 974 |
|
| 975 |
def accept(self, delivery: Delivery, transaction: Optional['Transaction'] = None): |
| 976 |
""" |
| 977 |
A convenience method for accepting a received message as part of a |
| 978 |
transaction. If no transaction object is supplied, a regular |
| 979 |
non-transactional acceptance will be performed. |
| 980 |
|
| 981 |
:param delivery: Delivery tracking object for received message. |
| 982 |
:param transaction: Transaction tracking object which is required if |
| 983 |
the message is being accepted under the transaction. If ``None`` (default), |
| 984 |
then a normal non-transactional accept occurs. |
| 985 |
""" |
| 986 |
if transaction: |
| 987 |
transaction.accept(delivery) |
| 988 |
else: |
| 989 |
super(TransactionalClientHandler, self).accept(delivery) |
| 990 |
|
| 991 |
|
| 992 |
class FlowController(Handler): |
| 993 |
def __init__(self, window: int = 1024) -> None: |
| 994 |
self._window = window |
| 995 |
self._drained = 0 |
| 996 |
|
| 997 |
def on_link_local_open(self, event: Event) -> None: |
| 998 |
self._flow(event.link) |
| 999 |
|
| 1000 |
def on_link_remote_open(self, event: Event) -> None: |
| 1001 |
self._flow(event.link) |
| 1002 |
|
| 1003 |
def on_link_flow(self, event: Event) -> None: |
| 1004 |
self._flow(event.link) |
| 1005 |
|
| 1006 |
def on_delivery(self, event: Event) -> None: |
| 1007 |
self._flow(event.link) |
| 1008 |
|
| 1009 |
def _flow(self, link: Union['Sender', 'Receiver']) -> None: |
| 1010 |
if link.is_receiver: |
| 1011 |
self._drained += link.drained() |
| 1012 |
if self._drained == 0: |
| 1013 |
delta = self._window - link.credit |
| 1014 |
link.flow(delta) |
| 1015 |
|
| 1016 |
|
| 1017 |
class Handshaker(Handler): |
| 1018 |
|
| 1019 |
@staticmethod |
| 1020 |
def on_connection_remote_open(event: Event) -> None: |
| 1021 |
conn = event.connection |
| 1022 |
if conn.state & Endpoint.LOCAL_UNINIT: |
| 1023 |
conn.open() |
| 1024 |
|
| 1025 |
@staticmethod |
| 1026 |
def on_session_remote_open(event: Event) -> None: |
| 1027 |
ssn = event.session |
| 1028 |
if ssn.state & Endpoint.LOCAL_UNINIT: |
| 1029 |
ssn.open() |
| 1030 |
|
| 1031 |
@staticmethod |
| 1032 |
def on_link_remote_open(event: Event) -> None: |
| 1033 |
link = event.link |
| 1034 |
if link.state & Endpoint.LOCAL_UNINIT: |
| 1035 |
link.source.copy(link.remote_source) |
| 1036 |
link.target.copy(link.remote_target) |
| 1037 |
link.open() |
| 1038 |
|
| 1039 |
@staticmethod |
| 1040 |
def on_connection_remote_close(event: Event) -> None: |
| 1041 |
conn = event.connection |
| 1042 |
if not conn.state & Endpoint.LOCAL_CLOSED: |
| 1043 |
conn.close() |
| 1044 |
|
| 1045 |
@staticmethod |
| 1046 |
def on_session_remote_close(event: Event) -> None: |
| 1047 |
ssn = event.session |
| 1048 |
if not ssn.state & Endpoint.LOCAL_CLOSED: |
| 1049 |
ssn.close() |
| 1050 |
|
| 1051 |
@staticmethod |
| 1052 |
def on_link_remote_close(event: Event) -> None: |
| 1053 |
link = event.link |
| 1054 |
if not link.state & Endpoint.LOCAL_CLOSED: |
| 1055 |
link.close() |
| 1056 |
|
| 1057 |
|
| 1058 |
# Back compatibility definitions |
| 1059 |
CFlowController = FlowController |
| 1060 |
CHandshaker = Handshaker |
| 1061 |
|
| 1062 |
|
| 1063 |
class PythonIO: |
| 1064 |
|
| 1065 |
def __init__(self) -> None: |
| 1066 |
self.selectables = [] |
| 1067 |
self.delegate = IOHandler() |
| 1068 |
|
| 1069 |
def on_unhandled(self, method: str, event: Event) -> None: |
| 1070 |
event.dispatch(self.delegate) |
| 1071 |
|
| 1072 |
def on_selectable_init(self, event: Event) -> None: |
| 1073 |
self.selectables.append(event.context) |
| 1074 |
|
| 1075 |
def on_selectable_updated(self, event: Event) -> None: |
| 1076 |
pass |
| 1077 |
|
| 1078 |
def on_selectable_final(self, event: Event) -> None: |
| 1079 |
sel = event.context |
| 1080 |
if sel.is_terminal: |
| 1081 |
self.selectables.remove(sel) |
| 1082 |
sel.close() |
| 1083 |
|
| 1084 |
def on_reactor_quiesced(self, event: Event) -> None: |
| 1085 |
reactor = event.reactor |
| 1086 |
# check if we are still quiesced, other handlers of |
| 1087 |
# on_reactor_quiesced could have produced events to process |
| 1088 |
if not reactor.quiesced: |
| 1089 |
return |
| 1090 |
|
| 1091 |
reading = [] |
| 1092 |
writing = [] |
| 1093 |
deadline = None |
| 1094 |
for sel in self.selectables: |
| 1095 |
if sel.reading: |
| 1096 |
reading.append(sel) |
| 1097 |
if sel.writing: |
| 1098 |
writing.append(sel) |
| 1099 |
if sel.deadline: |
| 1100 |
if deadline is None: |
| 1101 |
deadline = sel.deadline |
| 1102 |
else: |
| 1103 |
deadline = min(sel.deadline, deadline) |
| 1104 |
|
| 1105 |
if deadline is not None: |
| 1106 |
timeout = deadline - time.time() |
| 1107 |
else: |
| 1108 |
timeout = reactor.timeout |
| 1109 |
if timeout < 0: |
| 1110 |
timeout = 0 |
| 1111 |
timeout = min(timeout, reactor.timeout) |
| 1112 |
readable, writable, _ = IO.select(reading, writing, [], timeout) |
| 1113 |
|
| 1114 |
now = reactor.mark() |
| 1115 |
|
| 1116 |
for s in readable: |
| 1117 |
s.readable() |
| 1118 |
for s in writable: |
| 1119 |
s.writable() |
| 1120 |
for s in self.selectables: |
| 1121 |
if s.deadline and now > s.deadline: |
| 1122 |
s.expired() |
| 1123 |
|
| 1124 |
reactor.yield_() |
| 1125 |
|
| 1126 |
|
| 1127 |
# For C style IO handler need to implement Selector |
| 1128 |
class IOHandler(Handler): |
| 1129 |
|
| 1130 |
def __init__(self) -> None: |
| 1131 |
self._selector = IO.Selector() |
| 1132 |
|
| 1133 |
def on_selectable_init(self, event: Event) -> None: |
| 1134 |
s = event.selectable |
| 1135 |
self._selector.add(s) |
| 1136 |
s._reactor._selectables += 1 |
| 1137 |
|
| 1138 |
def on_selectable_updated(self, event: Event) -> None: |
| 1139 |
s = event.selectable |
| 1140 |
self._selector.update(s) |
| 1141 |
|
| 1142 |
def on_selectable_final(self, event: Event) -> None: |
| 1143 |
s = event.selectable |
| 1144 |
self._selector.remove(s) |
| 1145 |
s._reactor._selectables -= 1 |
| 1146 |
s.close() |
| 1147 |
|
| 1148 |
def on_reactor_quiesced(self, event: Event) -> None: |
| 1149 |
r = event.reactor |
| 1150 |
|
| 1151 |
if not r.quiesced: |
| 1152 |
return |
| 1153 |
|
| 1154 |
d = r.timer_deadline |
| 1155 |
readable, writable, expired = self._selector.select(r.timeout) |
| 1156 |
|
| 1157 |
now = r.mark() |
| 1158 |
|
| 1159 |
for s in readable: |
| 1160 |
s.readable() |
| 1161 |
for s in writable: |
| 1162 |
s.writable() |
| 1163 |
for s in expired: |
| 1164 |
s.expired() |
| 1165 |
|
| 1166 |
r.yield_() |
| 1167 |
|
| 1168 |
def on_selectable_readable(self, event: Event) -> None: |
| 1169 |
s = event.selectable |
| 1170 |
t = s._transport |
| 1171 |
|
| 1172 |
# If we're an acceptor we can't have a transport |
| 1173 |
# and we don't want to do anything here in any case |
| 1174 |
if not t: |
| 1175 |
return |
| 1176 |
|
| 1177 |
capacity = t.capacity() |
| 1178 |
if capacity > 0: |
| 1179 |
try: |
| 1180 |
b = s.recv(capacity) |
| 1181 |
if len(b) > 0: |
| 1182 |
n = t.push(b) |
| 1183 |
else: |
| 1184 |
# EOF handling |
| 1185 |
self.on_selectable_error(event) |
| 1186 |
except socket.error as e: |
| 1187 |
# TODO: What's the error handling to be here? |
| 1188 |
log.error("Couldn't recv: %r" % e) |
| 1189 |
t.close_tail() |
| 1190 |
|
| 1191 |
# Always update as we may have gone to not reading or from |
| 1192 |
# not writing to writing when processing the incoming bytes |
| 1193 |
r = s._reactor |
| 1194 |
self.update(t, s, r.now) |
| 1195 |
|
| 1196 |
def on_selectable_writable(self, event: Event) -> None: |
| 1197 |
s = event.selectable |
| 1198 |
t = s._transport |
| 1199 |
|
| 1200 |
# If we're an acceptor we can't have a transport |
| 1201 |
# and we don't want to do anything here in any case |
| 1202 |
if not t: |
| 1203 |
return |
| 1204 |
|
| 1205 |
pending = t.pending() |
| 1206 |
if pending > 0: |
| 1207 |
|
| 1208 |
try: |
| 1209 |
n = s.send(t.peek(pending)) |
| 1210 |
t.pop(n) |
| 1211 |
except socket.error as e: |
| 1212 |
log.error("Couldn't send: %r" % e) |
| 1213 |
# TODO: Error? or actually an exception |
| 1214 |
t.close_head() |
| 1215 |
|
| 1216 |
newpending = t.pending() |
| 1217 |
if newpending != pending: |
| 1218 |
r = s._reactor |
| 1219 |
self.update(t, s, r.now) |
| 1220 |
|
| 1221 |
def on_selectable_error(self, event: Event) -> None: |
| 1222 |
s = event.selectable |
| 1223 |
t = s._transport |
| 1224 |
|
| 1225 |
t.close_head() |
| 1226 |
t.close_tail() |
| 1227 |
s.terminate() |
| 1228 |
s._transport = None |
| 1229 |
t._selectable = None |
| 1230 |
s.update() |
| 1231 |
|
| 1232 |
def on_selectable_expired(self, event: Event) -> None: |
| 1233 |
s = event.selectable |
| 1234 |
t = s._transport |
| 1235 |
r = s._reactor |
| 1236 |
|
| 1237 |
self.update(t, s, r.now) |
| 1238 |
|
| 1239 |
def on_connection_local_open(self, event: Event) -> None: |
| 1240 |
c = event.connection |
| 1241 |
if not c.state & Endpoint.REMOTE_UNINIT: |
| 1242 |
return |
| 1243 |
|
| 1244 |
t = Transport() |
| 1245 |
# It seems perverse, but the C code ignores bind errors too! |
| 1246 |
# and this is required or you get errors because Connector() has already |
| 1247 |
# bound the transport and connection! |
| 1248 |
t.bind_nothrow(c) |
| 1249 |
|
| 1250 |
def on_connection_bound(self, event: Event) -> None: |
| 1251 |
c = event.connection |
| 1252 |
t = event.transport |
| 1253 |
|
| 1254 |
reactor = c._reactor |
| 1255 |
|
| 1256 |
# link the new transport to its reactor: |
| 1257 |
t._reactor = reactor |
| 1258 |
|
| 1259 |
if c._acceptor: |
| 1260 |
# this connection was created by the acceptor. There is already a |
| 1261 |
# socket assigned to this connection. Nothing needs to be done. |
| 1262 |
return |
| 1263 |
|
| 1264 |
url = c.url or Url(c.hostname) |
| 1265 |
url.defaults() |
| 1266 |
|
| 1267 |
host = url.host |
| 1268 |
port = int(url.port) |
| 1269 |
|
| 1270 |
if not c.user: |
| 1271 |
user = url.username |
| 1272 |
if user: |
| 1273 |
c.user = user |
| 1274 |
password = url.password |
| 1275 |
if password: |
| 1276 |
c.password = password |
| 1277 |
|
| 1278 |
addrs = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM) |
| 1279 |
|
| 1280 |
# Try first possible address |
| 1281 |
log.debug("Connect trying first transport address: %s", addrs[0]) |
| 1282 |
sock = IO.connect(addrs[0]) |
| 1283 |
|
| 1284 |
# At this point we need to arrange to be called back when the socket is writable |
| 1285 |
ConnectSelectable(sock, reactor, addrs[1:], t, self) |
| 1286 |
|
| 1287 |
# TODO: Don't understand why we need this now - how can we get PN_TRANSPORT until the connection succeeds? |
| 1288 |
t._selectable = None |
| 1289 |
|
| 1290 |
@staticmethod |
| 1291 |
def update(transport: Transport, selectable: Selectable, now: float) -> None: |
| 1292 |
try: |
| 1293 |
capacity = transport.capacity() |
| 1294 |
selectable.reading = capacity > 0 |
| 1295 |
except: |
| 1296 |
if transport.closed: |
| 1297 |
selectable.terminate() |
| 1298 |
selectable._transport = None |
| 1299 |
transport._selectable = None |
| 1300 |
try: |
| 1301 |
pending = transport.pending() |
| 1302 |
selectable.writing = pending > 0 |
| 1303 |
except: |
| 1304 |
if transport.closed: |
| 1305 |
selectable.terminate() |
| 1306 |
selectable._transport = None |
| 1307 |
transport._selectable = None |
| 1308 |
selectable.deadline = transport.tick(now) |
| 1309 |
selectable.update() |
| 1310 |
|
| 1311 |
def on_transport(self, event: Event) -> None: |
| 1312 |
t = event.transport |
| 1313 |
r = t._reactor |
| 1314 |
s = t._selectable |
| 1315 |
if s and not s.is_terminal: |
| 1316 |
self.update(t, s, r.now) |
| 1317 |
|
| 1318 |
def on_transport_closed(self, event: Event) -> None: |
| 1319 |
t = event.transport |
| 1320 |
r = t._reactor |
| 1321 |
s = t._selectable |
| 1322 |
if s and not s.is_terminal: |
| 1323 |
s.terminate() |
| 1324 |
s._transport = None |
| 1325 |
t._selectable = None |
| 1326 |
r.update(s) |
| 1327 |
t.unbind() |
| 1328 |
|
| 1329 |
|
| 1330 |
class ConnectSelectable(Selectable): |
| 1331 |
def __init__( |
| 1332 |
self, |
| 1333 |
sock: socket.socket, |
| 1334 |
reactor: 'Container', |
| 1335 |
addrs: List[Any], |
| 1336 |
transport: Transport, |
| 1337 |
iohandler: IOHandler |
| 1338 |
) -> None: |
| 1339 |
super(ConnectSelectable, self).__init__(sock, reactor) |
| 1340 |
self.writing = True |
| 1341 |
self._addrs = addrs |
| 1342 |
self._transport = transport |
| 1343 |
self._iohandler = iohandler |
| 1344 |
transport._connect_selectable = self |
| 1345 |
|
| 1346 |
def readable(self) -> None: |
| 1347 |
pass |
| 1348 |
|
| 1349 |
def writable(self) -> None: |
| 1350 |
e = self._delegate.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) |
| 1351 |
t = self._transport |
| 1352 |
t._connect_selectable = None |
| 1353 |
|
| 1354 |
# Always cleanup this ConnectSelectable: either we failed or created a new one |
| 1355 |
# Do it first to ensure the socket gets deregistered before being registered again |
| 1356 |
# in the case of connecting |
| 1357 |
self.terminate() |
| 1358 |
self._transport = None |
| 1359 |
self.update() |
| 1360 |
|
| 1361 |
if e == 0: |
| 1362 |
log.debug("Connection succeeded") |
| 1363 |
|
| 1364 |
# Disassociate from the socket (which will be passed on) |
| 1365 |
self.release() |
| 1366 |
|
| 1367 |
s = self._reactor.selectable(delegate=self._delegate) |
| 1368 |
s._transport = t |
| 1369 |
t._selectable = s |
| 1370 |
self._iohandler.update(t, s, t._reactor.now) |
| 1371 |
|
| 1372 |
return |
| 1373 |
elif e == errno.ECONNREFUSED: |
| 1374 |
if len(self._addrs) > 0: |
| 1375 |
log.debug("Connection refused: trying next transport address: %s", self._addrs[0]) |
| 1376 |
|
| 1377 |
sock = IO.connect(self._addrs[0]) |
| 1378 |
# New ConnectSelectable for the new socket with rest of addresses |
| 1379 |
ConnectSelectable(sock, self._reactor, self._addrs[1:], t, self._iohandler) |
| 1380 |
return |
| 1381 |
else: |
| 1382 |
log.debug("Connection refused, but tried all transport addresses") |
| 1383 |
t.condition = Condition("proton.pythonio", "Connection refused to all addresses") |
| 1384 |
else: |
| 1385 |
log.error("Couldn't connect: %s", e) |
| 1386 |
t.condition = Condition("proton.pythonio", "Connection error: %s" % e) |
| 1387 |
|
| 1388 |
t.close_tail() |
| 1389 |
t.close_head() |