From 532f9ee665031558d975072626ecb680524ffd64 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sat, 2 Jul 2022 13:12:54 +0200 Subject: [PATCH] Added cache cleaning --- RNS/Reticulum.py | 43 ++++++++++++++++++++++++++++++++++++++++++- RNS/__init__.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index 74746a8..f41acca 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -40,7 +40,7 @@ import threading import atexit import struct import array -import os.path +import time import os import RNS @@ -126,6 +126,8 @@ class Reticulum: MDU = MTU - HEADER_MAXSIZE - IFAC_MIN_SIZE + CACHE_TIME = 24*60*60 + router = None config = None @@ -143,6 +145,12 @@ class Reticulum: # classes, saving necessary information to disk and carrying # out cleanup operations. + if RNS.Transport.owner.share_instance: + if RNS.Transport.owner.is_shared_instance: + RNS.Transport.owner.__clean_caches() + else: + RNS.Transport.owner.__clean_caches() + RNS.Transport.exit_handler() RNS.Identity.exit_handler() @@ -261,6 +269,8 @@ class Reticulum: self.is_shared_instance = True RNS.log("Started shared instance interface: "+str(interface), RNS.LOG_DEBUG) + self.__clean_caches() + except Exception as e: try: interface = LocalInterface.LocalClientInterface( @@ -285,6 +295,7 @@ class Reticulum: self.is_shared_instance = False self.is_standalone_instance = True self.is_connected_to_shared_instance = False + self.__clean_caches() def __apply_config(self): if "logging" in self.config: @@ -862,6 +873,36 @@ class Reticulum: + def __clean_caches(self): + RNS.log("Cleaning resource and packet caches...", RNS.LOG_DEBUG) + now = time.time() + + # Clean resource caches + for filename in os.listdir(self.resourcepath): + try: + if len(filename) == (RNS.Identity.HASHLENGTH//8)*2: + filepath = self.resourcepath + "/" + filename + mtime = os.path.getmtime(filepath) + age = now - mtime + if age > Reticulum.CACHE_TIME: + os.unlink(filepath) + + except Exception as e: + RNS.log("Error while cleaning resources cache, the contained exception was: "+str(e), RNS.LOG_ERROR) + + # Clean packet caches + for filename in os.listdir(self.cachepath): + try: + if len(filename) == (RNS.Identity.HASHLENGTH//8)*2: + filepath = self.cachepath + "/" + filename + mtime = os.path.getmtime(filepath) + age = now - mtime + if age > Reticulum.CACHE_TIME: + os.unlink(filepath) + + except Exception as e: + RNS.log("Error while cleaning resources cache, the contained exception was: "+str(e), RNS.LOG_ERROR) + def __create_default_config(self): self.config = ConfigObj(__default_rns_config__) self.config.filename = Reticulum.configpath diff --git a/RNS/__init__.py b/RNS/__init__.py index a657ad2..a7f90e1 100755 --- a/RNS/__init__.py +++ b/RNS/__init__.py @@ -172,6 +172,48 @@ def prettysize(num, suffix='B'): return "%.2f%s%s" % (num, last_unit, suffix) +def prettytime(time, verbose=False): + days = int(time // (24 * 3600)) + time = time % (24 * 3600) + hours = int(time // 3600) + time %= 3600 + minutes = int(time // 60) + time %= 60 + seconds = round(time, 2) + + ss = "" if seconds == 1 else "s" + sm = "" if minutes == 1 else "s" + sh = "" if hours == 1 else "s" + sd = "" if days == 1 else "s" + + components = [] + if days > 0: + components.append(str(days)+" day"+sd if verbose else str(days)+"d") + + if hours > 0: + components.append(str(hours)+" hour"+sh if verbose else str(hours)+"h") + + if minutes > 0: + components.append(str(minutes)+" minute"+sm if verbose else str(minutes)+"m") + + if seconds > 0: + components.append(str(seconds)+" second"+ss if verbose else str(seconds)+"s") + + i = 0 + tstr = "" + for c in components: + i += 1 + if i == 1: + pass + elif i < len(components): + tstr += ", " + elif i == len(components): + tstr += " and " + + tstr += c + + return tstr + def phyparams(): print("Required Physical Layer MTU : "+str(Reticulum.MTU)+" bytes") print("Plaintext Packet MDU : "+str(Packet.PLAIN_MDU)+" bytes")