#!/usr/bin/env python3 # -*- coding: utf-8 -*- """This is some demo code showing how a BRWSKY registrar would flood its contact details to an ANIMA network using GRASP. """ import grasp import threading import time import socket ################################### # Utility routine for debugging: # Print obj_registry and flood cache ################################### def dump_some(): grasp.tprint("Objective registry contents:") for x in grasp._obj_registry: o= x.objective grasp.tprint(o.name,"ASA:",x.asa_id,"Listen:",x.listening,"Neg:",o.neg, "Synch:",o.synch,"Count:",o.loop_count,"Value:",o.value) grasp.tprint("Flood cache contents:") for x in grasp._flood_cache: grasp.tprint(x.objective.name,"count:",x.objective.loop_count,"value:", x.objective.value,"source",x.source.locator, x.source.protocol, x.source.port,"expiry",x.source.expire) ################################### # Main thread starts here ################################### grasp.tprint("Reggie is starting up") #grasp.test_mode = True # set if you want detailed diagnostics time.sleep(1) # just to avoid mixed up print output #################################### # Register ASA/objectives #################################### _ok,_asa_nonce = grasp.register_asa("Reggie") if _ok: grasp.tprint("ASA Reggie registered OK") else: exit() # Firstly, do magic stuff to create a TCP port for EST. # For this demo, we just make up some numbers: est_port = 80 est_proto = socket.IPPROTO_TCP est_address = grasp._my_locator # current address determined by GRASP kernel est_ttl = 60000 #milliseconds to live of the announcement # Construct a correponding asa_locator est_locator = grasp.asa_locator(est_address,0,False) est_locator.is_ipaddress = True est_locator.protocol = est_proto est_locator.port = est_port # Construct the GRASP objective radius = 6 # Limit the radius of multicast relaying priority = 10 # Emulating DNSSD SRV priority weight = 73 # Emulating DNSSD SRV weight # List of the supported methods methods = ["BRWSKY_TLS_TCP", "BRWSKY_COAP_IPIP"] est_obj = grasp.objective("AN_registrar") est_obj.loop_count = radius est_obj.synch = True # Because it's synched, not negotiated est_obj.value = [radius, priority, weight, methods] # (More elegantly, we could define a Python class for this) _ok, _temp = grasp.register_obj(_asa_nonce,est_obj) if _ok: grasp.tprint("Objective", est_obj.name, "registered OK") else: exit() #################################### # Flood objective repeatedly #################################### class _flooder(threading.Thread): """Thread to flood objective repeatedly""" def __init__(self): threading.Thread.__init__(self) def run(self): while True: time.sleep(10) grasp.flood(_asa_nonce, est_locator, est_ttl, est_obj) time.sleep(10) if grasp.test_mode: dump_some() #bump the port number to imitate multiple instances est_locator.port += 1 if est_locator.port == 83: est_locator.port = 80 _flooder().start() grasp.tprint("Flooding objective for ever") ################################### # Listen for requests ################################### # Here, launch a thread to do the real work of the regsitrar # via port est_port grasp.tprint("Pretending to listen to port", est_port) ################################### # Do whatever needs to be done in the main thread ################################### while True: dump_some() time.sleep(10)