Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modernize #568

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ This beta release brings a range of improvements and bugfixes.
- Improved documentation.
- Improved request timeouts and handling.
- Improved link establishment.
- Improved resource transfer timing.
- Improved resource transfer timing.

**Fixed bugs**
- Fixed a race condition in inbound proof handling.
Expand Down
20 changes: 7 additions & 13 deletions Examples/Announce.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
def program_setup(configpath):
# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)

# Randomly create a new identity for our example
identity = RNS.Identity()

Expand Down Expand Up @@ -70,7 +70,7 @@ def program_setup(configpath):

# We register the announce handler with Reticulum
RNS.Transport.register_announce_handler(announce_handler)

# Everything's ready!
# Let's hand over control to the announce loop
announceLoop(destination_1, destination_2)
Expand All @@ -86,16 +86,14 @@ def announceLoop(destination_1, destination_2):
# know how to create messages directed towards it.
while True:
entered = input()

# Randomly select a fruit
fruit = fruits[random.randint(0,len(fruits)-1)]

# Send the announce including the app data
destination_1.announce(app_data=fruit.encode("utf-8"))
RNS.log(
"Sent announce from "+
RNS.prettyhexrep(destination_1.hash)+
" ("+destination_1.name+")"
f"Sent announce from {RNS.prettyhexrep(destination_1.hash)} ({destination_1.name})"
)

# Randomly select a noble gas
Expand All @@ -104,9 +102,7 @@ def announceLoop(destination_1, destination_2):
# Send the announce including the app data
destination_2.announce(app_data=noble_gas.encode("utf-8"))
RNS.log(
"Sent announce from "+
RNS.prettyhexrep(destination_2.hash)+
" ("+destination_2.name+")"
f"Sent announce from {RNS.prettyhexrep(destination_2.hash)} ({destination_2.name})"
)

# We will need to define an announce handler class that
Expand All @@ -126,14 +122,12 @@ def __init__(self, aspect_filter=None):
# and cannot use wildcards.
def received_announce(self, destination_hash, announced_identity, app_data):
RNS.log(
"Received an announce from "+
RNS.prettyhexrep(destination_hash)
f"Received an announce from {RNS.prettyhexrep(destination_hash)}"
)

if app_data:
RNS.log(
"The announce contained the following app data: "+
app_data.decode("utf-8")
f"The announce contained the following app data: {app_data.decode('utf-8')}"
)

##########################################################
Expand Down
10 changes: 4 additions & 6 deletions Examples/Broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
def program_setup(configpath, channel=None):
# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)

# If the user did not select a "channel" we use
# a default one called "public_information".
# This "channel" is added to the destination name-
Expand All @@ -40,23 +40,21 @@ def program_setup(configpath, channel=None):
# We specify a callback that will get called every time
# the destination receives data.
broadcast_destination.set_packet_callback(packet_callback)

# Everything's ready!
# Let's hand over control to the main loop
broadcastLoop(broadcast_destination)

def packet_callback(data, packet):
# Simply print out the received data
print("")
print("Received data: "+data.decode("utf-8")+"\r\n> ", end="")
print(f"Received data: {data.decode('utf-8')}\r\n> ", end="")
sys.stdout.flush()

def broadcastLoop(destination):
# Let the user know that everything is ready
RNS.log(
"Broadcast example "+
RNS.prettyhexrep(destination.hash)+
" running, enter text and hit enter to broadcast (Ctrl-C to quit)"
f"Broadcast example {RNS.prettyhexrep(destination.hash)} running, enter text and hit enter to broadcast (Ctrl-C to quit)"
)

# We enter a loop that runs until the users exits.
Expand Down
22 changes: 10 additions & 12 deletions Examples/Buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
def server(configpath):
# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)

# Randomly create a new identity for our example
server_identity = RNS.Identity()

Expand All @@ -61,9 +61,7 @@ def server(configpath):
def server_loop(destination):
# Let the user know that everything is ready
RNS.log(
"Link buffer example "+
RNS.prettyhexrep(destination.hash)+
" running, waiting for a connection."
f"Link buffer example {RNS.prettyhexrep(destination.hash)} running, waiting for a connection."
)

RNS.log("Hit enter to manually send an announce (Ctrl-C to quit)")
Expand All @@ -75,7 +73,7 @@ def server_loop(destination):
while True:
entered = input()
destination.announce()
RNS.log("Sent announce from "+RNS.prettyhexrep(destination.hash))
RNS.log(f"Sent announce from {RNS.prettyhexrep(destination.hash)}")

# When a client establishes a link to our server
# destination, this function will be called with
Expand Down Expand Up @@ -120,9 +118,9 @@ def server_buffer_ready(ready_bytes: int):
data = latest_buffer.read(ready_bytes)
data = data.decode("utf-8")

RNS.log("Received data over the buffer: " + data)
RNS.log(f"Received data over the buffer: {data}")

