/ Examples / Minimal.py
Minimal.py
  1  ##########################################################
  2  # This RNS example demonstrates a minimal setup, that    #
  3  # will start up the Reticulum Network Stack, generate a  #
  4  # new destination, and let the user send an announce.    #
  5  ##########################################################
  6  
  7  import argparse
  8  import sys
  9  import RNS
 10  
 11  # Let's define an app name. We'll use this for all
 12  # destinations we create. Since this basic example
 13  # is part of a range of example utilities, we'll put
 14  # them all within the app namespace "example_utilities"
 15  APP_NAME = "example_utilities"
 16  
 17  # This initialisation is executed when the program is started
 18  def program_setup(configpath):
 19      # We must first initialise Reticulum
 20      reticulum = RNS.Reticulum(configpath)
 21      
 22      # Randomly create a new identity for our example
 23      identity = RNS.Identity()
 24  
 25      # Using the identity we just created, we create a destination.
 26      # Destinations are endpoints in Reticulum, that can be addressed
 27      # and communicated with. Destinations can also announce their
 28      # existence, which will let the network know they are reachable
 29      # and automatically create paths to them, from anywhere else
 30      # in the network.
 31      destination = RNS.Destination(
 32          identity,
 33          RNS.Destination.IN,
 34          RNS.Destination.SINGLE,
 35          APP_NAME,
 36          "minimalsample"
 37      )
 38  
 39      # We configure the destination to automatically prove all
 40      # packets addressed to it. By doing this, RNS will automatically
 41      # generate a proof for each incoming packet and transmit it
 42      # back to the sender of that packet. This will let anyone that
 43      # tries to communicate with the destination know whether their
 44      # communication was received correctly.
 45      destination.set_proof_strategy(RNS.Destination.PROVE_ALL)
 46      
 47      # Everything's ready!
 48      # Let's hand over control to the announce loop
 49      announceLoop(destination)
 50  
 51  
 52  def announceLoop(destination):
 53      # Let the user know that everything is ready
 54      RNS.log(
 55          "Minimal example "+
 56          RNS.prettyhexrep(destination.hash)+
 57          " running, hit enter to manually send an announce (Ctrl-C to quit)"
 58      )
 59  
 60      # We enter a loop that runs until the users exits.
 61      # If the user hits enter, we will announce our server
 62      # destination on the network, which will let clients
 63      # know how to create messages directed towards it.
 64      while True:
 65          entered = input()
 66          destination.announce()
 67          RNS.log("Sent announce from "+RNS.prettyhexrep(destination.hash))
 68  
 69  
 70  ##########################################################
 71  #### Program Startup #####################################
 72  ##########################################################
 73  
 74  # This part of the program gets run at startup,
 75  # and parses input from the user, and then starts
 76  # the desired program mode.
 77  if __name__ == "__main__":
 78      try:
 79          parser = argparse.ArgumentParser(
 80              description="Minimal example to start Reticulum and create a destination"
 81          )
 82  
 83          parser.add_argument(
 84              "--config",
 85              action="store",
 86              default=None,
 87              help="path to alternative Reticulum config directory",
 88              type=str
 89          )
 90  
 91          args = parser.parse_args()
 92  
 93          if args.config:
 94              configarg = args.config
 95          else:
 96              configarg = None
 97  
 98          program_setup(configarg)
 99  
100      except KeyboardInterrupt:
101          print("")
102          sys.exit(0)