Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Yori Fournier
myplotlib
Commits
661187a8
Commit
661187a8
authored
Sep 05, 2017
by
Yori 'AGy' Fournier
Browse files
Add some functions in mplServer.py
readData should now work, need to be tested.
parent
c2e116c4
Changes
1
Hide whitespace changes
Inline
Side-by-side
mplServer.py
View file @
661187a8
...
...
@@ -3,6 +3,9 @@ from . import D_HOST
from
.
import
D_PORT
from
.
import
INFO
,
DBUG
,
WARN
,
SEVR
,
SPCE
from
.
import
D_DEBUG
DEBUG
=
D_DEBUG
from
.
import
SERVER_IOFUNCTIONS
from
.
import
SERVER_FIGURES
...
...
@@ -74,31 +77,140 @@ class Server():
while
self
.
running
:
# If no signal accept any new connection
if
(
not
self
.
connected
)
or
(
signal
is
None
):
conn
,
addr
=
self
.
sock
.
accept
()
print
(
INFO
+
'Connected by '
+
str
(
addr
))
# Wait for a signal
signal
=
conn
.
recv
(
1024
)
conn
,
addr
=
self
.
establishConnection
()
# Treat the signal
if
signal
in
(
'KILL'
,
'kill'
,
'Kill'
):
print
(
INFO
+
"Recieved 'Kill' signal."
)
conn
.
close
()
break
elif
signal
!=
''
:
answer
=
self
.
treatAnswer
(
signal
)
conn
.
sendall
(
str
(
answer
))
else
:
pass
signal
=
self
.
waitForSignal
(
conn
)
# close interaction
status
=
self
.
treatSignal
(
conn
,
signal
)
if
(
status
):
self
.
closeConnection
(
conn
)
else
:
print
(
SEVR
+
'You need to initialize the server first.'
)
print
(
SPCE
+
'>>> mplServer.init()'
)
# TREAT SIGNAL -----------------------------------------------------
def
treatSignal
(
self
,
conn
,
signal
):
'''Treat upcoming signals.'''
# Unpack Signal
header
,
content
=
self
.
unpackSignal
(
signal
)
# If connection still active
if
(
self
.
connected
):
# Read Data
if
header
==
'readdata'
:
status
=
self
.
treatReadData
(
conn
,
content
)
# New SyncFigure
elif
header
==
'newsyncfigure'
:
status
=
self
.
treatNewSyncFigure
(
conn
,
content
)
# Update SyncFigure
elif
header
==
'updatesyncfigure'
:
status
=
self
.
treatUpdateSyncFigure
(
conn
,
content
)
# Format RawData
elif
header
==
'syncfigformatrawdata'
:
status
=
self
.
treatSyncFigFormatRawData
(
conn
,
content
):
# Wrong Signal
else
:
if
(
DEBUG
):
print
(
WARN
+
"SERVER: I don't know ths signal, sorry."
)
answer
=
(
''
,
''
,
'Signal unknown'
)
self
.
sendAnswer
(
conn
,
answer
)
self
.
closeConnection
(
conn
)
status
=
False
# catch possible error
else
:
if
(
DEBUG
):
print
(
WARN
+
"SERVER: Connection interupted due to error."
)
status
=
False
return
(
status
)
# UNPACK SIGNAL ----------------------------------------------------
def
unpackSignal
(
self
,
signal
):
'''Unpack the signal.'''
header
=
''
content
=
''
try
:
(
header
,
content
)
=
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.'
)
self
.
sendAnswer
(
conn
,
answer
)
self
.
closeConnection
(
conn
)
return
((
header
,
content
))
# SIGNAL READDATA --------------------------------------------------
def
treatReadData
(
self
,
conn
,
content
):
'''Treat signals of type readData.
1. unpack signal with instruction size,
2. send receipt,
3. unpack intructions,
4. execute instructions,
5. send dataID.
'''
instrucSize
=
content
# Send a receipt
self
.
sendReceipt
(
conn
)
# Wait for the Instructions of given size
intructions
=
self
.
waitForInstructions
(
conn
,
instrucSize
)
# If exchanged failed
if
(
not
self
.
connected
):
if
(
DEBUG
):
print
(
WARN
+
"SERVER: Connection interupted due to error."
)
return
(
False
)
# Try to extract instructions
dataName
,
fct
,
args
,
kwargs
=
self
.
unpackReadDataInstructions
(
instructions
):
# If failed
if
(
not
self
.
connected
):
if
(
DEBUG
):
print
(
WARN
+
"SERVER: Connection interupted due to error."
)
return
(
False
)
# Try to execute the instructions
try
:
G_RAWDATA
[
dataName
]
=
fct
(
*
args
,
**
kwargs
)
if
(
DEBUG
):
print
(
INFO
+
"SERVER: I read the data following instructions"
)
except
:
if
(
DEBUG
):
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.'
)
self
.
sendAnswer
(
conn
,
answer
)
self
.
closeConnection
(
conn
)
return
(
False
)
# Send the final message
answer
=
(
'readData'
,
str
(
dataName
),
'data read with success.'
)
self
.
sendAnswer
(
conn
,
answer
)
return
(
True
)
# PACKING ----------------------------------------------------------
def
packFormattedData
(
self
,
formattedData
):
''' Pack the formatted data for transfer.
...
...
@@ -116,19 +228,46 @@ class Server():
return
(
packedData
)
# SIGNAL READDATA --------------------------------------------------
def
treatReadData
(
self
):
'''Treat signals of type readData.
1. unpack signal with instruction size,
2. send receipt,
3. unpack intructions,
4. execute instructions,
5. send dataID.
# PACKING ----------------------------------------------------------
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.
'''
pass
# Generate a string of a tuple of strings
packedAnswer
=
'('
for
string
in
answer
:
packedAnswer
=
packedAnswer
+
"
\"
"
+
str
(
string
)
+
"
\"
"
+
', '
packedData
=
packedAnswer
+
")"
return
(
packedAnswer
)
# UNPACK INSTRUCTIONS FOR READ DATA --------------------------------
def
unpackReadDataInstructions
(
self
,
instructions
):
'''Unpack the intructions for read data.'''
dataName
=
''
fct
=
None
args
=
()
kwargs
=
{}
try
:
dataName
,
fct
,
args
,
kwargs
=
eval
(
str
(
intructions
))
except
:
if
(
DEBUG
):
print
(
WARN
+
"SERVER: The intructions of readData could not be extracted."
)
print
(
SPCE
+
"SERVER: Received: "
+
str
(
content
))
print
(
SPCE
+
"SERVER: I expect '(dataName, functionName, args, kwargs)'"
)
answer
=
(
'readData'
,
''
,
'could not extract intructions'
)
self
.
sendAnswer
(
conn
,
answer
)
self
.
closeConnection
(
conn
)
return
(
dataName
,
fct
,
args
,
kwargs
)
# SIGNAL NEW SYNC FIGURE -------------------------------------------
def
treatNewSyncFigure
(
self
):
def
treatNewSyncFigure
(
self
,
conn
,
content
):
'''Treat signals of type newSyncFigure
1. unpack signal with instruction size
2. send receipt,
...
...
@@ -138,8 +277,39 @@ class Server():
'''
pass
# try:
# (figClassName, rawdata, kwargs) = eval(str(content))
# except:
# print(WARN+"The content of readData could not be extracted")
# print(SPCE+'Received: '+str(content))
# print(SPCE+"I expect '(figClass, rawdata, kwargs)'")
# answer = ('newSyncFigure', None, 'could not extract content')
# return(answer)
#
# if(True):
## try:
## print(figClass.__name__, rawdata, kwargs)
# FigID = 'Test'
## datas = "("
# kwargs = eval(kwargs)
# datas = ()
# for raw in eval(rawdata):
# datas = datas + (G_RAWDATA[raw],)
## print(datas)
# figClass = self.knownFigures[figClassName]
## print(figClass+'('+str(datas)+', '+kwargs+')')
## G_FIGURES[FigID] = eval(figClass+'('+str(datas)+', '+kwargs+')')
# G_FIGURES[FigID] = figClass(datas, **kwargs)
#
# answer = ('newSyncFigure', FigID, '')
## except:
## print(WARN+"The required fig could not be created")
## answer = ('newSyncFigure', None, 'could not create the Figure')
## return(answer)
#
# SIGNAL UPDATE SYNC FIGURE ----------------------------------------
def
treatUpdateSyncFigure
(
self
):
def
treatUpdateSyncFigure
(
self
,
conn
,
content
):
'''Treat signals of type updateSyncFigure
1. unpack signal with instruction size
2. send receipt,
...
...
@@ -150,7 +320,7 @@ class Server():
pass
# SIGNAL SYNC FIGURE FORMAT RAWDATA --------------------------------
def
treatSyncFigFormatRawData
(
self
):
def
treatSyncFigFormatRawData
(
self
,
conn
,
content
):
'''Treat signals of type SyncFigFormatRawData.
1. unpack signal with FigID,
2. Identify Figure and formatRawData,
...
...
@@ -159,125 +329,52 @@ class Server():
5. send packedFormattedData
'''
pass
# TREAT SIGNAL -----------------------------------------------------
def
treatAnswer
(
self
,
signal
):
'''Treat upcoming signals.'''
answer
=
None
try
:
(
header
,
content
)
=
eval
(
str
(
signal
))
except
:
print
(
WARN
+
"I could not execute the command"
)
print
(
SPCE
+
'Received: '
+
str
(
signal
))
print
(
SPCE
+
'Received: '
+
eval
(
str
(
signal
)))
print
(
SPCE
+
"I expect '(header, content)'"
)
answer
=
(
'readData'
,
None
,
'answer not correct format'
)
return
(
answer
)
if
header
in
(
'readData'
,
'READDATA'
,
'readdata'
):
try
:
(
dataName
,
functionName
,
args
,
kwargs
)
=
eval
(
str
(
content
))
except
:
print
(
WARN
+
"The content of readData could not be extracted"
)
print
(
SPCE
+
'Received: '
+
str
(
content
))
print
(
SPCE
+
"I expect '(dataName, functionName, args, kwargs)'"
)
answer
=
(
'readData'
,
None
,
'could not extract content'
)
return
(
answer
)
try
:
args
=
eval
(
str
(
args
))
kwargs
=
eval
(
str
(
kwargs
))
fct
=
self
.
knownFunctions
[
str
(
functionName
)]
G_RAWDATA
[
dataName
]
=
fct
(
*
args
,
**
kwargs
)
# print(G_RAWDATA[dataName].data)
answer
=
(
'readData'
,
dataName
,
'no error'
)
except
:
print
(
WARN
+
"The read function could not be executed"
)
answer
=
(
'readData'
,
None
,
'function could not be executed'
)
return
(
answer
)
elif
header
in
(
'newSyncFigure'
):
try
:
(
figClassName
,
rawdata
,
kwargs
)
=
eval
(
str
(
content
))
except
:
print
(
WARN
+
"The content of readData could not be extracted"
)
print
(
SPCE
+
'Received: '
+
str
(
content
))
print
(
SPCE
+
"I expect '(figClass, rawdata, kwargs)'"
)
answer
=
(
'newSyncFigure'
,
None
,
'could not extract content'
)
return
(
answer
)
if
(
True
):
# try:
# print(figClass.__name__, rawdata, kwargs)
FigID
=
'Test'
# datas = "("
kwargs
=
eval
(
kwargs
)
datas
=
()
for
raw
in
eval
(
rawdata
):
datas
=
datas
+
(
G_RAWDATA
[
raw
],)
# print(datas)
figClass
=
self
.
knownFigures
[
figClassName
]
# print(figClass+'('+str(datas)+', '+kwargs+')')
# G_FIGURES[FigID] = eval(figClass+'('+str(datas)+', '+kwargs+')')
G_FIGURES
[
FigID
]
=
figClass
(
datas
,
**
kwargs
)
answer
=
(
'newSyncFigure'
,
FigID
,
''
)
# try:
# (figID,) = eval(str(content))
# except:
# print(WARN+"The required fig could not be created")
# answer = ('newSyncFigure', None, 'could not create the Figure')
# print(WARN+"The content of syncFigFormatRawData could not be extracted")
# print(SPCE+'Received: '+str(content))
# print(SPCE+"I expect '(figID,)'")
# answer = ('syncFigFormatRawData', None, 'could not extract content')
# return(answer)
elif
header
in
(
'syncFigFormatRawData'
):
try
:
(
figID
,)
=
eval
(
str
(
content
))
except
:
print
(
WARN
+
"The content of syncFigFormatRawData could not be extracted"
)
print
(
SPCE
+
'Received: '
+
str
(
content
))
print
(
SPCE
+
"I expect '(figID,)'"
)
answer
=
(
'syncFigFormatRawData'
,
None
,
'could not extract content'
)
return
(
answer
)
try
:
fig
=
G_FIGURES
[
figID
]
except
:
print
(
WARN
+
"Figure ID does not exists."
)
print
(
SPCE
+
str
(
figID
))
answer
=
(
'syncFigFormatRawData'
,
None
,
'Figure ID does not exists.'
)
try
:
fig
.
formatRawData
()
except
:
print
(
WARN
+
'Could not format the rawdata of the figure.'
)
answer
=
(
'syncFigFormatRawData'
,
None
,
'Could not format the rawdata of the figure.'
)
datas
=
[]
for
ax
in
fig
.
get_axes
():
# ax.data is any user defined object
# ax.packFormattedData returns it's string representation
# to be unpacked by ax.unpackFormattedData
datas
.
append
(
ax
.
packFormattedData
(
ax
.
data
))
packedData
=
self
.
packFormattedData
(
datas
)
answer
=
(
'syncFigFormatRawData'
,
packedData
,
''
)
else
:
print
(
WARN
+
"I don't know ths signal, sorry"
)
answer
=
(
'readData'
,
None
,
'signal unknown'
)
return
(
answer
)
return
(
answer
)
#
# try:
# fig = G_FIGURES[figID]
# except:
# print(WARN+"Figure ID does not exists.")
# print(SPCE+str(figID))
# answer = ('syncFigFormatRawData', None, 'Figure ID does not exists.')
#
# try:
# fig.formatRawData()
# except:
# print(WARN+'Could not format the rawdata of the figure.')
# answer = ('syncFigFormatRawData', None, 'Could not format the rawdata of the figure.')
#
# datas = []
# for ax in fig.get_axes():
# # ax.data is any user defined object
# # ax.packFormattedData returns it's string representation
# # to be unpacked by ax.unpackFormattedData
# datas.append(ax.packFormattedData(ax.data))
#
# packedData = self.packFormattedData(datas)
#
# answer = ('syncFigFormatRawData', packedData, '')
#
# SEND ANSWER ------------------------------------------------------
def
sendAnswer
(
self
,
conn
,
answer
):
'''Send an answer to all clients.'''
answer
=
self
.
packAnswer
(
answer
)
conn
.
sendall
(
str
(
answer
))
return
(
True
)
# SEND RECEIPT -----------------------------------------------------
def
sendReceipt
(
self
,
conn
):
'''Send a receipt to all clients.'''
answer
=
self
.
packAnswer
(
'Receipt'
)
conn
.
sendall
(
str
(
answer
))
return
(
True
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment