def run(self): # counter to timeout split horzion checkround = 0 # the follwing is like the RIP protocol # like Bellman-Ford-Algorithmus smsgwglobals.wislogger.debug("ROUTER: Starting") # 1. request routing # if we could not get anything, shit happens Helper.requestrouting(initial=True) # 2. startup update timer while not self.e.isSet(): # starting router run smsgwglobals.wislogger.debug("ROUTER: Starting router run") # raise timeouts smsgwglobals.wislogger.debug("ROUTER: Starting raise obsolete") wisglobals.rdb.raise_obsolete() # check local table if there are obsoletes smsgwglobals.wislogger.debug("ROUTER: Starting delete obsolete") wisglobals.rdb.delete_routing() # send out table smsgwglobals.wislogger.debug("ROUTER: Starting sending table") Helper.receiverouting() # remove dead direcly connected smsgwglobals.wislogger.debug("ROUTER: Starting delete directly") if checkround == 4: checkround == 0 Helper.checkrouting() else: checkround = checkround + 1 smsgwglobals.wislogger.debug("ROUTER: Starting sleep") time.sleep(25) smsgwglobals.wislogger.debug("ROUTER: Finishing router run") smsgwglobals.wislogger.debug("ROUTER: Stopped")
def api(self, arg, **params): cl = cherrypy.request.headers['Content-Length'] rawbody = cherrypy.request.body.read(int(cl)) smsgwglobals.wislogger.debug(rawbody) plaintext = GlobalHelper.decodeAES(rawbody) smsgwglobals.wislogger.debug(plaintext) data = json.loads(plaintext) if arg == "watchdog": if data["run"] == "True": self.triggerwatchdog() else: cherrypy.response.status = 400 if arg == "heartbeat": if "routingid" in data: smsgwglobals.wislogger.debug(data["routingid"]) try: count = wisglobals.rdb.raise_heartbeat(data["routingid"]) if count == 0: smsgwglobals.wislogger.debug("COUNT: " + str(count)) cherrypy.response.status = 400 except error.DatabaseError: cherrypy.response.status = 400 else: cherrypy.response.status = 400 if arg == "receiverouting": try: wisglobals.rdb.merge_routing(data) except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message) if arg == "requestrouting": if data["get"] != "peers": cherrypy.response.status = 400 return smsgwglobals.wislogger.debug("Sending routing table to you") try: erg = wisglobals.rdb.read_routing() jerg = json.dumps(erg) data = GlobalHelper.encodeAES(jerg) return data except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message) if arg == "managemodem": try: if data["action"] == "register": smsgwglobals.wislogger.debug("managemodem register") smsgwglobals.wislogger.debug(wisglobals.wisid) # add wisid to data object data["wisid"] = wisglobals.wisid # store date in routing table wisglobals.rdb.write_routing(data) # call receiverouting to distribute routing Helper.receiverouting() elif data["action"] == "unregister": smsgwglobals.wislogger.debug("managemodem unregister") routingid = data["routingid"] wisglobals.rdb.change_obsolete(routingid, 14) if routingid in wisglobals.watchdogRouteThread: wisglobals.watchdogRouteThread[routingid].terminate() wisglobals.watchdogRouteThread.pop(routingid) wisglobals.watchdogRouteThreadNotify.pop(routingid) wisglobals.watchdogRouteThreadQueue.pop(routingid) Helper.receiverouting() else: return False except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message) if arg == "deligatesms": if "sms" in data: smsgwglobals.wislogger.debug(data["sms"]) try: sms = Smstransfer(**data["sms"]) sms.smsdict["status"] = -1 sms.writetodb() self.triggerwatchdog() except error.DatabaseError: cherrypy.response.status = 400 else: cherrypy.response.status = 400 if arg == "router": if data["action"] == "status": smsgwglobals.wislogger.debug("API: " + data["action"]) if wisglobals.routerThread is None: cherrypy.response.status = 200 data = GlobalHelper.encodeAES('{"ROUTER":"noobject"}') return data if wisglobals.routerThread.isAlive(): cherrypy.response.status = 200 data = GlobalHelper.encodeAES('{"ROUTER":"alive"}') return data else: cherrypy.response.status = 200 data = GlobalHelper.encodeAES('{"ROUTER":"dead"}') return data if arg == "getsms": if data["get"] != "sms": cherrypy.response.status = 400 return if "date" in data: date = data["date"] smsgwglobals.wislogger.debug("API: " + date) else: date = None smsgwglobals.wislogger.debug("Sending SMS Table") smsgwglobals.wislogger.debug("Sending SMS Table date: " + str(date)) try: db = Database() erg = db.read_sms_date(date=date) jerg = json.dumps(erg) data = GlobalHelper.encodeAES(jerg) return data except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message) if arg == "get_sms_stats": if data["get"] != "sms": cherrypy.response.status = 400 return try: db = Database() erg = db.read_sms_stats() jerg = json.dumps(erg) data = GlobalHelper.encodeAES(jerg) return data except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message)
def api(self, arg, **params): cl = cherrypy.request.headers['Content-Length'] rawbody = cherrypy.request.body.read(int(cl)) smsgwglobals.wislogger.debug(rawbody) plaintext = GlobalHelper.decodeAES(rawbody) smsgwglobals.wislogger.debug(plaintext) data = json.loads(plaintext) if arg == "watchdog": if data["run"] == "True": self.triggerwatchdog() else: cherrypy.response.status = 400 if arg == "heartbeat": if "routingid" in data: smsgwglobals.wislogger.debug(data["routingid"]) try: count = wisglobals.rdb.raise_heartbeat(data["routingid"]) if count == 0: smsgwglobals.wislogger.debug("COUNT: " + str(count)) cherrypy.response.status = 400 except error.DatabaseError: cherrypy.response.status = 400 else: cherrypy.response.status = 400 if arg == "receiverouting": try: wisglobals.rdb.merge_routing(data) except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message) if arg == "requestrouting": if data["get"] != "peers": cherrypy.response.status = 400 return smsgwglobals.wislogger.debug("Sending routing table to you") try: erg = wisglobals.rdb.read_routing() jerg = json.dumps(erg) data = GlobalHelper.encodeAES(jerg) return data except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message) if arg == "managemodem": try: if data["action"] == "register": smsgwglobals.wislogger.debug("managemodem register") smsgwglobals.wislogger.debug(wisglobals.wisid) # add wisid to data object data["wisid"] = wisglobals.wisid # store date in routing table wisglobals.rdb.write_routing(data) # call receiverouting to distribute routing Helper.receiverouting() elif data["action"] == "unregister": smsgwglobals.wislogger.debug("managemodem unregister") wisglobals.rdb.change_obsolete(data["routingid"], 14) Helper.receiverouting() else: return False except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message) if arg == "deligatesms": if "sms" in data: smsgwglobals.wislogger.debug(data["sms"]) try: sms = Smstransfer(**data["sms"]) sms.smsdict["status"] = 1 sms.writetodb() self.triggerwatchdog() except error.DatabaseError: cherrypy.response.status = 400 else: cherrypy.response.status = 400 if arg == "router": if data["action"] == "status": smsgwglobals.wislogger.debug("API: " + data["action"]) if wisglobals.routerThread is None: cherrypy.response.status = 200 data = GlobalHelper.encodeAES('{"ROUTER":"noobject"}') return data if wisglobals.routerThread.isAlive(): cherrypy.response.status = 200 data = GlobalHelper.encodeAES('{"ROUTER":"alive"}') return data else: cherrypy.response.status = 200 data = GlobalHelper.encodeAES('{"ROUTER":"dead"}') return data if arg == "getsms": if data["get"] != "sms": cherrypy.response.status = 400 return if "date" in data: date = data["date"] smsgwglobals.wislogger.debug("API: " + date) else: date = None smsgwglobals.wislogger.debug("Sending SMS Table") smsgwglobals.wislogger.debug("Sending SMS Table date: " + str(date)) try: db = Database() erg = db.read_sms_date(date=date) jerg = json.dumps(erg) data = GlobalHelper.encodeAES(jerg) return data except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message)