From dfc97ffedb9a356299b593f76ce057f5953a77ab Mon Sep 17 00:00:00 2001 From: Alvaro Neira Ayuso Date: Mon, 16 Dec 2019 11:17:09 +0100 Subject: Merge ogRest and ogProcess to have only one class --- src/ogClient.py | 6 +++--- src/ogProcess.py | 42 ------------------------------------------ src/ogRest.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 51 insertions(+), 52 deletions(-) delete mode 100644 src/ogProcess.py (limited to 'src') diff --git a/src/ogClient.py b/src/ogClient.py index 48ae5e7..fb87bac 100644 --- a/src/ogClient.py +++ b/src/ogClient.py @@ -6,7 +6,7 @@ import email from io import StringIO from src.HTTPParser import * -from src.ogProcess import * +from src.ogRest import * from enum import Enum class State(Enum): @@ -74,7 +74,7 @@ class ogClient: self.data = self.data + data httpparser = HTTPParser() - ogprocess = ogProcess() + ogrest = ogRest() if not self.trailer: if self.data.find("\r\n") > 0: @@ -89,7 +89,7 @@ class ogClient: if self.trailer and len(self.data) >= self.content_len: httpparser.parser(self.data) - ogprocess.processOperation(httpparser.getRequestOP(), httpparser.getURI(), self) + ogrest.processOperation(httpparser.getRequestOP(), httpparser.getURI(), self) # Cleanup state information from request self.data = "" diff --git a/src/ogProcess.py b/src/ogProcess.py deleted file mode 100644 index 27954a7..0000000 --- a/src/ogProcess.py +++ /dev/null @@ -1,42 +0,0 @@ -import threading -import platform -import time -from src import ogRest - -if platform.system() == 'Linux': - from src.linux import ogOperations - -class ogProcess(): - def processOperation(self, op, URI, client): - if ("poweroff" in URI): - self.process_poweroff(client) - elif ("reboot" in URI): - self.process_reboot(client) - elif ("probe" in URI): - self.process_probe(client) - else: - client.send(ogRest.getResponse(ogRest.ogResponses.BAD_REQUEST)) - - return 0 - - def process_reboot(self, client): - # Rebooting thread - def rebt(): - ogOperations.reboot() - - client.send(ogRest.getResponse(ogRest.ogResponses.IN_PROGRESS)) - client.disconnect() - threading.Thread(target=rebt).start() - - def process_poweroff(self, client): - # Powering off thread - def pwoff(): - time.sleep(2) - ogOperations.poweroff() - - client.send(ogRest.getResponse(ogRest.ogResponses.IN_PROGRESS)) - client.disconnect() - threading.Thread(target=pwoff).start() - - def process_probe(self, client): - client.send(ogRest.getResponse(ogRest.ogResponses.OK)) diff --git a/src/ogRest.py b/src/ogRest.py index dfc382c..85e961a 100644 --- a/src/ogRest.py +++ b/src/ogRest.py @@ -1,14 +1,55 @@ +import threading +import platform +import time from enum import Enum +if platform.system() == 'Linux': + from src.linux import ogOperations + class ogResponses(Enum): BAD_REQUEST=0 IN_PROGRESS=1 OK=2 -def getResponse(response): - if response == ogResponses.BAD_REQUEST: - return 'HTTP/1.0 400 Bad request\r\n\r\n' - if response == ogResponses.IN_PROGRESS: - return 'HTTP/1.0 202 Accepted\r\n\r\n' - if response == ogResponses.OK: - return 'HTTP/1.0 200 OK\r\n\r\n' +class ogRest(): + def getResponse(self, response): + if response == ogResponses.BAD_REQUEST: + return 'HTTP/1.0 400 Bad request\r\n\r\n' + if response == ogResponses.IN_PROGRESS: + return 'HTTP/1.0 202 Accepted\r\n\r\n' + if response == ogResponses.OK: + return 'HTTP/1.0 200 OK\r\n\r\n' + + def processOperation(self, op, URI, client): + if ("poweroff" in URI): + self.process_poweroff(client) + elif ("reboot" in URI): + self.process_reboot(client) + elif ("probe" in URI): + self.process_probe(client) + else: + client.send(self.getResponse(ogResponses.BAD_REQUEST)) + + 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() + + 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() + + def process_probe(self, client): + client.send(self.getResponse(ogResponses.OK)) -- cgit v1.2.3-18-g5258