From af538e048919bf9ab8207b97ad20a33410cd9de7 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Tue, 6 Sep 2022 17:42:13 +0200 Subject: [PATCH] Improved shutdown handling and table saving --- RNS/Interfaces/TCPInterface.py | 1 + RNS/Transport.py | 65 ++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/RNS/Interfaces/TCPInterface.py b/RNS/Interfaces/TCPInterface.py index 29f9ed9..606a858 100644 --- a/RNS/Interfaces/TCPInterface.py +++ b/RNS/Interfaces/TCPInterface.py @@ -495,6 +495,7 @@ class TCPServerInterface(Interface): def __str__(self): return "TCPServerInterface["+self.name+"/"+self.bind_ip+":"+str(self.bind_port)+"]" + class TCPInterfaceHandler(socketserver.BaseRequestHandler): def __init__(self, callback, *args, **keys): self.callback = callback diff --git a/RNS/Transport.py b/RNS/Transport.py index e78d7c8..466ee9c 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -1984,10 +1984,27 @@ class Transport: @staticmethod def detach_interfaces(): - for interface in Transport.interfaces: - interface.detach() + detachable_interfaces = [] + for interface in Transport.interfaces: + # Currently no rules are being applied + # here, and all interfaces will be sent + # the detach call on RNS teardown. + if True: + detachable_interfaces.append(interface) + else: + pass + for interface in Transport.local_client_interfaces: + # Currently no rules are being applied + # here, and all interfaces will be sent + # the detach call on RNS teardown. + if True: + detachable_interfaces.append(interface) + else: + pass + + for interface in detachable_interfaces: interface.detach() @staticmethod @@ -2035,8 +2052,10 @@ class Transport: return announce_emitted @staticmethod - def exit_handler(): + def save_packet_hashlist(): try: + save_start = time.time() + if not RNS.Reticulum.transport_enabled(): Transport.packet_hashlist = [] else: @@ -2047,10 +2066,21 @@ class Transport: file.write(umsgpack.packb(Transport.packet_hashlist)) file.close() + save_time = time.time() - save_start + if save_time < 1: + time_str = str(round(save_time*1000,2))+"ms" + else: + time_str = str(round(save_time,2))+"s" + RNS.log("Saved packet hashlist in "+time_str, RNS.LOG_VERBOSE) + except Exception as e: RNS.log("Could not save packet hashlist to storage, the contained exception was: "+str(e), RNS.LOG_ERROR) + + @staticmethod + def save_path_table(): if not Transport.owner.is_connected_to_shared_instance: + save_start = time.time() RNS.log("Saving path table to storage...", RNS.LOG_VERBOSE) try: serialised_destinations = [] @@ -2091,10 +2121,21 @@ class Transport: file = open(destination_table_path, "wb") file.write(umsgpack.packb(serialised_destinations)) file.close() - RNS.log("Done saving "+str(len(serialised_destinations))+" path table entries to storage", RNS.LOG_VERBOSE) + + save_time = time.time() - save_start + if save_time < 1: + time_str = str(round(save_time*1000,2))+"ms" + else: + time_str = str(round(save_time,2))+"s" + RNS.log("Saved "+str(len(serialised_destinations))+" path table entries in "+time_str, RNS.LOG_VERBOSE) + except Exception as e: RNS.log("Could not save path table to storage, the contained exception was: "+str(e), RNS.LOG_ERROR) + @staticmethod + def save_tunnel_table(): + if not Transport.owner.is_connected_to_shared_instance: + save_start = time.time() RNS.log("Saving tunnel table to storage...", RNS.LOG_VERBOSE) try: serialised_tunnels = [] @@ -2143,6 +2184,18 @@ class Transport: file = open(tunnels_path, "wb") file.write(umsgpack.packb(serialised_tunnels)) file.close() - RNS.log("Done saving "+str(len(serialised_tunnels))+" tunnel table entries to storage", RNS.LOG_VERBOSE) + + save_time = time.time() - save_start + if save_time < 1: + time_str = str(round(save_time*1000,2))+"ms" + else: + time_str = str(round(save_time,2))+"s" + RNS.log("Saved "+str(len(serialised_tunnels))+" tunnel table entries in "+time_str, RNS.LOG_VERBOSE) except Exception as e: - RNS.log("Could not save tunnel table to storage, the contained exception was: "+str(e), RNS.LOG_ERROR) \ No newline at end of file + RNS.log("Could not save tunnel table to storage, the contained exception was: "+str(e), RNS.LOG_ERROR) + + @staticmethod + def exit_handler(): + Transport.save_packet_hashlist() + Transport.save_path_table() + Transport.save_tunnel_table() \ No newline at end of file