Skip to content

Commit

Permalink
Updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
markqvist committed Jan 19, 2025
1 parent 9bc55a9 commit 9e8d71d
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 52 deletions.
3 changes: 2 additions & 1 deletion Examples/Announce.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import argparse
import random
import sys
import RNS

# Let's define an app name. We'll use this for all
Expand Down Expand Up @@ -168,4 +169,4 @@ def received_announce(self, destination_hash, announced_identity, app_data):

except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
2 changes: 1 addition & 1 deletion Examples/Broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ def broadcastLoop(destination):

except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
7 changes: 3 additions & 4 deletions Examples/Buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
exit()
sys.exit(0)

# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
Expand Down Expand Up @@ -254,9 +254,8 @@ def link_closed(link):
else:
RNS.log("Link closed, exiting now")

RNS.Reticulum.exit_handler()
time.sleep(1.5)
os._exit(0)
sys.exit(0)

# When the buffer has new data, read it and write it to the terminal.
def client_buffer_ready(ready_bytes: int):
Expand Down Expand Up @@ -320,4 +319,4 @@ def client_buffer_ready(ready_bytes: int):

except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
11 changes: 5 additions & 6 deletions Examples/Channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def server(configpath):
def server_loop(destination):
# Let the user know that everything is ready
RNS.log(
"Link example "+
"Channel example "+
RNS.prettyhexrep(destination.hash)+
" running, waiting for a connection."
)
Expand Down Expand Up @@ -212,7 +212,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
exit()
sys.exit(0)

# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
Expand Down Expand Up @@ -276,7 +276,7 @@ def client_loop():
packed_size = len(message.pack())
channel = server_link.get_channel()
if channel.is_ready_to_send():
if packed_size <= channel.MDU:
if packed_size <= channel.mdu:
channel.send(message)
else:
RNS.log(
Expand Down Expand Up @@ -321,9 +321,8 @@ def link_closed(link):
else:
RNS.log("Link closed, exiting now")

RNS.Reticulum.exit_handler()
time.sleep(1.5)
os._exit(0)
sys.exit(0)

# When a packet is received over the channel, we
# simply print out the data.
Expand Down Expand Up @@ -387,4 +386,4 @@ def client_message_received(message):

except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
5 changes: 3 additions & 2 deletions Examples/Echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
##########################################################

import argparse
import sys
import RNS

# Let's define an app name. We'll use this for all
Expand Down Expand Up @@ -130,7 +131,7 @@ def client(destination_hexhash, configpath, timeout=None):
except Exception as e:
RNS.log("Invalid destination entered. Check your input!")
RNS.log(str(e)+"\n")
exit()
sys.exit(0)

# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
Expand Down Expand Up @@ -328,4 +329,4 @@ def packet_timed_out(receipt):
client(args.destination, configarg, timeout=timeoutarg)
except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
9 changes: 4 additions & 5 deletions Examples/Filetransfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
exit()
sys.exit(0)

# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
Expand Down Expand Up @@ -462,7 +462,7 @@ def filelist_timeout_job():
global server_files
if len(server_files) == 0:
RNS.log("Timed out waiting for filelist, exiting")
os._exit(0)
sys.exit(0)


# When a link is closed, we'll inform the
Expand All @@ -475,9 +475,8 @@ def link_closed(link):
else:
RNS.log("Link closed, exiting now")

RNS.Reticulum.exit_handler()
time.sleep(1.5)
os._exit(0)
sys.exit(0)

# When RNS detects that the download has
# started, we'll update our menu state
Expand Down Expand Up @@ -601,4 +600,4 @@ def clear_screen():

except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
7 changes: 3 additions & 4 deletions Examples/Identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
exit()
sys.exit(0)

# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
Expand Down Expand Up @@ -245,9 +245,8 @@ def link_closed(link):
else:
RNS.log("Link closed, exiting now")

RNS.Reticulum.exit_handler()
time.sleep(1.5)
os._exit(0)
sys.exit(0)

# When a packet is received over the link, we
# simply print out the data.
Expand Down Expand Up @@ -311,4 +310,4 @@ def client_packet_received(message, packet):

except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
7 changes: 3 additions & 4 deletions Examples/Link.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
exit()
sys.exit(0)

# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
Expand Down Expand Up @@ -222,9 +222,8 @@ def link_closed(link):
else:
RNS.log("Link closed, exiting now")

RNS.Reticulum.exit_handler()
time.sleep(1.5)
os._exit(0)
sys.exit(0)

# When a packet is received over the link, we
# simply print out the data.
Expand Down Expand Up @@ -288,4 +287,4 @@ def client_packet_received(message, packet):

except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
3 changes: 2 additions & 1 deletion Examples/Minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
##########################################################

import argparse
import sys
import RNS

# Let's define an app name. We'll use this for all
Expand Down Expand Up @@ -98,4 +99,4 @@ def announceLoop(destination):

except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
5 changes: 3 additions & 2 deletions Examples/Ratchets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
##########################################################

import argparse
import sys
import RNS

# Let's define an app name. We'll use this for all
Expand Down Expand Up @@ -138,7 +139,7 @@ def client(destination_hexhash, configpath, timeout=None):
except Exception as e:
RNS.log("Invalid destination entered. Check your input!")
RNS.log(str(e)+"\n")
exit()
sys.exit(0)

# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
Expand Down Expand Up @@ -337,4 +338,4 @@ def packet_timed_out(receipt):
client(args.destination, configarg, timeout=timeoutarg)
except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
7 changes: 3 additions & 4 deletions Examples/Request.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
exit()
sys.exit(0)

# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
Expand Down Expand Up @@ -226,9 +226,8 @@ def link_closed(link):
else:
RNS.log("Link closed, exiting now")

RNS.Reticulum.exit_handler()
time.sleep(1.5)
os._exit(0)
sys.exit(0)


##########################################################
Expand Down Expand Up @@ -284,4 +283,4 @@ def link_closed(link):

except KeyboardInterrupt:
print("")
exit()
sys.exit(0)
26 changes: 8 additions & 18 deletions Examples/Speedtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ def server_packet_received(message, packet):
time.sleep(0.2)
rc = 0
received_data = 0
# latest_client_link.teardown()
# os._exit(0)


##########################################################
Expand All @@ -159,6 +157,7 @@ def server_packet_received(message, packet):

# A reference to the server link
server_link = None
should_quit = False

# This initialisation is executed when the users chooses
# to run as a client
Expand All @@ -175,7 +174,7 @@ def client(destination_hexhash, configpath):
destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
exit()
sys.exit(0)

# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
Expand Down Expand Up @@ -216,24 +215,15 @@ def client(destination_hexhash, configpath):
client_loop()

def client_loop():
global server_link
global server_link, should_quit

# Wait for the link to become active
while not server_link:
time.sleep(0.1)

should_quit = False
while not should_quit:
try:
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
time.sleep(0.2)

# This function is called when a link
# has been established with the server
Expand Down Expand Up @@ -276,17 +266,17 @@ def link_established(link):
# When a link is closed, we'll inform the
# user, and exit the program
def link_closed(link):
global should_quit
if link.teardown_reason == RNS.Link.TIMEOUT:
RNS.log("The link timed out, exiting now")
elif link.teardown_reason == RNS.Link.DESTINATION_CLOSED:
RNS.log("The link was closed by the server, exiting now")
else:
RNS.log("Link closed, exiting now")

RNS.Reticulum.exit_handler()

should_quit = True
time.sleep(1.5)
os._exit(0)
sys.exit(0)

def client_packet_received(message, packet):
pass
Expand Down Expand Up @@ -344,4 +334,4 @@ def client_packet_received(message, packet):

except KeyboardInterrupt:
print("")
exit()
sys.exit(0)

0 comments on commit 9e8d71d

Please sign in to comment.