Commit 88a37ea5 authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

Cleaning the server

Functionalize connection, signals and instructions.
Add comments and docs.

not running, not tested.
parent 81269be8
......@@ -9,66 +9,160 @@ from . import SERVER_FIGURES
G_RAWDATA = {}
G_FIGURES = {}
# CLASS SERVER ---------------------------------------------------------
class Server():
def __init__(self):
# CONSTRUCTOR ------------------------------------------------------
def __init__(self, host, port):
'''This is a myplotlib server.
It listens to a tcp socket binded to host:port
'''
# create a TCP socket without delay by reinit.
self.sock = socket(AF_INET, SOCK_STREAM)
self.sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# define host and port
self.host = host
self.port = port
self.soc = socket(AF_INET, SOCK_STREAM)
self.soc.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) # requires some extra import
self.host = D_HOST
self.port = D_PORT
# set constants
self.running = False
self.initialized = False
self.connected = False
self.nbrAuthAccess = 1 # could be added in server-config.py
# DEFINE KNOWN I/O FUNCTIONS ---------------------------------------
def defineKnownFunctions(self):
'''Function to overwrite in order to declare the known
I/O functions by the server context.'''
self.knownFunctions = SERVER_IOFUNCTIONS
# DEFINE KNOWN FIGURE CLASSES --------------------------------------
def defineKnownFigures(self):
'''Function to overwrite in order to declare the known
Figure classes by the server context.'''
self.knownFigures = SERVER_FIGURES
# INITIAISATION ----------------------------------------------------
def init(self):
'''Intialisation of the server, defines known functions, known
figure classes, and bind the socket to the specific port.
It needs to be called.'''
# Define functions and figure classes
self.defineKnownFunctions()
self.defineKnownFigures()
self.soc.bind((self.host, self.port))
self.soc.listen(1)
# Bind socket and host:port
self.sock.bind((self.host, self.port))
self.sock.listen(self.nbrAuthAccess)
self.initialized = True
# RUNNING ----------------------------------------------------------
def run(self):
'''Listenning procedure. Wait for a signal, treat the
signal by single or several connections, and close the
interaction.'''
if (self.initialized):
self.running = True
signal = None
signal = False
while self.running:
while True:
# 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))
if not signal:
conn, addr = self.soc.accept()
print(INFO + 'Connected by ' + str(addr))
# Wait for a signal
signal = conn.recv(1024)
signal = conn.recv(1024)
# 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
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
# close interaction
else:
print(SEVR+'You need to initialize the server first.')
print(SPCE+'>>> mplServer.init()')
# PACKING ----------------------------------------------------------
def packFormattedData(self, formattedData):
'''formattedData is a tuple of as many strings as
''' Pack the formatted data for transfer.
FormattedData is a tuple of as many strings as
the figure has Axes. the packedData is the
string representation of it to be later evaluated.'''
string representation of it to be later evaluated
by the client.'''
# Generate a string of a tuple of strings
packedData = '('
for string in formattedData:
packedData = packedData + "\"" + str(string) + "\"" + ', '
packedData = packedData + ")"
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.
'''
pass
# SIGNAL NEW SYNC FIGURE -------------------------------------------
def treatNewSyncFigure(self):
'''Treat signals of type newSyncFigure
1. unpack signal with instruction size
2. send receipt,
3. unpack instructions,
4. create figure with corresponding rawdata
5. send FigureID
'''
pass
# SIGNAL UPDATE SYNC FIGURE ----------------------------------------
def treatUpdateSyncFigure(self):
'''Treat signals of type updateSyncFigure
1. unpack signal with instruction size
2. send receipt,
3. unpack instructions,
4. update figure with corresponding parameters
5. send Status
'''
pass
# SIGNAL SYNC FIGURE FORMAT RAWDATA --------------------------------
def treatSyncFigFormatRawData(self):
'''Treat signals of type SyncFigFormatRawData.
1. unpack signal with FigID,
2. Identify Figure and formatRawData,
3. send data size
4. wait for receipt
5. send packedFormattedData
'''
pass
# TREAT SIGNAL -----------------------------------------------------
def treatAnswer(self, signal):
'''Treat upcoming signals.'''
answer = None
......@@ -180,3 +274,42 @@ class Server():
return(answer)
return(answer)
# SEND ANSWER ------------------------------------------------------
def sendAnswer(self, conn, answer):
'''Send an answer to all clients.'''
conn.sendall(str(answer))
return(True)
# ESTABLISH CONNECTION ---------------------------------------------
def establishConnection(self):
'''Establish connection with requesting client.'''
conn, addr = self.sock.accept()
print(INFO + 'Connected by ' + str(addr))
return((conn, addr))
# WAIT FOR SIGNAL --------------------------------------------------
def waitForSignal(self, conn):
'''Wait for a signal.'''
signal = conn.recv(1024)
return(signal)
# WAIT FOR INSTRUCTIONS --------------------------------------------
def waitForInstructions(self, size, conn):
'''Wait for instruction of given size.'''
instructions = conn.recv(size)
return(instructions)
# CLOSE CONNECTION -------------------------------------------------
def closeConnection(self, conn):
'''Close given connection.'''
conn.close()
self.connected = False
return(True)
# STOP -------------------------------------------------------------
def stop(self, conn):
'''Stop myplServer.'''
print(INFO+"Recieved 'Kill' signal.")
conn.close()
self.runnning=False
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