reply_message = "I received \""+data+"\" over the buffer"
reply_message = f"I received \"{data}\" over the buffer"
reply_message = reply_message.encode("utf-8")
latest_buffer.write(reply_message)
latest_buffer.flush()
Expand Down Expand Up @@ -151,9 +149,9 @@ def client(destination_hexhash, configpath):
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
if len(destination_hexhash) != dest_len:
raise ValueError(
"Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2)
f"Destination length is invalid, must be {dest_len} hexadecimal characters ({dest_len // 2} bytes)."
)

destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
Expand Down Expand Up @@ -223,7 +221,7 @@ def client_loop():


except Exception as e:
RNS.log("Error while sending data over the link buffer: "+str(e))
RNS.log(f"Error while sending data over the link buffer: {e}")
should_quit = True
server_link.teardown()

Expand Down Expand Up @@ -253,7 +251,7 @@ def link_closed(link):
RNS.log("The link was closed by the server, exiting now")
else:
RNS.log("Link closed, exiting now")

RNS.Reticulum.exit_handler()
time.sleep(1.5)
os._exit(0)
Expand All @@ -262,7 +260,7 @@ def link_closed(link):
def client_buffer_ready(ready_bytes: int):
global buffer
data = buffer.read(ready_bytes)
RNS.log("Received data over the link buffer: " + data.decode("utf-8"))
RNS.log(f"Received data over the link buffer: {data.decode('utf-8')}")
print("> ", end=" ")
sys.stdout.flush()

Expand Down
29 changes: 12 additions & 17 deletions Examples/Channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def unpack(self, raw):
def server(configpath):
# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)

# Randomly create a new identity for our link example
server_identity = RNS.Identity()

Expand All @@ -124,9 +124,7 @@ def server(configpath):
def server_loop(destination):
# Let the user know that everything is ready
RNS.log(
"Link example "+
RNS.prettyhexrep(destination.hash)+
" running, waiting for a connection."
f"Link example {RNS.prettyhexrep(destination.hash)} running, waiting for a connection."
)

RNS.log("Hit enter to manually send an announce (Ctrl-C to quit)")
Expand All @@ -138,7 +136,7 @@ def server_loop(destination):
while True:
entered = input()
destination.announce()
RNS.log("Sent announce from "+RNS.prettyhexrep(destination.hash))
RNS.log(f"Sent announce from {RNS.prettyhexrep(destination.hash)}")

# When a client establishes a link to our server
# destination, this function will be called with
Expand Down Expand Up @@ -176,9 +174,9 @@ def server_message_received(message):
#
#
if isinstance(message, StringMessage):
RNS.log("Received data on the link: " + message.data + " (message created at " + str(message.timestamp) + ")")
RNS.log(f"Received data on the link: {message.data} (message created at {message.timestamp})")

reply_message = StringMessage("I received \""+message.data+"\" over the link")
reply_message = StringMessage(f"I received \"{message.data}\" over the link")
latest_client_link.get_channel().send(reply_message)

# Incoming messages are sent to each message
Expand Down Expand Up @@ -206,9 +204,9 @@ def client(destination_hexhash, configpath):
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
if len(destination_hexhash) != dest_len:
raise ValueError(
"Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2)
f"Destination length is invalid, must be {dest_len} hexadecimal characters ({dest_len // 2} bytes)."
)

destination_hash = bytes.fromhex(destination_hexhash)
except:
RNS.log("Invalid destination entered. Check your input!\n")
Expand Down Expand Up @@ -280,17 +278,14 @@ def client_loop():
channel.send(message)
else:
RNS.log(
"Cannot send this packet, the data size of "+
str(packed_size)+" bytes exceeds the link packet MDU of "+
str(channel.MDU)+" bytes",
f"Cannot send this packet, the data size of {packed_size} bytes exceeds the link packet MDU of {channel.MDU} bytes",
RNS.LOG_ERROR
)
else:
RNS.log("Channel is not ready to send, please wait for " +
"pending messages to complete.", RNS.LOG_ERROR)
RNS.log(f"Channel is not ready to send, please wait for pending messages to complete.", RNS.LOG_ERROR)

except Exception as e:
RNS.log("Error while sending data over the link: "+str(e))
RNS.log(f"Error while sending data over the link: {e}")
should_quit = True
server_link.teardown()

Expand Down Expand Up @@ -320,7 +315,7 @@ def link_closed(link):
RNS.log("The link was closed by the server, exiting now")
else:
RNS.log("Link closed, exiting now")

RNS.Reticulum.exit_handler()
time.sleep(1.5)
os._exit(0)
Expand All @@ -329,7 +324,7 @@ def link_closed(link):
# simply print out the data.
def client_message_received(message):
if isinstance(message, StringMessage):
RNS.log("Received data on the link: " + message.data + " (message created at " + str(message.timestamp) + ")")
RNS.log(f"Received data on the link: {message.data} (message created at {message.timestamp})")
print("> ", end=" ")
sys.stdout.flush()

Expand Down
Loading
Loading