/ Examples / Broadcast.py
Broadcast.py
  1  ##########################################################
  2  # This RNS example demonstrates broadcasting unencrypted #
  3  # information to any listening destinations.             #
  4  ##########################################################
  5  
  6  import sys
  7  import argparse
  8  import RNS
  9  
 10  # Let's define an app name. We'll use this for all
 11  # destinations we create. Since this basic example
 12  # is part of a range of example utilities, we'll put
 13  # them all within the app namespace "example_utilities"
 14  APP_NAME = "example_utilities"
 15  
 16  # This initialisation is executed when the program is started
 17  def program_setup(configpath, channel=None):
 18      # We must first initialise Reticulum
 19      reticulum = RNS.Reticulum(configpath)
 20      
 21      # If the user did not select a "channel" we use
 22      # a default one called "public_information".
 23      # This "channel" is added to the destination name-
 24      # space, so the user can select different broadcast
 25      # channels.
 26      if channel == None:
 27          channel = "public_information"
 28  
 29      # We create a PLAIN destination. This is an uncencrypted endpoint
 30      # that anyone can listen to and send information to.
 31      broadcast_destination = RNS.Destination(
 32          None,
 33          RNS.Destination.IN,
 34          RNS.Destination.PLAIN,
 35          APP_NAME,
 36          "broadcast",
 37          channel
 38      )
 39  
 40      # We specify a callback that will get called every time
 41      # the destination receives data.
 42      broadcast_destination.set_packet_callback(packet_callback)
 43      
 44      # Everything's ready!
 45      # Let's hand over control to the main loop
 46      broadcastLoop(broadcast_destination)
 47  
 48  def packet_callback(data, packet):
 49      # Simply print out the received data
 50      print("")
 51      print("Received data: "+data.decode("utf-8")+"\r\n> ", end="")
 52      sys.stdout.flush()
 53  
 54  def broadcastLoop(destination):
 55      # Let the user know that everything is ready
 56      RNS.log(
 57          "Broadcast example "+
 58          RNS.prettyhexrep(destination.hash)+
 59          " running, enter text and hit enter to broadcast (Ctrl-C to quit)"
 60      )
 61  
 62      # We enter a loop that runs until the users exits.
 63      # If the user hits enter, we will send the information
 64      # that the user entered into the prompt.
 65      while True:
 66          print("> ", end="")
 67          entered = input()
 68  
 69          if entered != "":
 70              data    = entered.encode("utf-8")
 71              packet  = RNS.Packet(destination, data)
 72              packet.send()
 73  
 74  
 75  
 76  ##########################################################
 77  #### Program Startup #####################################
 78  ##########################################################
 79  
 80  # This part of the program gets run at startup,
 81  # and parses input from the user, and then starts
 82  # the program.
 83  if __name__ == "__main__":
 84      try:
 85          parser = argparse.ArgumentParser(
 86              description="Reticulum example demonstrating sending and receiving broadcasts"
 87          )
 88  
 89          parser.add_argument(
 90              "--config",
 91              action="store",
 92              default=None,
 93              help="path to alternative Reticulum config directory",
 94              type=str
 95          )
 96  
 97          parser.add_argument(
 98              "--channel",
 99              action="store",
100              default=None,
101              help="broadcast channel name",
102              type=str
103          )
104  
105          args = parser.parse_args()
106  
107          if args.config:
108              configarg = args.config
109          else:
110              configarg = None
111  
112          if args.channel:
113              channelarg = args.channel
114          else:
115              channelarg = None
116  
117          program_setup(configarg, channelarg)
118  
119      except KeyboardInterrupt:
120          print("")
121          sys.exit(0)