Allow announce handlers to also receive path responses

This commit is contained in:
Mark Qvist 2024-09-06 19:52:05 +02:00
parent 971f5ffadd
commit 8a82d6bfeb
1 changed files with 33 additions and 23 deletions

View File

@ -1677,7 +1677,6 @@ class Transport:
# Call externally registered callbacks from apps # Call externally registered callbacks from apps
# wanting to know when an announce arrives # wanting to know when an announce arrives
if packet.context != RNS.Packet.PATH_RESPONSE:
for handler in Transport.announce_handlers: for handler in Transport.announce_handlers:
try: try:
# Check that the announced destination matches # Check that the announced destination matches
@ -1692,6 +1691,15 @@ class Transport:
handler_expected_hash = RNS.Destination.hash_from_name_and_identity(handler.aspect_filter, announce_identity) handler_expected_hash = RNS.Destination.hash_from_name_and_identity(handler.aspect_filter, announce_identity)
if packet.destination_hash == handler_expected_hash: if packet.destination_hash == handler_expected_hash:
execute_callback = True execute_callback = True
# If this is a path response, check whether the
# handler wants to receive it.
if packet.context == RNS.Packet.PATH_RESPONSE:
if hasattr(handler, "receive_path_responses") and handler.receive_path_responses == True:
pass
else:
execute_callback = False
if execute_callback: if execute_callback:
handler.received_announce( handler.received_announce(
destination_hash=packet.destination_hash, destination_hash=packet.destination_hash,
@ -1979,7 +1987,9 @@ class Transport:
""" """
Registers an announce handler. Registers an announce handler.
:param handler: Must be an object with an *aspect_filter* attribute and a *received_announce(destination_hash, announced_identity, app_data)* callable. See the :ref:`Announce Example<example-announce>` for more info. :param handler: Must be an object with an *aspect_filter* attribute and a *received_announce(destination_hash, announced_identity, app_data)*
callable. Can optionally have a *receive_path_responses* attribute set to ``True``, to also receive all path responses, in addition to live
announces. See the :ref:`Announce Example<example-announce>` for more info.
""" """
if hasattr(handler, "received_announce") and callable(handler.received_announce): if hasattr(handler, "received_announce") and callable(handler.received_announce):
if hasattr(handler, "aspect_filter"): if hasattr(handler, "aspect_filter"):