def metahandler( conv, msg ): #-------------------------------------------------------------------------- """ Handle the connection establishment. Each side starts by sending their known babblers encoded as JSON data. Parse this data using simplejson and load them one by one by calling 'loadbabbler' method. After the babblers have been loaded they are stored in the database and a request for a update of the service list is sended. :param conv: conversation instance from which the message has been received :param msg: actual message data as string """ ssldebug("Synchronizing babblers with %s" % conv.id) table = simplejson.loads(msg) for identifier in table: babbler.loadbabbler(identifier, table[identifier], conv) savebabblertodb(babbler) ssldebug("Synchronizing done with %s" % conv.id) conv.senddata("SREQ", conv.getmessagesequence(), "Service request")
def startpolicing(): def processserviceupdate( document ): #-------------------------------------------------------------------------- """ Handle a update of service list. Will be called by couchDB change notifier if a babbler's service list is altered by receiving a service update. New services are simply queued in the supervisor instance and no longer existing services are removed. :param document: document which has been altered (passed by CouchDBHandler.watchdbthreading). """ try: services = simplejson.loads(watchDB.read(document))["services"] for index in services: service = services[index] uid = "%s/%s" % (document, index) checker.queueservice(uid, service["proto"], service["ipv4"], service["port"], service["timeout"], 200, 180 ) except KeyboardInterrupt: raise except: traceback.print_exc() finally: checker.removeobsoleteservices(document) #-------------------------------------------------------------------------- handler = {"HTTP":HTTPService} checker = Supervisor(handler) try: resultDB = CouchDBManager("localhost", "5984", "gossip_watchresults") watchDB = CouchDBManager("localhost", "5984", "gossip_watchlist") watchDB.watchdbthreading(processserviceupdate, lock=checker.servicelock) while True: if not checker.isqueueempty(): wait = int(checker.getnextschedule() - time.time()) else: wait = 30 if wait <= 0: checker.checkservice() else: resultDB.write("results", checker.getresults()) resultDB.compact() time.sleep(wait) ssldebug("%d service(s) currently watched..." % checker.getservicecount()) finally: watchDB.shutdown = True
sampleconfig = '{' + \ '"host": ["localhost"],' + \ '"port": [50000],' + \ '"maxconv": 25,' + \ '"debug": 0,' + \ '"verbose": 1,' + \ '"version": 1,' + \ '"certificates": {' + \ '"key": "certificates/localserv.pem",' + \ '"certificate": "certificates/localservcert.pem",' + \ '"ca": "certificates/astaro-ca.pem"' + \ '}' + \ '}' ssldebug(sampleconfig) configdb.write("self", sampleconfig) servdb = CouchDBManager("localhost", "5984", "gossip_watchlist") sampleservice = '{"services":{' + \ '"sample_service":{'+\ '"google": {' + \ '"proto": "HTTP",' + \ '"port": 80,' + \ '"timeout": 600,' + \ '"ipv4": "www.google.at"' + \ '}' + \ '}' + \ '}}'