From fd243b998f0a2fffb5acc3bbd9a624aa26703869 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sun, 19 May 2024 21:41:34 +1200 Subject: [PATCH] allow requesting path on demand --- web.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/web.py b/web.py index 6a4e299..a62494f 100644 --- a/web.py +++ b/web.py @@ -3,6 +3,7 @@ import argparse import json import os +import time from datetime import datetime, timezone from typing import Callable, List @@ -181,6 +182,23 @@ class ReticulumWebChat: # convert destination hash to bytes destination_hash = bytes.fromhex(destination_hash) + # check if user wants to request the path from the network right now + request_query_param = request.query.get("request", "false") + should_request_now = request_query_param == "true" or request_query_param == "1" + if should_request_now: + + # determine how long we should wait for a path response + timeout_seconds = int(request.query.get("timeout", 15)) + timeout_after_seconds = time.time() + timeout_seconds + + # request path if we don't have it + if not RNS.Transport.has_path(destination_hash): + RNS.Transport.request_path(destination_hash) + + # wait until we have a path, or give up after the configured timeout + while not RNS.Transport.has_path(destination_hash) and time.time() < timeout_after_seconds: + await asyncio.sleep(0.1) + # ensure path is known if not RNS.Transport.has_path(destination_hash): return web.json_response({