LMXF-specification/LXMF/Handlers.py

87 lines
4.3 KiB
Python
Raw Normal View History

2022-06-17 08:54:04 +02:00
import time
2025-04-17 13:31:00 +02:00
import threading
2022-06-17 08:54:04 +02:00
import RNS
import RNS.vendor.umsgpack as msgpack
from .LXMF import APP_NAME, stamp_cost_from_app_data, pn_announce_data_is_valid
2022-06-17 08:54:04 +02:00
from .LXMessage import LXMessage
class LXMFDeliveryAnnounceHandler:
def __init__(self, lxmrouter):
self.aspect_filter = APP_NAME+".delivery"
self.receive_path_responses = True
2022-06-17 08:54:04 +02:00
self.lxmrouter = lxmrouter
def received_announce(self, destination_hash, announced_identity, app_data):
for lxmessage in self.lxmrouter.pending_outbound:
if destination_hash == lxmessage.destination_hash:
if lxmessage.method == LXMessage.DIRECT or lxmessage.method == LXMessage.OPPORTUNISTIC:
2022-06-17 08:54:04 +02:00
lxmessage.next_delivery_attempt = time.time()
2025-04-17 13:31:00 +02:00
def outbound_trigger():
while self.lxmrouter.processing_outbound: time.sleep(0.1)
self.lxmrouter.process_outbound()
2022-06-17 08:54:04 +02:00
2025-04-17 13:31:00 +02:00
threading.Thread(target=outbound_trigger, daemon=True).start()
2022-06-17 08:54:04 +02:00
try:
stamp_cost = stamp_cost_from_app_data(app_data)
2024-09-08 01:22:00 +02:00
self.lxmrouter.update_stamp_cost(destination_hash, stamp_cost)
except Exception as e:
RNS.log(f"An error occurred while trying to decode announced stamp cost. The contained exception was: {e}", RNS.LOG_ERROR)
2022-06-17 08:54:04 +02:00
class LXMFPropagationAnnounceHandler:
def __init__(self, lxmrouter):
self.aspect_filter = APP_NAME+".propagation"
self.receive_path_responses = False
2022-06-17 08:54:04 +02:00
self.lxmrouter = lxmrouter
def received_announce(self, destination_hash, announced_identity, app_data):
try:
if type(app_data) == bytes:
if self.lxmrouter.propagation_node:
data = msgpack.unpackb(app_data)
if pn_announce_data_is_valid(data):
node_timebase = data[1]
propagation_transfer_limit = None
propagation_sync_limit = None
2025-01-24 14:05:12 +01:00
wanted_inbound_peers = None
if len(data) >= 5:
try: propagation_sync_limit = int(data[4])
except Exception as e: propagation_sync_limit = None
if len(data) >= 4:
2025-01-30 15:04:21 +01:00
# TODO: Rethink, probably not necessary anymore
# try: wanted_inbound_peers = int(data[3])
# except: wanted_inbound_peers = None
2025-01-30 15:11:26 +01:00
pass
2025-01-30 15:04:21 +01:00
if len(data) >= 3:
2025-04-17 13:31:00 +02:00
try: propagation_transfer_limit = float(data[2])
except: propagation_transfer_limit = None
2025-01-22 01:37:09 +01:00
if destination_hash in self.lxmrouter.static_peers:
self.lxmrouter.peer(destination_hash=destination_hash,
timestamp=node_timebase,
propagation_transfer_limit=propagation_transfer_limit,
propagation_sync_limit=propagation_sync_limit,
wanted_inbound_peers=wanted_inbound_peers)
2025-01-22 01:37:09 +01:00
else:
if self.lxmrouter.autopeer:
if data[0] == True:
if RNS.Transport.hops_to(destination_hash) <= self.lxmrouter.autopeer_maxdepth:
self.lxmrouter.peer(destination_hash=destination_hash,
timestamp=node_timebase,
propagation_transfer_limit=propagation_transfer_limit,
propagation_sync_limit=propagation_sync_limit,
wanted_inbound_peers=wanted_inbound_peers)
elif data[0] == False:
self.lxmrouter.unpeer(destination_hash, node_timebase)
2022-06-17 08:54:04 +02:00
except Exception as e:
RNS.log("Error while evaluating propagation node announce, ignoring announce.", RNS.LOG_DEBUG)
RNS.log("The contained exception was: "+str(e), RNS.LOG_DEBUG)