mirror of https://git.48k.eu/ogclient
ogclient: add support for X-Sequence header
Enable parsing of "X-Sequence" HTTP headers from incoming requests. Add "seq" field in restRequest class. Enable adding "X-Sequence" to outgoing responses. Add "seq" field inside restResponse class. Store current client sequence number inside ogClient class. Ideally, the restRequest object should be used to retrieve the sequence number but not all processing functions inside ogRest.py receive the request as parameter (eg: process_refresh). In the other hand, all processing functions receive the ogClient object.more_events v1.3.0
parent
926a73cf33
commit
0c03d82ca8
|
@ -47,6 +47,7 @@ class ogClient:
|
|||
self.ip = self.CONFIG['opengnsys']['ip']
|
||||
self.port = self.CONFIG['opengnsys']['port']
|
||||
self.ogrest = ogRest(self.CONFIG)
|
||||
self.seq = None
|
||||
|
||||
def get_socket(self):
|
||||
return self.sock
|
||||
|
@ -144,6 +145,8 @@ class ogClient:
|
|||
|
||||
if 'Content-Length' in headers.keys():
|
||||
self.content_len = int(headers['Content-Length'])
|
||||
if 'X-Sequence' in headers.keys():
|
||||
self.seq = int(headers['X-Sequence'])
|
||||
|
||||
self.trailer = True
|
||||
# Add 4 because self.data.find("\r\n\r\n") does not count
|
||||
|
|
|
@ -39,7 +39,7 @@ class jsonBody():
|
|||
return json.dumps(self.jsontree)
|
||||
|
||||
class restResponse():
|
||||
def __init__(self, response, json_body=None):
|
||||
def __init__(self, response, json_body=None, seq=None):
|
||||
self.msg = ''
|
||||
if response == ogResponses.BAD_REQUEST:
|
||||
self.msg = 'HTTP/1.0 400 Bad Request'
|
||||
|
@ -65,6 +65,11 @@ class restResponse():
|
|||
|
||||
self.msg += '\r\n'
|
||||
|
||||
if seq:
|
||||
self.seq = seq
|
||||
self.msg += 'X-Sequence: ' + str(seq)
|
||||
self.msg += '\r\n'
|
||||
|
||||
if json_body:
|
||||
self.msg += 'Content-Length: ' + str(len(json_body.dump()))
|
||||
self.msg += '\r\nContent-Type: application/json'
|
||||
|
@ -80,7 +85,7 @@ class restResponse():
|
|||
class ogThread():
|
||||
def shellrun(client, request, ogRest):
|
||||
if not request.getrun():
|
||||
response = restResponse(ogResponses.BAD_REQUEST)
|
||||
response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
|
||||
client.send(response.get())
|
||||
ogRest.state = ThreadState.IDLE
|
||||
return
|
||||
|
@ -94,10 +99,10 @@ class ogThread():
|
|||
if request.getEcho():
|
||||
json_body = jsonBody()
|
||||
json_body.add_element('out', shellout)
|
||||
response = restResponse(ogResponses.OK, json_body)
|
||||
response = restResponse(ogResponses.OK, json_body, seq=client.seq)
|
||||
client.send(response.get())
|
||||
else:
|
||||
response = restResponse(ogResponses.OK)
|
||||
response = restResponse(ogResponses.OK, seq=client.seq)
|
||||
client.send(response.get())
|
||||
|
||||
ogRest.state = ThreadState.IDLE
|
||||
|
@ -116,7 +121,7 @@ class ogThread():
|
|||
ogRest.send_internal_server_error(client, exc=e)
|
||||
return
|
||||
|
||||
response = restResponse(ogResponses.OK)
|
||||
response = restResponse(ogResponses.OK, seq=client.seq)
|
||||
client.send(response.get())
|
||||
client.disconnect()
|
||||
|
||||
|
@ -131,7 +136,7 @@ class ogThread():
|
|||
json_body.add_element('partition', request.getPartition())
|
||||
json_body.add_element('software', software)
|
||||
|
||||
response = restResponse(ogResponses.OK, json_body)
|
||||
response = restResponse(ogResponses.OK, json_body, seq=client.seq)
|
||||
client.send(response.get())
|
||||
ogRest.state = ThreadState.IDLE
|
||||
|
||||
|
@ -145,7 +150,7 @@ class ogThread():
|
|||
json_body = jsonBody()
|
||||
json_body.add_element('hardware', result)
|
||||
|
||||
response = restResponse(ogResponses.OK, json_body)
|
||||
response = restResponse(ogResponses.OK, json_body, seq=client.seq)
|
||||
client.send(response.get())
|
||||
ogRest.state = ThreadState.IDLE
|
||||
|
||||
|
@ -158,7 +163,7 @@ class ogThread():
|
|||
|
||||
json_body = jsonBody(out)
|
||||
|
||||
response = restResponse(ogResponses.OK, json_body)
|
||||
response = restResponse(ogResponses.OK, json_body, seq=client.seq)
|
||||
client.send(response.get())
|
||||
ogRest.state = ThreadState.IDLE
|
||||
|
||||
|
@ -174,7 +179,7 @@ class ogThread():
|
|||
json_body.add_element('partition', request.getPartition())
|
||||
json_body.add_element('image_id', request.getId())
|
||||
|
||||
response = restResponse(ogResponses.OK, json_body)
|
||||
response = restResponse(ogResponses.OK, json_body, seq=client.seq)
|
||||
client.send(response.get())
|
||||
ogRest.state = ThreadState.IDLE
|
||||
|
||||
|
@ -202,7 +207,7 @@ class ogThread():
|
|||
json_body.add_element('filesystem', image_info.filesystem)
|
||||
json_body.add_element('datasize', datasize)
|
||||
|
||||
response = restResponse(ogResponses.OK, json_body)
|
||||
response = restResponse(ogResponses.OK, json_body, seq=client.seq)
|
||||
client.send(response.get())
|
||||
ogRest.state = ThreadState.IDLE
|
||||
|
||||
|
@ -215,7 +220,7 @@ class ogThread():
|
|||
|
||||
json_body = jsonBody(out)
|
||||
|
||||
response = restResponse(ogResponses.OK, json_body)
|
||||
response = restResponse(ogResponses.OK, json_body, seq=client.seq)
|
||||
client.send(response.get())
|
||||
ogRest.state = ThreadState.IDLE
|
||||
|
||||
|
@ -260,7 +265,7 @@ class ogRest():
|
|||
def send_internal_server_error(self, client, exc=None):
|
||||
if exc:
|
||||
logging.exception('Unexpected error')
|
||||
response = restResponse(ogResponses.INTERNAL_ERR)
|
||||
response = restResponse(ogResponses.INTERNAL_ERR, seq=client.seq)
|
||||
client.send(response.get())
|
||||
self.state = ThreadState.IDLE
|
||||
|
||||
|
@ -277,7 +282,7 @@ class ogRest():
|
|||
if self.state == ThreadState.BUSY:
|
||||
logging.warn('Request has been received '
|
||||
'while ogClient is busy')
|
||||
response = restResponse(ogResponses.SERVICE_UNAVAILABLE)
|
||||
response = restResponse(ogResponses.SERVICE_UNAVAILABLE, seq=client.seq)
|
||||
client.send(response.get())
|
||||
return
|
||||
else:
|
||||
|
@ -295,7 +300,7 @@ class ogRest():
|
|||
else:
|
||||
logging.warn('Unsupported request: %s',
|
||||
{URI[:ogRest.LOG_LENGTH]})
|
||||
response = restResponse(ogResponses.BAD_REQUEST)
|
||||
response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
|
||||
client.send(response.get())
|
||||
self.state = ThreadState.IDLE
|
||||
elif ("POST" in method):
|
||||
|
@ -320,11 +325,11 @@ class ogRest():
|
|||
else:
|
||||
logging.warn('Unsupported request: %s',
|
||||
URI[:ogRest.LOG_LENGTH])
|
||||
response = restResponse(ogResponses.BAD_REQUEST)
|
||||
response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
|
||||
client.send(response.get())
|
||||
self.state = ThreadState.IDLE
|
||||
else:
|
||||
response = restResponse(ogResponses.BAD_REQUEST)
|
||||
response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
|
||||
client.send(response.get())
|
||||
self.state = ThreadState.IDLE
|
||||
|
||||
|
@ -345,7 +350,7 @@ class ogRest():
|
|||
self.state = ThreadState.IDLE
|
||||
|
||||
def process_reboot(self, client):
|
||||
response = restResponse(ogResponses.IN_PROGRESS)
|
||||
response = restResponse(ogResponses.IN_PROGRESS, seq=client.seq)
|
||||
client.send(response.get())
|
||||
|
||||
if self.mode != 'virtual':
|
||||
|
@ -356,7 +361,7 @@ class ogRest():
|
|||
threading.Thread(target=ogThread.reboot, args=(self,)).start()
|
||||
|
||||
def process_poweroff(self, client):
|
||||
response = restResponse(ogResponses.IN_PROGRESS)
|
||||
response = restResponse(ogResponses.IN_PROGRESS, seq=client.seq)
|
||||
client.send(response.get())
|
||||
|
||||
if self.mode != 'virtual':
|
||||
|
@ -370,7 +375,7 @@ class ogRest():
|
|||
try:
|
||||
status = self.operations.probe(self)
|
||||
except:
|
||||
response = restResponse(ogResponses.INTERNAL_ERR)
|
||||
response = restResponse(ogResponses.INTERNAL_ERR, seq=client.seq)
|
||||
client.send(response.get())
|
||||
return
|
||||
|
||||
|
@ -379,9 +384,9 @@ class ogRest():
|
|||
json_body.add_element(k, v)
|
||||
|
||||
if self.state != ThreadState.BUSY:
|
||||
response = restResponse(ogResponses.OK, json_body)
|
||||
response = restResponse(ogResponses.OK, json_body, seq=client.seq)
|
||||
else:
|
||||
response = restResponse(ogResponses.IN_PROGRESS, json_body)
|
||||
response = restResponse(ogResponses.IN_PROGRESS, json_body, seq=client.seq)
|
||||
|
||||
client.send(response.get())
|
||||
|
||||
|
@ -398,7 +403,7 @@ class ogRest():
|
|||
threading.Thread(target=ogThread.hardware, args=(client, self,)).start()
|
||||
|
||||
def process_schedule(self, client):
|
||||
response = restResponse(ogResponses.OK)
|
||||
response = restResponse(ogResponses.OK, seq=client.seq)
|
||||
client.send(response.get())
|
||||
self.state = ThreadState.IDLE
|
||||
|
||||
|
|
|
@ -35,6 +35,7 @@ class restRequest:
|
|||
self.id = None
|
||||
self.echo = None
|
||||
self.code = None
|
||||
self.seq = None
|
||||
|
||||
def parser(self,data):
|
||||
self.request_line, self.headers_alone = data.split('\n', 1)
|
||||
|
@ -49,6 +50,10 @@ class restRequest:
|
|||
if 'Content-Length' in self.headers.keys():
|
||||
self.content_len = int(self.headers['Content-Length'])
|
||||
|
||||
if 'X-Sequence' in self.headers.keys():
|
||||
self.seq = int(self.headers['X-Sequence'])
|
||||
logging.debug(f'Request with sequence number {self.seq}')
|
||||
|
||||
if (not self.request_line == None or not self.request_line == ''):
|
||||
self.method = self.request_line.split('/', 1)[0]
|
||||
self.URI = self.request_line.split('/', 1)[1]
|
||||
|
|
Loading…
Reference in New Issue