def thread_sender(self, mobile_numbers_to_send, json_data, priority, sourceip, xforwardedfor): if isinstance(sourceip, tuple): sourceip = str(sourceip[0]) if isinstance(xforwardedfor, tuple): xforwardedfor = str(xforwardedfor[0]) for targetnr in mobile_numbers_to_send: # this is used for parameter extraction # Create sms data object and make sure that it has a smsid sms_uuid = str(uuid.uuid1()) sms = Smstransfer(content=json_data.get('content'), targetnr=targetnr, priority=priority, appid=json_data.get('appid'), sourceip=sourceip, xforwardedfor=xforwardedfor, smsid=sms_uuid) smsgwglobals.wislogger.debug("WIS: sendsms interface " + str(sms.getjson())) # process sms to insert it into database try: Helper.processsms(sms) smsid = sms.smstransfer["sms"]["smsid"] SMS_QUEUE.put(smsid) except apperror.NoRoutesFoundError: self.triggerwatchdog() pass except apperror.NotAllowedTimeFrame: self.triggerwatchdog() pass
def reprocess_sms(self): if self.allowed_time(): # Read SMS with statuses NoRoutes + NoPossibleRoutes + NotAllowedTimeframe smsen = self.db.read_sms(status=104) smsen = smsen + self.db.read_sms(status=105) if smsen: for sms in smsen: smsgwglobals.wislogger.debug( "REPROCESS_SMS job: processing: " + str(sms)) smstrans = Smstransfer( content=sms.get('content'), targetnr=sms.get('targetnr'), priority=sms.get('priority'), appid=sms.get('appid'), sourceip=sms.get('sourceip'), xforwardedfor=sms.get('xforwardedfor'), smsid=sms.get('smsid')) try: Helper.processsms(smstrans) except apperror.NoRoutesFoundError: pass else: # Add sms to global queue wisglobals.watchdogThread.queue.put( smstrans.smsdict["smsid"]) wisglobals.watchdogThreadNotify.set() else: smsgwglobals.wislogger.debug( "REPROCESS_SMS job: skipping. NO SMS to process") else: smsgwglobals.wislogger.debug( "REPROCESS_SMS job: skipping. Not allowed timeframe")
def sendsms(self, **params): # this is used for parameter extraction # Create sms data object sms = Smstransfer(content=cherrypy.request.params.get('content'), targetnr=cherrypy.request.params.get('mobile'), priority=int(cherrypy.request.params.get('priority')), appid=cherrypy.request.params.get('appid'), sourceip=cherrypy.request.headers.get('Remote-Addr'), xforwardedfor=cherrypy.request.headers.get( 'X-Forwarded-For')) # check if parameters are given resp = XMLResponse() if not sms.smsdict["content"]: self.triggerwatchdog() return resp.errorxml("0001") if not sms.smsdict["targetnr"]: self.triggerwatchdog() return resp.errorxml("0001") if not sms.smsdict["priority"]: self.triggerwatchdog() return resp.errorxml("0001") smsgwglobals.wislogger.debug(sms.getjson()) # process sms to insert it into database try: Helper.processsms(sms) except apperror.NoRoutesFoundError: self.triggerwatchdog() return resp.errorxml("0001") self.triggerwatchdog() return resp.successxml("0001")
def process(self, sms_id): try: db = database.Database() smsen = db.read_sms(smsid=sms_id) if not smsen: smsgwglobals.wislogger.debug("WATCHDOG: no SMS with ID: " + sms_id + " in DB") # Add sms_id back to the queue self.queue.put(sms_id) except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message) # Add sms_id back to the queue self.queue.put(sms_id) # we have sms, just process sms = smsen[0] smsgwglobals.wislogger.debug("WATCHDOG: Process SMS: " + str(sms)) # create smstrans object for easy handling smstrans = Smstransfer(**sms) route = wisglobals.rdb.read_routing(smstrans.smsdict["modemid"]) if route is None or len(route) == 0: smsgwglobals.wislogger.debug("WATCHDOG: ALERT ROUTE LOST") # try to reprocess route try: smstrans.updatedb() Helper.processsms(smstrans) except apperror.NoRoutesFoundError: pass else: self.queue.put(smstrans.smsdict["smsid"]) elif route[0]["wisid"] != wisglobals.wisid: self.deligate(smstrans, route) else: # we have a route, this wis is the correct one # therefore give the sms to the PIS # this is a bad hack to ignore obsolete routes # this may lead to an error, fixme route[:] = [d for d in route if d['obsolete'] < 1] smsgwglobals.wislogger.debug("WATCHDOG: process with route %s ", str(route)) smsgwglobals.wislogger.debug("WATCHDOG: Sending to PIS %s", str(sms)) # only continue if route contains data if len(route) > 0: self.dispatch_sms(smstrans, route) else: # Reprocess try: smstrans.updatedb() Helper.processsms(smstrans) except apperror.NoRoutesFoundError: pass else: self.queue.put(smstrans.smsdict["smsid"])
def sendsms(self, **params): # all parameters to lower case cherrypy.request.params = dict([(x[0].lower(), x[1]) for x in cherrypy.request.params.items()]) priority = 1 if 'priority' in cherrypy.request.params: priority = int(cherrypy.request.params.get('priority')) # this is used for parameter extraction # Create sms data object and make sure that it has a smsid sms = Smstransfer(content=cherrypy.request.params.get('content'), targetnr=cherrypy.request.params.get('mobile'), priority=priority, appid=cherrypy.request.params.get('appid'), sourceip=cherrypy.request.headers.get('Remote-Addr'), xforwardedfor=cherrypy.request.headers.get( 'X-Forwarded-For'), smsid=str(uuid.uuid1())) # check if parameters are given resp = XMLResponse() if not sms.smsdict["content"]: self.triggerwatchdog() return resp.errorxml("0001") if not sms.smsdict["targetnr"]: self.triggerwatchdog() return resp.errorxml("0001") if not sms.smsdict["priority"]: self.triggerwatchdog() return resp.errorxml("0001") smsgwglobals.wislogger.debug("WIS: sendsms iterface " + str(sms.getjson())) # process sms to insert it into database try: Helper.processsms(sms) except apperror.NoRoutesFoundError: self.triggerwatchdog() return resp.errorxml("0001") self.triggerwatchdog() return resp.successxml("0001")
def process(self): smsgwglobals.wislogger.debug("WATCHDOG: processing sms") # check if we have SMS to work on smscount = 0 try: db = database.Database() # cleanup old sms db.delete_old_sms(wisglobals.cleanupseconds) smsen = db.read_sms(status=0) smsen = smsen + db.read_sms(status=1) smscount = len(smsen) if smscount == 0: smsgwglobals.wislogger.debug("WATCHDOG: " + "no SMS to process") return except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message) # we have sms, just process while smscount > 0: for sms in smsen: smsgwglobals.wislogger.debug("WATCHDOG: " + str(sms)) # create smstrans object for easy handling smstrans = smstransfer.Smstransfer(**sms) # check if we have routes # if we have no routes, set error code and # continue with the next sms routes = wisglobals.rdb.read_routing() if routes is None or len(routes) == 0: smstrans.smsdict["statustime"] = datetime.utcnow() smstrans.smsdict["status"] = 100 smsgwglobals.wislogger.debug(smstrans.smsdict) smstrans.updatedb() continue # check if modemid exists in routing route = wisglobals.rdb.read_routing( smstrans.smsdict["modemid"]) if route is None or len(route) == 0: smsgwglobals.wislogger.debug("WATCHDOG: " + " ALERT ROUTE LOST") # try to reprocess route smstrans.smsdict["status"] = 106 smstrans.updatedb() Helper.processsms(smstrans) elif route[0]["wisid"] != wisglobals.wisid: self.deligate(smstrans, route) else: # we have a route, this wis is the correct one # therefore give the sms to the PIS # this is a bad hack to ignore obsolete routes # this may lead to an error, fixme route[:] = [d for d in route if d['obsolete'] < 13] smsgwglobals.wislogger.debug("WATCHDOG: process with route %s ", str(route)) smsgwglobals.wislogger.debug("WATCHDOG: " + " Sending to PIS") self.send(sms, route) smsen = db.read_sms(status=0) smsen = smsen + db.read_sms(status=1) smscount = len(smsen)
def send(self, sms): smstrans = sms["sms"] route = sms["route"] # encode to json jdata = json.dumps(smstrans.smsdict, default=str) data = GlobalHelper.encodeAES(jdata) request = \ urllib.request.Request( route[0]["pisurl"] + "/sendsms") request.add_header("Content-Type", "application/json;charset=utf-8") try: smsgwglobals.wislogger.debug("WATCHDOG [route: " + str(self.routingid) + "] " + "Sending VIA " + smstrans.smsdict["modemid"] + route[0]["pisurl"] + "/sendsms") f = urllib.request.urlopen(request, data, timeout=wisglobals.pissendtimeout) smsgwglobals.wislogger.debug("WATCHDOG [route: " + str(self.routingid) + "] SMS send to PIS returncode:" + str(f.getcode())) # if all is OK set the sms status to SENT smstrans.smsdict["statustime"] = datetime.utcnow() if f.getcode() == 200: status_code = f.read() if int(status_code) == 1: if smstrans.smsdict["status"] == -1: smstrans.smsdict["status"] = 101 smsgwglobals.wislogger.debug("WATCHDOG [route: " + str(self.routingid) + "] SEND deligated:" + str(smstrans.smsdict)) else: smstrans.smsdict["status"] = 1 smsgwglobals.wislogger.debug("WATCHDOG [route: " + str(self.routingid) + "] SEND direct:" + str(smstrans.smsdict)) smsgwglobals.wislogger.debug("WATCHDOG [route: " + str(self.routingid) + "] SEND Update DB SUCCESS:" + str(smstrans.smsdict)) smstrans.updatedb() elif int(status_code) == 2000 or int(status_code) == 31 or int( status_code) == 27 or int(status_code) == 69: # PIS doesn't have modem endpoint - reprocess SMS and choose different route) - Error 2000 # Modem fail - reprocess SMS and choose different route) - Error 31 (can't read SMSC nummber, 99.99% - we just lost connection) # Modem fail - reprocess SMS and choose different route) - Error 27 ( no money or SIM card blocked) # Modem fail - reprocess SMS and choose different route) - Error 69 (can't read SMSC nummber, 99.99% - we just lost connection) # BUT use same smsid (after new route will be choosed it will decrease sms_count on route (IMSI) smsgwglobals.wislogger.debug("WATCHDOG [route: " + str(self.routingid) + "] PIS can't reach PID: " + str(smstrans.smsdict)) try: Helper.processsms(smstrans) except apperror.NoRoutesFoundError: pass else: # Add sms to global queue wisglobals.watchdogThread.queue.put( smstrans.smsdict["smsid"]) wisglobals.watchdogThreadNotify.set() else: if smstrans.smsdict["status"] == 0: smstrans.smsdict["status"] = int(status_code) smsgwglobals.wislogger.debug("WATCHDOG [route: " + str(self.routingid) + "] SEND direct ERROR:" + str(smstrans.smsdict)) else: smstrans.smsdict["status"] = 100 + int(status_code) smsgwglobals.wislogger.debug( "WATCHDOG [route: " + str(self.routingid) + "] SEND deligated ERROR:" + str(smstrans.smsdict)) smsgwglobals.wislogger.debug("WATCHDOG [route: " + str(self.routingid) + "] SEND Update DB ERROR:" + str(smstrans.smsdict)) smstrans.updatedb() except urllib.error.URLError as e: if smstrans.smsdict["status"] == -1: smstrans.smsdict["status"] = 300 else: smstrans.smsdict["status"] = 200 smsgwglobals.wislogger.debug("WATCHDOG [route: " + str(self.routingid) + "] SEND EXCEPTION " + str(smstrans.smsdict)) smstrans.updatedb() # set SMS to not send!!! smsgwglobals.wislogger.debug(e) smsgwglobals.wislogger.debug("WA3TCHDOG [route: " + str(self.routingid) + "] SEND Get peers NOTOK") # On 500 error - (probably PID/route died - try to reprocess sms) try: Helper.processsms(smstrans) except apperror.NoRoutesFoundError: pass else: # Add sms to global queue wisglobals.watchdogThread.queue.put(smstrans.smsdict["smsid"]) wisglobals.watchdogThreadNotify.set() except socket.timeout as e: smstrans.smsdict["status"] = 400 smstrans.updatedb() smsgwglobals.wislogger.debug(e) smsgwglobals.wislogger.debug("WATCHDOG [route: " + str(self.routingid) + "] SEND Socket connection timeout") # On 400 error - (probably PID/route died - try to reprocess sms) try: Helper.processsms(smstrans) except apperror.NoRoutesFoundError: pass else: # Add sms to global queue wisglobals.watchdogThread.queue.put(smstrans.smsdict["smsid"]) wisglobals.watchdogThreadNotify.set()
def process(self): smsgwglobals.wislogger.debug("WATCHDOG: processing sms") # check if we have SMS to work on smscount = 0 try: db = database.Database() # cleanup old sms db.delete_old_sms(wisglobals.cleanupseconds) smsen = db.read_sms(status=0) smsen = smsen + db.read_sms(status=1) smscount = len(smsen) if smscount == 0: smsgwglobals.wislogger.debug("WATCHDOG: " + "no SMS to process") return except error.DatabaseError as e: smsgwglobals.wislogger.debug(e.message) # we have sms, just process smsgwglobals.wislogger.debug("WATCHDOG: Count to process: %s", str(smscount)) while smscount > 0: for sms in smsen: smsgwglobals.wislogger.debug("WATCHDOG: Process SMS: " + str(sms)) # create smstrans object for easy handling smstrans = smstransfer.Smstransfer(**sms) # check if we have routes # if we have no routes, set error code and # continue with the next sms routes = wisglobals.rdb.read_routing() if routes is None or len(routes) == 0: smstrans.smsdict["statustime"] = datetime.utcnow() smstrans.smsdict["status"] = 100 smsgwglobals.wislogger.debug( "WATCHDOG: NO routes to process SMS: " + str(smstrans.smsdict)) smstrans.updatedb() continue # check if modemid exists in routing route = wisglobals.rdb.read_routing( smstrans.smsdict["modemid"]) if route is None or len(route) == 0: smsgwglobals.wislogger.debug("WATCHDOG: " + " ALERT ROUTE LOST") # try to reprocess route smstrans.smsdict["status"] = 106 smstrans.updatedb() Helper.processsms(smstrans) elif route[0]["wisid"] != wisglobals.wisid: self.deligate(smstrans, route) else: # we have a route, this wis is the correct one # therefore give the sms to the PIS # this is a bad hack to ignore obsolete routes # this may lead to an error, fixme route[:] = [d for d in route if d['obsolete'] < 13] smsgwglobals.wislogger.debug( "WATCHDOG: process with route %s ", str(route)) smsgwglobals.wislogger.debug("WATCHDOG: Sending to PIS %s", str(sms)) # only continue if route contains data if len(route) > 0: self.send(smstrans, route) smsen = db.read_sms(status=0) smsen = smsen + db.read_sms(status=1) smscount = len(smsen)