Commit e5cf3138 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

Merge branch '54-clean-server' of gitlab.aip.de:yfournier/myplotlib into 54-clean-server

parents a852dcb1 f47460ea
......@@ -12,6 +12,9 @@ from . import SERVER_FIGURES
G_RAWDATA = {}
G_FIGURES = {}
def emptyfct():
return(False)
# CLASS SERVER ---------------------------------------------------------
class MplServer():
......@@ -60,7 +63,7 @@ class MplServer():
# Bind socket and host:port
self.sock.bind((self.host, self.port))
self.sock.listen(self.nbrAuthAccess)
self.initialized = True
# RUNNING ----------------------------------------------------------
......@@ -129,7 +132,7 @@ class MplServer():
# catch possible error
else:
if (DEBUG):
print(WARN+"SERVER: Connection interupted due to error.")
print(WARN+"SERVER: Connection interupted due to error. (0)")
status = False
return(status)
......@@ -162,7 +165,7 @@ class MplServer():
'''Treat signals of type readData.
1. unpack signal with instruction size,
2. send receipt,
3. unpack intructions,
3. unpack instructions,
4. execute instructions,
5. send dataID.
'''
......@@ -170,21 +173,24 @@ class MplServer():
self.sendReceipt(conn)
# Wait for the Instructions of given size
intructions = self.waitForInstructions(conn, instrucSize)
instructions = self.waitForInstructions(conn, instrucSize)
# If exchanged failed
if (not self.connected):
if (DEBUG):
print(WARN+"SERVER: Connection interupted due to error.")
print(WARN+"SERVER: Connection interupted due to error. (1)")
return(False)
# THIS HAS TO BE DONE IN A BETTER WAY CATCHING EXCEPTION AND DISANTANGLE UNCAPSULATION AND UNPACK
header, instructions = eval(instructions)
# Try to extract instructions
dataName, fct, args, kwargs = self.unpackReadDataInstructions(instructions)
dataName, fct, args, kwargs = self.unpackReadDataInstructions(conn, instructions)
# If failed
if (not self.connected):
if (DEBUG):
print(WARN+"SERVER: Connection interupted due to error.")
print(WARN+"SERVER: Connection interupted due to error. (2)")
return(False)
# Try to execute the instructions
......@@ -192,18 +198,19 @@ class MplServer():
G_RAWDATA[dataName] = fct(*args, **kwargs)
if (DEBUG):
print(INFO+"SERVER: I read the data following instructions")
print(str(G_RAWDATA[dataName].data)) # SHOULD BE SUPPRESS LATER
except:
if(DEBUG):
print(WARN+"SERVER: The intructions of readData could not be executed.")
print(WARN+"SERVER: The instructions of readData could not be executed.")
print(SPCE+"SERVER: I tried: "+str(fct.__name__)+'('+str(args)+', '+str(kwargs)+')')
answer = ('readData', 'False', 'could not execute intructions.')
answer = ('readData', 'False', 'could not execute instructions.')
self.sendAnswer(conn, answer)
self.closeConnection(conn)
return(False)
# Send the final message
answer = ('readData', str(dataName), 'data read with success.')
answer = ('readData', 'True', 'data read with success.')
self.sendAnswer(conn, answer)
return(True)
......@@ -237,30 +244,34 @@ class MplServer():
for string in answer:
packedAnswer = packedAnswer + "\"" + str(string) + "\"" + ', '
packedData = packedAnswer + ")"
packedAnswer = packedAnswer + ")"
return(packedAnswer)
# UNPACK INSTRUCTIONS FOR READ DATA --------------------------------
def unpackReadDataInstructions(self, instructions):
'''Unpack the intructions for read data.'''
def unpackReadDataInstructions(self, conn, instructions):
'''Unpack the instructions for read data.'''
dataName = ''
fct = None
args = ()
kwargs = {}
fct = 'None'
args = '()'
kwargs = '{}'
try:
dataName, fct, args, kwargs = eval(str(intructions))
dataName, fct, args, kwargs = eval(str(instructions))
except:
if(DEBUG):
print(WARN+"SERVER: The intructions of readData could not be extracted.")
print(SPCE+"SERVER: Received: "+str(content))
print(WARN+"SERVER: The instructions of readData could not be extracted.")
print(SPCE+"SERVER: Received: "+str(instructions))
print(SPCE+"SERVER: I expect '(dataName, functionName, args, kwargs)'")
answer = ('readData', 'False', 'could not extract intructions')
answer = ('readData', 'False', 'could not extract instructions')
self.sendAnswer(conn, answer)
self.closeConnection(conn)
fct = self.knownFunctions.get(fct, emptyfct) # SHOULD DEFINE EMPTY FUNCTION
args = eval(args)
kwargs = eval(kwargs)
return(dataName, fct, args, kwargs)
......@@ -380,6 +391,7 @@ class MplServer():
def establishConnection(self):
'''Establish connection with requesting client.'''
conn, addr = self.sock.accept()
self.connected = True
print(INFO + 'Connected by ' + str(addr))
return((conn, addr))
......@@ -390,7 +402,7 @@ class MplServer():
return(signal)
# WAIT FOR INSTRUCTIONS --------------------------------------------
def waitForInstructions(self, size, conn):
def waitForInstructions(self, conn, size):
'''Wait for instruction of given size.'''
instructions = conn.recv(size)
return(instructions)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment