Added timeout to rnpath utility

This commit is contained in:
Mark Qvist 2022-04-20 13:40:07 +02:00
parent 402b5fc461
commit 4af14a712c

View File

@ -30,7 +30,7 @@ import argparse
from RNS._version import __version__ from RNS._version import __version__
def program_setup(configdir, table, drop, destination_hexhash, verbosity): def program_setup(configdir, table, drop, destination_hexhash, verbosity, timeout):
if table: if table:
reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity)
table = sorted(reticulum.get_path_table(), key=lambda e: (e["interface"], e["hops"]) ) table = sorted(reticulum.get_path_table(), key=lambda e: (e["interface"], e["hops"]) )
@ -85,12 +85,14 @@ def program_setup(configdir, table, drop, destination_hexhash, verbosity):
i = 0 i = 0
syms = "⢄⢂⢁⡁⡈⡐⡠" syms = "⢄⢂⢁⡁⡈⡐⡠"
while not RNS.Transport.has_path(destination_hash): limit = time.time()+timeout
while not RNS.Transport.has_path(destination_hash) and time.time()<limit:
time.sleep(0.1) time.sleep(0.1)
print(("\b\b"+syms[i]+" "), end="") print(("\b\b"+syms[i]+" "), end="")
sys.stdout.flush() sys.stdout.flush()
i = (i+1)%len(syms) i = (i+1)%len(syms)
if RNS.Transport.has_path(destination_hash):
hops = RNS.Transport.hops_to(destination_hash) hops = RNS.Transport.hops_to(destination_hash)
next_hop = RNS.prettyhexrep(reticulum.get_next_hop(destination_hash)) next_hop = RNS.prettyhexrep(reticulum.get_next_hop(destination_hash))
next_hop_interface = reticulum.get_next_hop_if_name(destination_hash) next_hop_interface = reticulum.get_next_hop_if_name(destination_hash)
@ -101,6 +103,8 @@ def program_setup(configdir, table, drop, destination_hexhash, verbosity):
ms = "" ms = ""
print("\rPath found, destination "+RNS.prettyhexrep(destination_hash)+" is "+str(hops)+" hop"+ms+" away via "+next_hop+" on "+next_hop_interface) print("\rPath found, destination "+RNS.prettyhexrep(destination_hash)+" is "+str(hops)+" hop"+ms+" away via "+next_hop+" on "+next_hop_interface)
else:
print("\r \rPath not found")
def main(): def main():
@ -136,6 +140,15 @@ def main():
default=False default=False
) )
parser.add_argument(
"-w",
action="store",
metavar="seconds",
type=float,
help="timeout before giving up",
default=15
)
parser.add_argument( parser.add_argument(
"destination", "destination",
nargs="?", nargs="?",
@ -163,7 +176,8 @@ def main():
table = args.table, table = args.table,
drop = args.drop, drop = args.drop,
destination_hexhash = args.destination, destination_hexhash = args.destination,
verbosity = args.verbose verbosity = args.verbose,
timeout = args.w,
) )
except KeyboardInterrupt: except KeyboardInterrupt: