Updated examples

This commit is contained in:
Mark Qvist 2025-01-19 20:38:41 +01:00
parent 9bc55a9047
commit 9e8d71ddaf
12 changed files with 40 additions and 52 deletions

View File

@ -6,6 +6,7 @@
import argparse import argparse
import random import random
import sys
import RNS import RNS
# Let's define an app name. We'll use this for all # Let's define an app name. We'll use this for all
@ -168,4 +169,4 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -118,4 +118,4 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -157,7 +157,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash) destination_hash = bytes.fromhex(destination_hexhash)
except: except:
RNS.log("Invalid destination entered. Check your input!\n") RNS.log("Invalid destination entered. Check your input!\n")
exit() sys.exit(0)
# We must first initialise Reticulum # We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath) reticulum = RNS.Reticulum(configpath)
@ -254,9 +254,8 @@ def link_closed(link):
else: else:
RNS.log("Link closed, exiting now") RNS.log("Link closed, exiting now")
RNS.Reticulum.exit_handler()
time.sleep(1.5) time.sleep(1.5)
os._exit(0) sys.exit(0)
# When the buffer has new data, read it and write it to the terminal. # When the buffer has new data, read it and write it to the terminal.
def client_buffer_ready(ready_bytes: int): def client_buffer_ready(ready_bytes: int):
@ -320,4 +319,4 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -124,7 +124,7 @@ def server(configpath):
def server_loop(destination): def server_loop(destination):
# Let the user know that everything is ready # Let the user know that everything is ready
RNS.log( RNS.log(
"Link example "+ "Channel example "+
RNS.prettyhexrep(destination.hash)+ RNS.prettyhexrep(destination.hash)+
" running, waiting for a connection." " running, waiting for a connection."
) )
@ -212,7 +212,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash) destination_hash = bytes.fromhex(destination_hexhash)
except: except:
RNS.log("Invalid destination entered. Check your input!\n") RNS.log("Invalid destination entered. Check your input!\n")
exit() sys.exit(0)
# We must first initialise Reticulum # We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath) reticulum = RNS.Reticulum(configpath)
@ -276,7 +276,7 @@ def client_loop():
packed_size = len(message.pack()) packed_size = len(message.pack())
channel = server_link.get_channel() channel = server_link.get_channel()
if channel.is_ready_to_send(): if channel.is_ready_to_send():
if packed_size <= channel.MDU: if packed_size <= channel.mdu:
channel.send(message) channel.send(message)
else: else:
RNS.log( RNS.log(
@ -321,9 +321,8 @@ def link_closed(link):
else: else:
RNS.log("Link closed, exiting now") RNS.log("Link closed, exiting now")
RNS.Reticulum.exit_handler()
time.sleep(1.5) time.sleep(1.5)
os._exit(0) sys.exit(0)
# When a packet is received over the channel, we # When a packet is received over the channel, we
# simply print out the data. # simply print out the data.
@ -387,4 +386,4 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -6,6 +6,7 @@
########################################################## ##########################################################
import argparse import argparse
import sys
import RNS import RNS
# Let's define an app name. We'll use this for all # Let's define an app name. We'll use this for all
@ -130,7 +131,7 @@ def client(destination_hexhash, configpath, timeout=None):
except Exception as e: except Exception as e:
RNS.log("Invalid destination entered. Check your input!") RNS.log("Invalid destination entered. Check your input!")
RNS.log(str(e)+"\n") RNS.log(str(e)+"\n")
exit() sys.exit(0)
# We must first initialise Reticulum # We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath) reticulum = RNS.Reticulum(configpath)
@ -328,4 +329,4 @@ if __name__ == "__main__":
client(args.destination, configarg, timeout=timeoutarg) client(args.destination, configarg, timeout=timeoutarg)
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -224,7 +224,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash) destination_hash = bytes.fromhex(destination_hexhash)
except: except:
RNS.log("Invalid destination entered. Check your input!\n") RNS.log("Invalid destination entered. Check your input!\n")
exit() sys.exit(0)
# We must first initialise Reticulum # We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath) reticulum = RNS.Reticulum(configpath)
@ -462,7 +462,7 @@ def filelist_timeout_job():
global server_files global server_files
if len(server_files) == 0: if len(server_files) == 0:
RNS.log("Timed out waiting for filelist, exiting") RNS.log("Timed out waiting for filelist, exiting")
os._exit(0) sys.exit(0)
# When a link is closed, we'll inform the # When a link is closed, we'll inform the
@ -475,9 +475,8 @@ def link_closed(link):
else: else:
RNS.log("Link closed, exiting now") RNS.log("Link closed, exiting now")
RNS.Reticulum.exit_handler()
time.sleep(1.5) time.sleep(1.5)
os._exit(0) sys.exit(0)
# When RNS detects that the download has # When RNS detects that the download has
# started, we'll update our menu state # started, we'll update our menu state
@ -601,4 +600,4 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -133,7 +133,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash) destination_hash = bytes.fromhex(destination_hexhash)
except: except:
RNS.log("Invalid destination entered. Check your input!\n") RNS.log("Invalid destination entered. Check your input!\n")
exit() sys.exit(0)
# We must first initialise Reticulum # We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath) reticulum = RNS.Reticulum(configpath)
@ -245,9 +245,8 @@ def link_closed(link):
else: else:
RNS.log("Link closed, exiting now") RNS.log("Link closed, exiting now")
RNS.Reticulum.exit_handler()
time.sleep(1.5) time.sleep(1.5)
os._exit(0) sys.exit(0)
# When a packet is received over the link, we # When a packet is received over the link, we
# simply print out the data. # simply print out the data.
@ -311,4 +310,4 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -119,7 +119,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash) destination_hash = bytes.fromhex(destination_hexhash)
except: except:
RNS.log("Invalid destination entered. Check your input!\n") RNS.log("Invalid destination entered. Check your input!\n")
exit() sys.exit(0)
# We must first initialise Reticulum # We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath) reticulum = RNS.Reticulum(configpath)
@ -222,9 +222,8 @@ def link_closed(link):
else: else:
RNS.log("Link closed, exiting now") RNS.log("Link closed, exiting now")
RNS.Reticulum.exit_handler()
time.sleep(1.5) time.sleep(1.5)
os._exit(0) sys.exit(0)
# When a packet is received over the link, we # When a packet is received over the link, we
# simply print out the data. # simply print out the data.
@ -288,4 +287,4 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -5,6 +5,7 @@
########################################################## ##########################################################
import argparse import argparse
import sys
import RNS import RNS
# Let's define an app name. We'll use this for all # Let's define an app name. We'll use this for all
@ -98,4 +99,4 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -5,6 +5,7 @@
########################################################## ##########################################################
import argparse import argparse
import sys
import RNS import RNS
# Let's define an app name. We'll use this for all # Let's define an app name. We'll use this for all
@ -138,7 +139,7 @@ def client(destination_hexhash, configpath, timeout=None):
except Exception as e: except Exception as e:
RNS.log("Invalid destination entered. Check your input!") RNS.log("Invalid destination entered. Check your input!")
RNS.log(str(e)+"\n") RNS.log(str(e)+"\n")
exit() sys.exit(0)
# We must first initialise Reticulum # We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath) reticulum = RNS.Reticulum(configpath)
@ -337,4 +338,4 @@ if __name__ == "__main__":
client(args.destination, configarg, timeout=timeoutarg) client(args.destination, configarg, timeout=timeoutarg)
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -119,7 +119,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash) destination_hash = bytes.fromhex(destination_hexhash)
except: except:
RNS.log("Invalid destination entered. Check your input!\n") RNS.log("Invalid destination entered. Check your input!\n")
exit() sys.exit(0)
# We must first initialise Reticulum # We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath) reticulum = RNS.Reticulum(configpath)
@ -226,9 +226,8 @@ def link_closed(link):
else: else:
RNS.log("Link closed, exiting now") RNS.log("Link closed, exiting now")
RNS.Reticulum.exit_handler()
time.sleep(1.5) time.sleep(1.5)
os._exit(0) sys.exit(0)
########################################################## ##########################################################
@ -284,4 +283,4 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)

