source: ogClient-Git/src/ogRest.py @ 2e342b5

Last change on this file since 2e342b5 was 2e342b5, checked in by Alvaro Neira Ayuso <alvaroneay@…>, 5 years ago

Modify methods to use less arguments

Now, all the arguments are received from httpparser. Those arguments convert
the function in long lines of codes. Passing directly the httpparser, all the
function will have less arguments and will be more clear the code.

  • Property mode set to 100644
File size: 5.2 KB
Line 
1import threading
2import platform
3import time
4from enum import Enum
5import json
6import queue
7
8from src.HTTPParser import *
9
10if platform.system() == 'Linux':
11        from src.linux import ogOperations
12
13class jsonResponse():
14        def __init__(self):
15                self.jsontree = {}
16
17        def addElement(self, key, value):
18                self.jsontree[key] = value
19
20        def dumpMsg(self):
21                return json.dumps(self.jsontree)
22
23class ogThread():
24        # Executing cmd thread
25        def execcmd(msgqueue, httpparser):
26                msgqueue.queue.clear()
27                msgqueue.put(ogOperations.execCMD(httpparser))
28
29        # Powering off thread
30        def poweroff():
31                time.sleep(2)
32                ogOperations.poweroff()
33
34        # Rebooting thread
35        def reboot():
36                ogOperations.reboot()
37
38        # Process session
39        def procsession(msgqueue, httpparser):
40                msgqueue.queue.clear()
41                msgqueue.put(ogOperations.procsession(httpparser))
42
43        # Process software
44        def procsoftware(msgqueue, httpparser, path):
45                msgqueue.queue.clear()
46                msgqueue.put(ogOperations.procsoftware(httpparser, path))
47
48        # Process hardware
49        def prochardware(msgqueue, path):
50                msgqueue.queue.clear()
51                msgqueue.put(ogOperations.prochardware(path))
52
53        # Process setup
54        def procsetup(msgqueue, httpparser):
55                ogOperations.procsetup(httpparser)
56
57        # Process image restore
58        def procirestore(msgqueue, httpparser):
59                msgqueue.queue.clear()
60                msgqueue.put(ogOperations.procirestore(httpparser))
61
62class ogResponses(Enum):
63        BAD_REQUEST=0
64        IN_PROGRESS=1
65        OK=2
66
67class ogRest():
68        def __init__(self):
69                self.msgqueue = queue.Queue(1000)
70
71        def getResponse(self, response, jsonResp=None):
72                msg = ''
73                if response == ogResponses.BAD_REQUEST:
74                        msg = 'HTTP/1.0 400 Bad request'
75                elif response == ogResponses.IN_PROGRESS:
76                        msg = 'HTTP/1.0 202 Accepted'
77                elif response == ogResponses.OK:
78                        msg = 'HTTP/1.0 200 OK'
79                else:
80                        return msg
81
82                if not jsonResp == None:
83                        msg = msg + '\nContent-Type:application/json'
84                        msg = msg + '\nContent-Length:' + str(len(jsonResp.dumpMsg()))
85                        msg = msg + '\n' + jsonResp.dumpMsg()
86
87                msg = msg + '\r\n\r\n'
88                return msg
89
90        def processOperation(self, httpparser, client):
91                op = httpparser.getRequestOP()
92                URI = httpparser.getURI()
93                if ("GET" in op):
94                        if ("probe" in URI):
95                                self.process_probe(client)
96                        elif ("shell/output" in URI):
97                                self.process_shellout(client)
98                        elif ("hardware" in URI):
99                                self.process_hardware(client)
100                        elif ("run/schedule" in URI):
101                                self.process_schedule(client)
102                        else:
103                                client.send(self.getResponse(ogResponses.BAD_REQUEST))
104                elif ("POST" in op):
105                        if ("poweroff" in URI):
106                                self.process_poweroff(client)
107                        elif ("reboot" in URI):
108                                self.process_reboot(client)
109                        elif ("shell/run" in URI):
110                                self.process_shellrun(client, httpparser)
111                        elif ("session" in URI):
112                                self.process_session(client, httpparser)
113                        elif ("software" in URI):
114                                self.process_software(client, httpparser)
115                        elif ("setup" in URI):
116                                self.process_setup(client, httpparser)
117                        elif ("image/restore" in URI):
118                                self.process_irestore(client, httpparser)
119                        else:
120                                client.send(self.getResponse(ogResponses.BAD_REQUEST))
121                else:
122                        client.send(self.getResponse(ogResponses.BAD_REQUEST))
123
124                return 0
125
126        def process_reboot(self, client):
127                client.send(self.getResponse(ogResponses.IN_PROGRESS))
128                client.disconnect()
129                threading.Thread(target=ogThread.reboot).start()
130
131        def process_poweroff(self, client):
132                client.send(self.getResponse(ogResponses.IN_PROGRESS))
133                client.disconnect()
134                threading.Thread(target=ogThread.poweroff).start()
135
136        def process_probe(self, client):
137                client.send(self.getResponse(ogResponses.OK))
138
139        def process_shellrun(self, client, httpparser):
140                if httpparser.getCMD() == None:
141                        client.send(self.getResponse(ogResponses.BAD_REQUEST))
142                        return
143
144                try:
145                        ogThread.execcmd(self.msgqueue, httpparser)
146                except ValueError as err:
147                        print(err.args[0])
148                        client.send(self.getResponse(ogResponses.BAD_REQUEST))
149                        return
150
151                client.send(self.getResponse(ogResponses.OK))
152
153        def process_shellout(self, client):
154                jsonResp = jsonResponse()
155                if self.msgqueue.empty():
156                        jsonResp.addElement('out', '')
157                        client.send(self.getResponse(ogResponses.OK, jsonResp))
158                else:
159                        jsonResp.addElement('out', self.msgqueue.get())
160                        client.send(self.getResponse(ogResponses.OK, jsonResp))
161
162        def process_session(self, client, httpparser):
163                threading.Thread(target=ogThread.procsession, args=(self.msgqueue, httpparser,)).start()
164                client.send(self.getResponse(ogResponses.OK))
165
166        def process_software(self, client, httpparser):
167                path = '/tmp/CSft-' + client.ip + '-' + partition
168                threading.Thread(target=ogThread.procsoftware, args=(self.msgqueue, httpparser, path,)).start()
169                client.send(self.getResponse(ogResponses.OK))
170
171        def process_hardware(self, client):
172                path = '/tmp/Chrd-' + client.ip
173                threading.Thread(target=ogThread.prochardware, args=(self.msgqueue, path,)).start()
174                client.send(self.getResponse(ogResponses.OK))
175
176        def process_schedule(self, client):
177                client.send(self.getResponse(ogResponses.OK))
178
179        def process_setup(self, client, httpparser):
180                threading.Thread(target=ogThread.procsetup, args=(self.msgqueue, httpparser,)).start()
181                client.send(self.getResponse(ogResponses.OK))
182
183        def process_irestore(self, client, httpparser):
184                threading.Thread(target=ogThread.procirestore, args=(self.msgqueue, httpparser,)).start()
185                client.send(self.getResponse(ogResponses.OK))
Note: See TracBrowser for help on using the repository browser.