Defensive programming inc

This commit is contained in:
Patrick Jentsch 2020-03-16 13:15:13 +01:00
parent 52e5092d13
commit b8437735b4
2 changed files with 27 additions and 21 deletions

View File

@ -413,8 +413,8 @@ class Client:
def __init__(self, host='127.0.0.1', port=4877):
self.host = host
self.port = port
self.connection = socket.socket()
self.connection.connect((self.host, self.port))
self.socket = socket.socket()
self.socket.connect((self.host, self.port))
def ctrl_connect(self, username, password):
# INPUT: (STRING username, STRING password)
@ -862,24 +862,24 @@ class Client:
def __recv_DATA_BYTE(self):
while True:
if (len(self.connection.recv(1, socket.MSG_PEEK)) == 1):
byte_data = self.connection.recv(1)
if (len(self.socket.recv(1, socket.MSG_PEEK)) == 1):
byte_data = self.socket.recv(1)
break
sleep(0.1)
return struct.unpack('!B', byte_data)[0]
def __recv_DATA_BOOL(self):
while True:
if (len(self.connection.recv(1, socket.MSG_PEEK)) == 1):
byte_data = self.connection.recv(1)
if (len(self.socket.recv(1, socket.MSG_PEEK)) == 1):
byte_data = self.socket.recv(1)
break
sleep(0.1)
return struct.unpack('!?', byte_data)[0]
def __recv_DATA_INT(self):
while True:
if (len(self.connection.recv(4, socket.MSG_PEEK)) == 4):
byte_data = self.connection.recv(4)
if (len(self.socket.recv(4, socket.MSG_PEEK)) == 4):
byte_data = self.socket.recv(4)
break
sleep(0.1)
return struct.unpack('!i', byte_data)[0]
@ -887,8 +887,8 @@ class Client:
def __recv_DATA_STRING(self):
n = self.__recv_WORD()
while True:
if (len(self.connection.recv(n, socket.MSG_PEEK)) == n):
byte_data = self.connection.recv(n)
if (len(self.socket.recv(n, socket.MSG_PEEK)) == n):
byte_data = self.socket.recv(n)
break
sleep(0.1)
return struct.unpack('!{}s'.format(n), byte_data)[0].decode()
@ -947,29 +947,29 @@ class Client:
def __recv_WORD(self):
while True:
if (len(self.connection.recv(2, socket.MSG_PEEK)) == 2):
byte_data = self.connection.recv(2)
if (len(self.socket.recv(2, socket.MSG_PEEK)) == 2):
byte_data = self.socket.recv(2)
break
sleep(0.1)
return struct.unpack('!H', byte_data)[0]
def __send_BYTE(self, byte_data):
data = struct.pack('!B', byte_data)
self.connection.sendall(data)
self.socket.sendall(data)
def __send_BOOL(self, bool_data):
data = struct.pack('!?', bool_data)
self.connection.sendall(data)
self.socket.sendall(data)
def __send_INT(self, int_data):
data = struct.pack('!i', int_data)
self.connection.sendall(data)
self.socket.sendall(data)
def __send_STRING(self, string_data):
encoded_string_data = string_data.encode('utf-8')
n = len(encoded_string_data)
data = struct.pack('!H{}s'.format(n), n, encoded_string_data)
self.connection.sendall(data)
self.socket.sendall(data)
def __send_INT_LIST(self, int_list_data):
n = len(int_list_data)
@ -985,4 +985,4 @@ class Client:
def __send_WORD(self, word_data):
data = struct.pack('!H', word_data)
self.connection.sendall(data)
self.socket.sendall(data)

View File

@ -75,12 +75,10 @@ def corpus_analysis_session_handler(app, corpus_id, user_id, session_id):
corpus = Corpus.query.get(corpus_id)
user = User.query.get(user_id)
if corpus is None:
logger.warning("404")
response = {'code': 404, 'msg': 'Not Found'}
socketio.emit('corpus_analysis_init', response, room=session_id)
return
elif not (corpus.creator == user or user.is_administrator()):
logger.warning("403")
response = {'code': 403, 'msg': 'Forbidden'}
socketio.emit('corpus_analysis_init', response, room=session_id)
return
@ -88,7 +86,12 @@ def corpus_analysis_session_handler(app, corpus_id, user_id, session_id):
db.session.refresh(corpus)
socketio.sleep(3)
client = CQiWrapper(host='corpus_{}_analysis'.format(corpus_id))
client.connect()
try:
client.connect()
except Exception:
response = {'code': 500, 'msg': 'Internal Server Error'}
socketio.emit('corpus_analysis_init', response, room=session_id)
return
corpus_analysis_clients[session_id] = client
if corpus_id not in corpus_analysis_sessions:
corpus_analysis_sessions[corpus_id] = [session_id]
@ -100,7 +103,10 @@ def corpus_analysis_session_handler(app, corpus_id, user_id, session_id):
while session_id in connected_sessions:
socketio.sleep(3)
''' Teardown analysis session '''
client.disconnect()
try:
client.disconnect()
except Exception:
pass
corpus_analysis_clients.pop(session_id, None)
corpus_analysis_sessions[corpus_id].remove(session_id)
if not corpus_analysis_sessions[corpus_id]: