diff --git a/meshchat.py b/meshchat.py index 2d77fe2..d8e31f0 100644 --- a/meshchat.py +++ b/meshchat.py @@ -2735,7 +2735,8 @@ def set(self, value: int): lxmf_preferred_propagation_node_last_synced_at = IntConfig("lxmf_preferred_propagation_node_last_synced_at", None) lxmf_local_propagation_node_enabled = BoolConfig("lxmf_local_propagation_node_enabled", False) - +# FIXME: we should probably set this as an instance variable of ReticulumMeshChat so it has a proper home, and pass it in to the constructor? +nomadnet_cached_links = {} class NomadnetDownloader: def __init__(self, destination_hash: bytes, path: str, data: str|None, on_download_success: Callable[[bytes], None], on_download_failure: Callable[[str], None], on_progress_update: Callable[[float], None], timeout: int|None = None): @@ -2752,6 +2753,14 @@ def __init__(self, destination_hash: bytes, path: str, data: str|None, on_downlo # setup link to destination and request download async def download(self, path_lookup_timeout: int = 15, link_establishment_timeout: int = 15): + # use existing established link if it's active + if self.destination_hash in nomadnet_cached_links: + link = nomadnet_cached_links[self.destination_hash] + if link.status is RNS.Link.ACTIVE: + print("[NomadnetDownloader] using existing link for request") + self.link_established(link) + return + # determine when to timeout timeout_after_seconds = time.time() + path_lookup_timeout @@ -2781,6 +2790,7 @@ async def download(self, path_lookup_timeout: int = 15, link_establishment_timeo ) # create link to destination + print("[NomadnetDownloader] establishing new link for request") link = RNS.Link(destination, established_callback=self.link_established) # determine when to timeout @@ -2797,6 +2807,9 @@ async def download(self, path_lookup_timeout: int = 15, link_establishment_timeo # link to destination was established, we should now request the download def link_established(self, link): + # cache link for using in future requests + nomadnet_cached_links[self.destination_hash] = link + # request download over link link.request( self.path,