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

Cleaned mplServer and mplClient

imports
not yet tested
parent 661187a8
......@@ -102,7 +102,7 @@ if D_HIERARCHY in ('CLIENT', 'client'):
# MyFig: Overlay on matplotlib.Figure class
from .myFig_client import MyFig_client as MyFig
from .serverInterface import ServerInterface
from .mplClient import MplClient
elif(D_HIERARCHY in ('SERVER', 'server')):
......@@ -121,7 +121,7 @@ elif(D_HIERARCHY in ('SERVER', 'server')):
SERVER_IOFUNCTIONS = {'readStupidData': readStupidData}
SERVER_FIGURES = {'FigTest', FigTest}
from .server import Server
from .mplServer import MplServer
elif D_HIERARCHY in ('LOCAL', 'local'):
......
from socket import socket, AF_INET, SOCK_STREAM
class ServerInterface(object):
# CLASS CLIENT ---------------------------------------------------------
class MplClient(object):
# CONSTRUCTOR ------------------------------------------------------
def __init__(self, ip, port):
'''This is a myplotlib client.
It sends instructions to a MplSever at host:port.
'''
self.ip = ip
self.port = port
# just a list of names
self.currentRemoteData = []
# PACK SIGNAL ------------------------------------------------------
def packSignal(self, unpackedSignal):
'''Pack the signal.'''
packedSignal = ''
header, instrucSize = unpackedSignal
header = str(header)
instrucSize = str(instrucSize)
packedSignal = "("+"\""+header+"\""+", "+instrucSize+")"
return(packedSignal)
# PACK INSTRUCTION -------------------------------------------------
def packInstructions(self, instructions):
'''Pack the instructions, a tuple of strings: (header, content).'''
header, content = instructions
packedInstructions = "(\""+header+"\", \""+content+"\")"
return(packedInstructions)
# PACK READDATA CONTENT --------------------------------------------
def packReadDataContent(self, ioFunction, dataName, *args, **kwargs):
'''Pack the content of the instructions for readData.'''
packedContent = "[\""+str(dataName)+"\", \""+ioFunction.__name__+"\", \""+str(args)+"\", \""+str(kwargs)+"\"]"
return(packedContent)
# READ DATA --------------------------------------------------------
def readData(self, ioFunction, dataName, *args, **kwargs):
'''Start the readData procedure.
1. Send ReadData Signal with length of packedInstructions
2. Wait for receipt
3. Send Instructions
4. Wait for status
'''
# Pack instructions and get size
content = self.packReadDataContent(ioFunction, dataName, *args, **kwargs)
packedInstructions = self.packInstructions(('readData', content))
instructionSize = len(packedInstructions)
content = "['"+str(dataName)+"', '"+ioFunction.__name__+"', \""+str(args)+"\", \""+str(kwargs)+"\"]"
# Send the signal with size
signal = self.packSignal(('readData', instructionSize))
signal = "("+"'readData'"+', '+str(content)+')'
self.sendSignal(signal)
if(DEBUG):
print(INFO+'Sent Signal, waiting for receipt...')
status = self.waitForReceipt()
# Check status
if not status:
return(False)
# Send Instructions
self.sendInstructions(packedInstructions)
# Check status
status = self.waitForStatus()
if not status:
return(False)
# create signal
sock = self.sendSignal(signal)
answer = self.waitForAnswer(sock)
header, content, errmsg = eval(answer)
dataName = content
# add data in the currentSyncData interface
if dataName is None:
print(SEVR+errmsg)
status=False
else:
self.currentRemoteData.append(dataName)
status=True
# content = "['"+str(dataName)+"', '"+ioFunction.__name__+"', \""+str(args)+"\", \""+str(kwargs)+"\"]"
#
# signal = "("+"'readData'"+', '+str(content)+')'
#
# # create signal
# sock = self.sendSignal(signal)
# answer = self.waitForAnswer(sock)
# header, content, errmsg = eval(answer)
# dataName = content
# # add data in the currentSyncData interface
# if dataName is None:
# print(SEVR+errmsg)
# status=False
# else:
# self.currentRemoteData.append(dataName)
# status=True
#
return(True)
# SEND SIGNAL ------------------------------------------------------
def sendSignal(self, packedSignal):
'''Send a signal: readData, newSyncFigure, syncFigFormatRawData'''
sock = socket(AF_INET, SOCK_STREAM)
sock.connect((self.ip, self.port))
sock.sendall(str(packedSignal))
return(sock)
# WAIT FOR RECEIPT -------------------------------------------------
def waitForReceipt(self, sock):
'''Wait for the receipt. return a tuple.'''
answer = sock.recv(SIZE_STATUS_MAX)
try:
header, status, errmsg = eval(answer)
except:
print(SEVR+'could not Exctract the receipt.')
if not status:
print(SEVR+str(errmsg))
return(status)
# SEND INSTRUCTIONS ------------------------------------------------
def sendInstructions(self, sock, packedInstructions):
'''Send Instructions.'''
sock.sendall(packedInstructions)
return(True)
# SEND ALT INSTRUCTIONS --------------------------------------------
# def sendInstructions(self, packedInstructions):
# '''Send Instructions.'''
# sock = socket(AF_INET, SOCK_STREAM)
# sock.connect((self.ip, self.port))
# sock.sendall(packedInstructions)
#
# return(sock)
# WAIT FOR STATUS --------------------------------------------------
def waitForStatus(self, sock):
'''Wait for the status. return a tuple.'''
answer = sock.recv(SIZE_STATUS_MAX)
try:
header, status, errmsg = eval(answer)
except:
print(SEVR+'could not Exctract the status.')
if not status:
print(SEVR+str(errmsg))
return(answer)
# NEW SYNC FIGURE --------------------------------------------------
def newSyncFigure(self, figClass, symbolicRawdata, **kwargs):
syncFigure = figClass(symbolicRawdata, **kwargs)
......@@ -57,9 +185,10 @@ class ServerInterface(object):
return(syncFigure)
# SYNC FIGURE FORMAT RAWDATA ---------------------------------------
def syncFigFormatRawData(self, syncID):
'''the returned datas should be a tuple of strings'''
content = "['"+str(syncID)+"']"
signal = "("+"'syncFigFormatRawData'"+", "+str(content)+")"
......@@ -112,14 +241,9 @@ class ServerInterface(object):
partlyUnpackedData = (header, evaluatedContent, errmsg)
return(partlyUnpackedData)
def sendSignal(self, signal):
sock = socket(AF_INET, SOCK_STREAM)
sock.connect((self.ip, self.port))
sock.sendall(str(signal))
return(sock)
# WAIT FOR ANSWER --------------------------------------------------
def waitForAnswer(self, sock):
answer = sock.recv(1024)
......
......@@ -13,7 +13,7 @@ G_RAWDATA = {}
G_FIGURES = {}
# CLASS SERVER ---------------------------------------------------------
class Server():
class MplServer():
# CONSTRUCTOR ------------------------------------------------------
def __init__(self, host, port):
......@@ -115,13 +115,13 @@ class Server():
# Format RawData
elif header == 'syncfigformatrawdata':
status = self.treatSyncFigFormatRawData(conn, content):
status = self.treatSyncFigFormatRawData(conn, content)
# Wrong Signal
else:
if (DEBUG):
print(WARN+"SERVER: I don't know ths signal, sorry.")
answer = ('', '', 'Signal unknown')
answer = ('', 'False', 'Signal unknown')
self.sendAnswer(conn, answer)
self.closeConnection(conn)
status = False
......@@ -140,25 +140,25 @@ class Server():
'''Unpack the signal.'''
header = ''
content = ''
instrucSize = ''
try:
(header, content) = eval(str(signal))
(header, instrucSize) = eval(str(signal))
header = str(header).lower()
except:
if (DEBUG):
print(WARN+"SERVER: I don't understand the signal.")
print(SPCE+'SERVER: Received: '+str(signal))
print(SPCE+'SERVER: Evaluated as: '+eval(str(signal)))
print(SPCE+"SERVER: Expected: '(header, content)'")
answer = ('', '', 'The signal has an unknwon format.')
print(SPCE+"SERVER: Expected: '(header, instrucSize)'")
answer = ('', 'False', 'The signal has an unknown format.')
self.sendAnswer(conn, answer)
self.closeConnection(conn)
return((header, content))
return((header, instrucSize))
# SIGNAL READDATA --------------------------------------------------
def treatReadData(self, conn, content):
def treatReadData(self, conn, instrucSize):
'''Treat signals of type readData.
1. unpack signal with instruction size,
2. send receipt,
......@@ -166,8 +166,6 @@ class Server():
4. execute instructions,
5. send dataID.
'''
instrucSize = content
# Send a receipt
self.sendReceipt(conn)
......@@ -181,7 +179,7 @@ class Server():
return(False)
# Try to extract instructions
dataName, fct, args, kwargs = self.unpackReadDataInstructions(instructions):
dataName, fct, args, kwargs = self.unpackReadDataInstructions(instructions)
# If failed
if (not self.connected):
......@@ -199,7 +197,7 @@ class Server():
print(WARN+"SERVER: The intructions of readData could not be executed.")
print(SPCE+"SERVER: I tried: "+str(fct.__name__)+'('+str(args)+', '+str(kwargs)+')')
answer = ('readData', '', 'could not execute intructions.')
answer = ('readData', 'False', 'could not execute intructions.')
self.sendAnswer(conn, answer)
self.closeConnection(conn)
return(False)
......@@ -229,7 +227,7 @@ class Server():
return(packedData)
# PACKING ----------------------------------------------------------
def packAnswer(self, answer)
def packAnswer(self, answer):
'''Pack the tuple of three strings (header, content, errmsg)
as a string representation of it, to be evaluated by the client.
'''
......@@ -260,14 +258,14 @@ class Server():
print(SPCE+"SERVER: Received: "+str(content))
print(SPCE+"SERVER: I expect '(dataName, functionName, args, kwargs)'")
answer = ('readData', '', 'could not extract intructions')
answer = ('readData', 'False', 'could not extract intructions')
self.sendAnswer(conn, answer)
self.closeConnection(conn)
return(dataName, fct, args, kwargs)
# SIGNAL NEW SYNC FIGURE -------------------------------------------
def treatNewSyncFigure(self, conn, content):
def treatNewSyncFigure(self, conn, instrucSize):
'''Treat signals of type newSyncFigure
1. unpack signal with instruction size
2. send receipt,
......@@ -309,7 +307,7 @@ class Server():
#
# SIGNAL UPDATE SYNC FIGURE ----------------------------------------
def treatUpdateSyncFigure(self, conn, content):
def treatUpdateSyncFigure(self, conn, instrucSize):
'''Treat signals of type updateSyncFigure
1. unpack signal with instruction size
2. send receipt,
......@@ -320,7 +318,7 @@ class Server():
pass
# SIGNAL SYNC FIGURE FORMAT RAWDATA --------------------------------
def treatSyncFigFormatRawData(self, conn, content):
def treatSyncFigFormatRawData(self, conn, instrucSize):
'''Treat signals of type SyncFigFormatRawData.
1. unpack signal with FigID,
2. Identify Figure and formatRawData,
......@@ -374,7 +372,7 @@ class Server():
# SEND RECEIPT -----------------------------------------------------
def sendReceipt(self, conn):
'''Send a receipt to all clients.'''
answer = self.packAnswer('Receipt')
answer = self.packAnswer(('','True','Receipt'))
conn.sendall(str(answer))
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