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
Jose M. Guisado 2023-06-13 10:51:42 +02:00
parent 926a73cf33
commit 0c03d82ca8
3 changed files with 35 additions and 22 deletions

View File

@ -47,6 +47,7 @@ class ogClient:
self.ip = self.CONFIG['opengnsys']['ip'] self.ip = self.CONFIG['opengnsys']['ip']
self.port = self.CONFIG['opengnsys']['port'] self.port = self.CONFIG['opengnsys']['port']
self.ogrest = ogRest(self.CONFIG) self.ogrest = ogRest(self.CONFIG)
self.seq = None
def get_socket(self): def get_socket(self):
return self.sock return self.sock
@ -144,6 +145,8 @@ class ogClient:
if 'Content-Length' in headers.keys(): if 'Content-Length' in headers.keys():
self.content_len = int(headers['Content-Length']) self.content_len = int(headers['Content-Length'])
if 'X-Sequence' in headers.keys():
self.seq = int(headers['X-Sequence'])
self.trailer = True self.trailer = True
# Add 4 because self.data.find("\r\n\r\n") does not count # Add 4 because self.data.find("\r\n\r\n") does not count

View File

@ -39,7 +39,7 @@ class jsonBody():
return json.dumps(self.jsontree) return json.dumps(self.jsontree)
class restResponse(): class restResponse():
def __init__(self, response, json_body=None): def __init__(self, response, json_body=None, seq=None):
self.msg = '' self.msg = ''
if response == ogResponses.BAD_REQUEST: if response == ogResponses.BAD_REQUEST:
self.msg = 'HTTP/1.0 400 Bad Request' self.msg = 'HTTP/1.0 400 Bad Request'
@ -65,6 +65,11 @@ class restResponse():
self.msg += '\r\n' self.msg += '\r\n'
if seq:
self.seq = seq
self.msg += 'X-Sequence: ' + str(seq)
self.msg += '\r\n'
if json_body: if json_body:
self.msg += 'Content-Length: ' + str(len(json_body.dump())) self.msg += 'Content-Length: ' + str(len(json_body.dump()))
self.msg += '\r\nContent-Type: application/json' self.msg += '\r\nContent-Type: application/json'
@ -80,7 +85,7 @@ class restResponse():
class ogThread(): class ogThread():
def shellrun(client, request, ogRest): def shellrun(client, request, ogRest):
if not request.getrun(): if not request.getrun():
response = restResponse(ogResponses.BAD_REQUEST) response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
client.send(response.get()) client.send(response.get())
ogRest.state = ThreadState.IDLE ogRest.state = ThreadState.IDLE
return return
@ -94,10 +99,10 @@ class ogThread():
if request.getEcho(): if request.getEcho():
json_body = jsonBody() json_body = jsonBody()
json_body.add_element('out', shellout) 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()) client.send(response.get())
else: else:
response = restResponse(ogResponses.OK) response = restResponse(ogResponses.OK, seq=client.seq)
client.send(response.get()) client.send(response.get())
ogRest.state = ThreadState.IDLE ogRest.state = ThreadState.IDLE
@ -116,7 +121,7 @@ class ogThread():
ogRest.send_internal_server_error(client, exc=e) ogRest.send_internal_server_error(client, exc=e)
return return
response = restResponse(ogResponses.OK) response = restResponse(ogResponses.OK, seq=client.seq)
client.send(response.get()) client.send(response.get())
client.disconnect() client.disconnect()
@ -131,7 +136,7 @@ class ogThread():
json_body.add_element('partition', request.getPartition()) json_body.add_element('partition', request.getPartition())
json_body.add_element('software', software) 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()) client.send(response.get())
ogRest.state = ThreadState.IDLE ogRest.state = ThreadState.IDLE
@ -145,7 +150,7 @@ class ogThread():
json_body = jsonBody() json_body = jsonBody()
json_body.add_element('hardware', result) 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()) client.send(response.get())
ogRest.state = ThreadState.IDLE ogRest.state = ThreadState.IDLE
@ -158,7 +163,7 @@ class ogThread():
json_body = jsonBody(out) json_body = jsonBody(out)
response = restResponse(ogResponses.OK, json_body) response = restResponse(ogResponses.OK, json_body, seq=client.seq)
client.send(response.get()) client.send(response.get())
ogRest.state = ThreadState.IDLE ogRest.state = ThreadState.IDLE
@ -174,7 +179,7 @@ class ogThread():
json_body.add_element('partition', request.getPartition()) json_body.add_element('partition', request.getPartition())
json_body.add_element('image_id', request.getId()) 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()) client.send(response.get())
ogRest.state = ThreadState.IDLE ogRest.state = ThreadState.IDLE
@ -202,7 +207,7 @@ class ogThread():
json_body.add_element('filesystem', image_info.filesystem) json_body.add_element('filesystem', image_info.filesystem)
json_body.add_element('datasize', datasize) 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()) client.send(response.get())
ogRest.state = ThreadState.IDLE ogRest.state = ThreadState.IDLE
@ -215,7 +220,7 @@ class ogThread():
json_body = jsonBody(out) json_body = jsonBody(out)
response = restResponse(ogResponses.OK, json_body) response = restResponse(ogResponses.OK, json_body, seq=client.seq)
client.send(response.get()) client.send(response.get())
ogRest.state = ThreadState.IDLE ogRest.state = ThreadState.IDLE
@ -260,7 +265,7 @@ class ogRest():
def send_internal_server_error(self, client, exc=None): def send_internal_server_error(self, client, exc=None):
if exc: if exc:
logging.exception('Unexpected error') logging.exception('Unexpected error')
response = restResponse(ogResponses.INTERNAL_ERR) response = restResponse(ogResponses.INTERNAL_ERR, seq=client.seq)
client.send(response.get()) client.send(response.get())
self.state = ThreadState.IDLE self.state = ThreadState.IDLE
@ -277,7 +282,7 @@ class ogRest():
if self.state == ThreadState.BUSY: if self.state == ThreadState.BUSY:
logging.warn('Request has been received ' logging.warn('Request has been received '
'while ogClient is busy') 'while ogClient is busy')
response = restResponse(ogResponses.SERVICE_UNAVAILABLE) response = restResponse(ogResponses.SERVICE_UNAVAILABLE, seq=client.seq)
client.send(response.get()) client.send(response.get())
return return
else: else:
@ -295,7 +300,7 @@ class ogRest():
else: else:
logging.warn('Unsupported request: %s', logging.warn('Unsupported request: %s',
{URI[:ogRest.LOG_LENGTH]}) {URI[:ogRest.LOG_LENGTH]})
response = restResponse(ogResponses.BAD_REQUEST) response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
client.send(response.get()) client.send(response.get())
self.state = ThreadState.IDLE self.state = ThreadState.IDLE
elif ("POST" in method): elif ("POST" in method):
@ -320,11 +325,11 @@ class ogRest():
else: else:
logging.warn('Unsupported request: %s', logging.warn('Unsupported request: %s',
URI[:ogRest.LOG_LENGTH]) URI[:ogRest.LOG_LENGTH])
response = restResponse(ogResponses.BAD_REQUEST) response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
client.send(response.get()) client.send(response.get())
self.state = ThreadState.IDLE self.state = ThreadState.IDLE
else: else:
response = restResponse(ogResponses.BAD_REQUEST) response = restResponse(ogResponses.BAD_REQUEST, seq=client.seq)
client.send(response.get()) client.send(response.get())
self.state = ThreadState.IDLE self.state = ThreadState.IDLE
@ -345,7 +350,7 @@ class ogRest():
self.state = ThreadState.IDLE self.state = ThreadState.IDLE
def process_reboot(self, client): def process_reboot(self, client):
response = restResponse(ogResponses.IN_PROGRESS) response = restResponse(ogResponses.IN_PROGRESS, seq=client.seq)
client.send(response.get()) client.send(response.get())
if self.mode != 'virtual': if self.mode != 'virtual':
@ -356,7 +361,7 @@ class ogRest():
threading.Thread(target=ogThread.reboot, args=(self,)).start() threading.Thread(target=ogThread.reboot, args=(self,)).start()
def process_poweroff(self, client): def process_poweroff(self, client):
response = restResponse(ogResponses.IN_PROGRESS) response = restResponse(ogResponses.IN_PROGRESS, seq=client.seq)
client.send(response.get()) client.send(response.get())
if self.mode != 'virtual': if self.mode != 'virtual':
@ -370,7 +375,7 @@ class ogRest():
try: try:
status = self.operations.probe(self) status = self.operations.probe(self)
except: except:
response = restResponse(ogResponses.INTERNAL_ERR) response = restResponse(ogResponses.INTERNAL_ERR, seq=client.seq)
client.send(response.get()) client.send(response.get())
return return
@ -379,9 +384,9 @@ class ogRest():
json_body.add_element(k, v) json_body.add_element(k, v)
if self.state != ThreadState.BUSY: if self.state != ThreadState.BUSY:
response = restResponse(ogResponses.OK, json_body) response = restResponse(ogResponses.OK, json_body, seq=client.seq)
else: else:
response = restResponse(ogResponses.IN_PROGRESS, json_body) response = restResponse(ogResponses.IN_PROGRESS, json_body, seq=client.seq)
client.send(response.get()) client.send(response.get())
@ -398,7 +403,7 @@ class ogRest():
threading.Thread(target=ogThread.hardware, args=(client, self,)).start() threading.Thread(target=ogThread.hardware, args=(client, self,)).start()
def process_schedule(self, client): def process_schedule(self, client):
response = restResponse(ogResponses.OK) response = restResponse(ogResponses.OK, seq=client.seq)
client.send(response.get()) client.send(response.get())
self.state = ThreadState.IDLE self.state = ThreadState.IDLE

View File

@ -35,6 +35,7 @@ class restRequest:
self.id = None self.id = None
self.echo = None self.echo = None
self.code = None self.code = None
self.seq = None
def parser(self,data): def parser(self,data):
self.request_line, self.headers_alone = data.split('\n', 1) self.request_line, self.headers_alone = data.split('\n', 1)
@ -49,6 +50,10 @@ class restRequest:
if 'Content-Length' in self.headers.keys(): if 'Content-Length' in self.headers.keys():
self.content_len = int(self.headers['Content-Length']) 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 == ''): if (not self.request_line == None or not self.request_line == ''):
self.method = self.request_line.split('/', 1)[0] self.method = self.request_line.split('/', 1)[0]
self.URI = self.request_line.split('/', 1)[1] self.URI = self.request_line.split('/', 1)[1]