Reticulum/Examples/Minimal.py

101 lines
3.4 KiB
Python
Raw Normal View History

2020-04-22 04:07:13 -06:00
##########################################################
# This RNS example demonstrates a minimal setup, that #
# will start up the Reticulum Network Stack, generate a #
# new destination, and let the user send an announce. #
##########################################################
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 02:57:54 -06:00
APP_NAME = "example_utilities"
2020-04-22 04:07:13 -06:00
# This initialisation is executed when the program is started
def program_setup(configpath):
2020-08-13 04:15:56 -06:00
# We must first initialise Reticulum
reticulum = RNS.Reticulum(configpath)
# Randomly create a new identity for our example
identity = RNS.Identity()
2020-04-22 04:07:13 -06:00
2020-08-13 04:15:56 -06:00
# Using the identity we just created, we create a destination.
# Destinations are endpoints in Reticulum, that can be addressed
# and communicated with. Destinations can also announce their
# existence, which will let the network know they are reachable
# and autoomatically create paths to them, from anywhere else
# in the network.
destination = RNS.Destination(
identity,
RNS.Destination.IN,
RNS.Destination.SINGLE,
APP_NAME,
"minimalsample"
)
2020-04-22 04:07:13 -06:00
2020-08-13 04:15:56 -06:00
# We configure the destination to automatically prove all
# packets adressed to it. By doing this, RNS will automatically
# generate a proof for each incoming packet and transmit it
# back to the sender of that packet. This will let anyone that
# tries to communicate with the destination know whether their
# communication was received correctly.
destination.set_proof_strategy(RNS.Destination.PROVE_ALL)
# Everything's ready!
# Let's hand over control to the announce loop
announceLoop(destination)
2020-04-22 04:07:13 -06:00
def announceLoop(destination):
2020-08-13 04:15:56 -06:00
# Let the user know that everything is ready
RNS.log(
"Minimal example "+
RNS.prettyhexrep(destination.hash)+
" running, hit enter to manually send an announce (Ctrl-C to quit)"
)
2020-04-22 04:07:13 -06:00
2020-08-13 04:15:56 -06:00
# We enter a loop that runs until the users exits.
# If the user hits enter, we will announce our server
# destination on the network, which will let clients
# know how to create messages directed towards it.
while True:
entered = input()
destination.announce()
RNS.log("Sent announce from "+RNS.prettyhexrep(destination.hash))
2020-04-22 04:07:13 -06:00
##########################################################
#### Program Startup #####################################
##########################################################
# This part of the program gets run at startup,
# and parses input from the user, and then starts
# the desired program mode.
if __name__ == "__main__":
2020-08-13 04:15:56 -06:00
try:
parser = argparse.ArgumentParser(
description="Minimal example to start Reticulum and create a destination"
)
parser.add_argument(
"--config",
action="store",
default=None,
help="path to alternative Reticulum config directory",
type=str
)
2020-08-13 04:15:56 -06:00
args = parser.parse_args()
2020-04-22 04:07:13 -06:00
2020-08-13 04:15:56 -06:00
if args.config:
configarg = args.config
else:
configarg = None
2020-04-22 04:07:13 -06:00
2020-08-13 04:15:56 -06:00
program_setup(configarg)
2020-04-22 04:07:13 -06:00
2020-08-13 04:15:56 -06:00
except KeyboardInterrupt:
print("")
exit()