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

Made the test with the client

readData is now working with the clean version.

Only remaining issue the unpacking of the instruction, done in a
single step should be done in two step.

1. removing header
2. extracting the content of instructions
parent f5c432ea
......@@ -60,7 +60,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 +129,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 +162,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 +170,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,12 +195,13 @@ 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)
......@@ -237,30 +241,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, None) # SHOULD DEFINE EMPTY FUNCTION
args = eval(args)
kwargs = eval(kwargs)
return(dataName, fct, args, kwargs)
......@@ -380,6 +388,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 +399,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)
......@@ -398,7 +407,7 @@ class MplServer():
# CLOSE CONNECTION -------------------------------------------------
def closeConnection(self, conn):
'''Close given connection.'''
conn.close()
# conn.close()
self.connected = False
return(True)
......
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