2020-05-14 10:52:41 +00:00
|
|
|
##########################################################
|
|
|
|
# This RNS example demonstrates broadcasting unencrypted #
|
|
|
|
# information to any listening destinations. #
|
|
|
|
##########################################################
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import RNS
|
|
|
|
|
|
|
|
# Let's define an app name. We'll use this for all
|
|
|
|
# destinations we create. Since this basic example
|
|
|
|
# is part of a range of example utilities, we'll put
|
|
|
|
# them all within the app namespace "example_utilities"
|
2021-05-15 08:57:54 +00:00
|
|
|
APP_NAME = "example_utilities"
|
2020-05-14 10:52:41 +00:00
|
|
|
|
|
|
|
# This initialisation is executed when the program is started
|
|
|
|
def program_setup(configpath, channel=None):
|
2020-08-13 10:15:56 +00:00
|
|
|
# We must first initialise Reticulum
|
|
|
|
reticulum = RNS.Reticulum(configpath)
|
2024-10-07 08:52:43 +00:00
|
|
|
|
2020-08-13 10:15:56 +00:00
|
|
|
# If the user did not select a "channel" we use
|
|
|
|
# a default one called "public_information".
|
|
|
|
# This "channel" is added to the destination name-
|
|
|
|
# space, so the user can select different broadcast
|
|
|
|
# channels.
|
|
|
|
if channel == None:
|
|
|
|
channel = "public_information"
|
|
|
|
|
|
|
|
# We create a PLAIN destination. This is an uncencrypted endpoint
|
|
|
|
# that anyone can listen to and send information to.
|
2021-05-17 17:10:11 +00:00
|
|
|
broadcast_destination = RNS.Destination(
|
|
|
|
None,
|
|
|
|
RNS.Destination.IN,
|
|
|
|
RNS.Destination.PLAIN,
|
|
|
|
APP_NAME,
|
|
|
|
"broadcast",
|
|
|
|
channel
|
|
|
|
)
|
2020-08-13 10:15:56 +00:00
|
|
|
|
|
|
|
# We specify a callback that will get called every time
|
|
|
|
# the destination receives data.
|
2021-05-20 13:31:38 +00:00
|
|
|
broadcast_destination.set_packet_callback(packet_callback)
|
2024-10-07 08:52:43 +00:00
|
|
|
|
2020-08-13 10:15:56 +00:00
|
|
|
# Everything's ready!
|
|
|
|
# Let's hand over control to the main loop
|
|
|
|
broadcastLoop(broadcast_destination)
|
2020-05-14 10:52:41 +00:00
|
|
|
|
|
|
|
def packet_callback(data, packet):
|
2020-08-13 10:15:56 +00:00
|
|
|
# Simply print out the received data
|
|
|
|
print("")
|
2024-10-07 08:44:18 +00:00
|
|
|
print(f"Received data: {data.decode('utf-8')}\r\n> ", end="")
|
2020-08-13 10:15:56 +00:00
|
|
|
sys.stdout.flush()
|
2020-05-14 10:52:41 +00:00
|
|
|
|
|
|
|
def broadcastLoop(destination):
|
2020-08-13 10:15:56 +00:00
|
|
|
# Let the user know that everything is ready
|
2021-05-17 17:10:11 +00:00
|
|
|
RNS.log(
|
2024-10-07 08:44:18 +00:00
|
|
|
f"Broadcast example {RNS.prettyhexrep(destination.hash)} running, enter text and hit enter to broadcast (Ctrl-C to quit)"
|
2021-05-17 17:10:11 +00:00
|
|
|
)
|
2020-05-14 10:52:41 +00:00
|
|
|
|
2020-08-13 10:15:56 +00:00
|
|
|
# We enter a loop that runs until the users exits.
|
|
|
|
# If the user hits enter, we will send the information
|
|
|
|
# that the user entered into the prompt.
|
|
|
|
while True:
|
|
|
|
print("> ", end="")
|
|
|
|
entered = input()
|
|
|
|
|
|
|
|
if entered != "":
|
|
|
|
data = entered.encode("utf-8")
|
|
|
|
packet = RNS.Packet(destination, data)
|
|
|
|
packet.send()
|
2020-05-14 10:52:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################
|
|
|
|
#### Program Startup #####################################
|
|
|
|
##########################################################
|
|
|
|
|
|
|
|
# This part of the program gets run at startup,
|
|
|
|
# and parses input from the user, and then starts
|
|
|
|
# the program.
|
|
|
|
if __name__ == "__main__":
|
2020-08-13 10:15:56 +00:00
|
|
|
try:
|
2021-05-17 17:10:11 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Reticulum example demonstrating sending and receiving broadcasts"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--config",
|
|
|
|
action="store",
|
|
|
|
default=None,
|
|
|
|
help="path to alternative Reticulum config directory",
|
|
|
|
type=str
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--channel",
|
|
|
|
action="store",
|
|
|
|
default=None,
|
|
|
|
help="broadcast channel name",
|
|
|
|
type=str
|
|
|
|
)
|
|
|
|
|
2020-08-13 10:15:56 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.config:
|
|
|
|
configarg = args.config
|
|
|
|
else:
|
|
|
|
configarg = None
|
|
|
|
|
|
|
|
if args.channel:
|
|
|
|
channelarg = args.channel
|
|
|
|
else:
|
|
|
|
channelarg = None
|
|
|
|
|
|
|
|
program_setup(configarg, channelarg)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("")
|
|
|
|
exit()
|