View File

@ -149,8 +149,6 @@ def server_packet_received(message, packet):
time.sleep(0.2) time.sleep(0.2)
rc = 0 rc = 0
received_data = 0 received_data = 0
# latest_client_link.teardown()
# os._exit(0)
########################################################## ##########################################################
@ -159,6 +157,7 @@ def server_packet_received(message, packet):
# A reference to the server link # A reference to the server link
server_link = None server_link = None
should_quit = False
# This initialisation is executed when the users chooses # This initialisation is executed when the users chooses
# to run as a client # to run as a client
@ -175,7 +174,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash) destination_hash = bytes.fromhex(destination_hexhash)
except: except:
RNS.log("Invalid destination entered. Check your input!\n") RNS.log("Invalid destination entered. Check your input!\n")
exit() sys.exit(0)
# We must first initialise Reticulum # We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath) reticulum = RNS.Reticulum(configpath)
@ -216,7 +215,7 @@ def client(destination_hexhash, configpath):
client_loop() client_loop()
def client_loop(): def client_loop():
global server_link global server_link, should_quit
# Wait for the link to become active # Wait for the link to become active
while not server_link: while not server_link:
@ -224,16 +223,7 @@ def client_loop():
should_quit = False should_quit = False
while not should_quit: while not should_quit:
try: time.sleep(0.2)
text = input()
# Check if we should quit the example
if text == "quit" or text == "q" or text == "exit":
should_quit = True
server_link.teardown()
except Exception as e:
raise e
# This function is called when a link # This function is called when a link
# has been established with the server # has been established with the server
@ -276,6 +266,7 @@ def link_established(link):
# When a link is closed, we'll inform the # When a link is closed, we'll inform the
# user, and exit the program # user, and exit the program
def link_closed(link): def link_closed(link):
global should_quit
if link.teardown_reason == RNS.Link.TIMEOUT: if link.teardown_reason == RNS.Link.TIMEOUT:
RNS.log("The link timed out, exiting now") RNS.log("The link timed out, exiting now")
elif link.teardown_reason == RNS.Link.DESTINATION_CLOSED: elif link.teardown_reason == RNS.Link.DESTINATION_CLOSED:
@ -283,10 +274,9 @@ def link_closed(link):
else: else:
RNS.log("Link closed, exiting now") RNS.log("Link closed, exiting now")
RNS.Reticulum.exit_handler() should_quit = True
time.sleep(1.5) time.sleep(1.5)
os._exit(0) sys.exit(0)
def client_packet_received(message, packet): def client_packet_received(message, packet):
pass pass
@ -344,4 +334,4 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
exit() sys.exit(0)