def upload_kiosk_messages(self): try: CMsgs.transmit_clientmessages() except: self.logger.exception("Unable to transmit client messages to server.") return False return True
def test_add(self): message = "Hello World Test Case 1 Dorem Lorem Dolor Sit Amet" sessionref = "reference9" origin = "cloud9" origintime = datetime.now() self._del_cmsgs_all() # Add to database CMsgs.add_clientmessage(self.ctx, message, sessionref, origin, origintime) configpath = self.ctx['config'] con = sqlite3.connect(join(configpath, 'sync.db'), detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) c = con.cursor() try: c.execute("SELECT message, sessionref, origin, origintime, id FROM clientmsg WHERE synctime is NULL LIMIT 15") rows = c.fetchall() except sqlite3.Error: logger.exception("Error fetching rows from sync.db.") rows = None finally: c.close() con.close() self.assertEqual(message, rows[0][0]) self.assertEqual(sessionref, rows[0][1]) self.assertEqual(origin, rows[0][2])
def test_upload(self): message = "Hello World Test Case 1 Dorem Lorem Dolor Sit Amet" sessionref = "reference9" origin = "cloud9" self._del_cmsgs_all() for i in range(1,5): CMsgs.add_clientmessage(self.ctx, message+str(i), str(i), origin+str(i), datetime.now()) self.assertTrue(CMsgs.transmit_clientmessages(self.ctx))
def service_server_messages(self, ignore_sa=False): msgs = self._download_server_messages() for msg in msgs: self.logger.debug("Message %s." % str(msg)) if 'msg_type' not in msg.keys(): self.logger.warn("No message type specified.") continue if 'msg' not in msg.keys(): self.logger.warn("No message specified.") continue if msg['msg_type'] == 'STAYALIVE': if not ignore_sa: self.stay_alive_routine(msg['msg']) elif msg['msg_type'] == 'CMD': self._exec_process(msg['msg']) elif msg['msg_type'] == 'GIVELOGS': res = self.upload_logs() CMsgs.add_clientmessage("Logs delivered.", res, "Command parser", datetime.utcnow()) else: self.logger.warn("Unrecogised command.") return
def stay_alive_routine(self,message_text): self.logger.info("Stay Alive for 30 minutes.") now = datetime.utcnow() stop = now + timedelta(seconds=1800) CMsgs.add_clientmessage(self.context, "Staying Alive.", '', "Command parser", datetime.utcnow()) while now < stop: now = datetime.utcnow() time.sleep(20) try: self.upload_kiosk_messages() except: self.logger.exception("An error occured when uploading messages.") # ignore another call to stay alive try: self.service_server_messages(ignore_sa=True) except: self.logger.exception("An error occured when attempting to fetch new orders.") CMsgs.add_clientmessage(self.context, "Going to die.", '', "Command parser", datetime.utcnow()) try: self.upload_kiosk_messages() except: self.logger.exception("An error occured when uploading messages.")
def _exec_process(self,message): self.logger.info("Executing message from server.") self.logger.debug("Command is %s." % message) try: proc = subprocess.Popen(message, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except: self.logger.exception("Subprocess could not be opened.") return False now = datetime.utcnow() start = datetime.utcnow() + timedelta(seconds=240) while now < start: now = datetime.now() if proc.poll() is not None: timeout = 0 break; timeout = 1 if timeout: self.logger.error("Process timed out.") return False ret_tuple = proc.communicate() self.logger.debug("Command returns: %s." % str(ret_tuple)) CMsgs.add_clientmessage(self.context, "Command Executed:\n%s" % str(ret_tuple), '', "Command Executer", datetime.utcnow()) return
def insert_kiosk_messages(): CMsgs.add_clientmessage('ABCDEF\nLoremIpsum', '1234', 'Test', datetime.utcnow()) CMsgs.add_clientmessage('Gazzzz\nLoremIpsum', '2', 'Test', datetime.utcnow()) CMsgs.add_clientmessage('ABCDEF\Booo', '3', 'cxcx', datetime.utcnow()) CMsgs.add_clientmessage('Lolololol\nLoremIpsum', '5', '3', datetime.utcnow()) return