From 59a28237f15ae8760043a08fd4c6fd007e0a6ac7 Mon Sep 17 00:00:00 2001 From: Alvaro Neira Ayuso Date: Fri, 27 Dec 2019 17:59:00 +0100 Subject: Create ogThread class to move all thread functions Right now, all the thread functions are declared inside the processor function. Those functions were created for execute specific commands in the machine (poweroff, reboot, etc). Creating this new class we are cleaning up the code. --- src/ogRest.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/ogRest.py b/src/ogRest.py index 60a33b0..f470f0b 100644 --- a/src/ogRest.py +++ b/src/ogRest.py @@ -8,6 +8,20 @@ import queue if platform.system() == 'Linux': from src.linux import ogOperations +class ogThread(): + # Executing cmd thread + def execcmd(msgqueue, cmd): + msgqueue.put(ogOperations.execCMD(cmd)) + + # Powering off thread + def pwoff(): + time.sleep(2) + ogOperations.poweroff() + + # Rebooting thread + def rebt(): + ogOperations.reboot() + class ogResponses(Enum): BAD_REQUEST=0 IN_PROGRESS=1 @@ -64,37 +78,24 @@ class ogRest(): return 0 def process_reboot(self, client): - # Rebooting thread - def rebt(): - ogOperations.reboot() - client.send(self.getResponse(ogResponses.IN_PROGRESS)) client.disconnect() - threading.Thread(target=rebt).start() + threading.Thread(target=ogThread.rebt).start() def process_poweroff(self, client): - # Powering off thread - def pwoff(): - time.sleep(2) - ogOperations.poweroff() - client.send(self.getResponse(ogResponses.IN_PROGRESS)) client.disconnect() - threading.Thread(target=pwoff).start() + threading.Thread(target=ogThread.pwoff).start() def process_probe(self, client): client.send(self.getResponse(ogResponses.OK)) def process_shellrun(self, client, cmd): - # Executing cmd thread - def execcmd(msgqueue): - msgqueue.put(ogOperations.execCMD(cmd)) - if cmd == None: client.send(self.getResponse(ogResponses.BAD_REQUEST)) return - threading.Thread(target=execcmd, args=(self.msgqueue,)).start() + threading.Thread(target=ogThread.execcmd, args=(self.msgqueue, cmd,)).start() client.send(self.getResponse(ogResponses.IN_PROGRESS)) def process_shellout(self, client): -- cgit v1.2.3-18-g5258