mplServer2.py 4.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import SocketServer
import pickle

from .signal import Signal,Status,Query,Answer

from . import SERVER_IOFUNCTIONS
from . import SERVER_FIGURES

from . import DBUG,WARN,SPCE,SEVR,INFO

G_RAWDATA = dict()

DEBUG = True
class MplHandler(SocketServer.StreamRequestHandler):
    def servPrint(self,x) :
        print("SERVER: "+x)
    
    def treatReadData(self,content):
        '''Treat signals of type readData.
        1. unpack signal with instruction size, 
        2. send receipt, 
        3. unpack instructions, 
        4. execute instructions,
        5. send dataID.
        '''
        
        fct      = SERVER_IOFUNCTIONS[content.get('func')]
        args     = content.get('args')
        kwargs   = content.get('kwargs')
        dataName = content.get('dataname')
        
         # Try to execute the instructions
        try:
            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 instructions of readData could not be executed.")
                print(SPCE+"SERVER: I tried: "+str(fct.__name__)+'('+str(args)+', '+str(kwargs)+')')
                status = Status(False,"The instructions of readData could not be executed. I tried: "+str(fct.__name__)+'('+str(args)+', '+str(kwargs)+')')
        if dataName in G_RAWDATA :
            status = Status(True,"SERVER: I read the data following instructions"+str(fct.__name__)+'('+str(args)+', '+str(kwargs)+')')
        return status

    def treatNewSyncFigure(content) :
        pass
    def treatUpdateSyncFigure(content) :
        pass
    def treatSyncFigFormatRawData(content):
        pass

    def treatSig(self,sig):
        print('I am here')
        print(str(sig))
        print(type(sig))
        print(isinstance(sig, Query))
        if   isinstance(sig,Query) :
            if   sig.queryType == sig.READDATA :
                return self.treatReadData(sig.content)
            elif sig.queryType == sig.NEWSYNCFIGURE :
                return self.treatNewSyncFigure(sig.content)
            elif sig.queryType == sig.UPDATESYNCFIGURE :
                return self.treatUpdateSyncFigure(sig.content) 
            elif sig.queryType == sig.SYNCFIGFORMATRAWDATA :
                return self.treatSyncFigFormatRawData(sig.content)
            else :
                self.servPrint('received unknown query type')
                return Status(False,"received unknown query type")
        elif isinstance(sig, Status) :
            if sig.value :
                pass #if it is just a positiv status message do nothing
            else :
                self.servPrint('the client Side reported of an error:')
                self.servPrint('----'+sig.content)
        elif isinstance(sig, Answer) :
            #~ #needed for server???
            pass
        elif isinstance(sig, Signal) :
            self.servPrint('received unknown signal')
            return Status(False,"received unknown signal")
        else : 
            self.servPrint('what is happening here??? received Object of type:')
            self.servPrint(str(sig))
            return Status(False,"received something totally diffenent")
            
    
    def handle(self):
        # self.rfile is a file-like object created by the handler;
        # we can now use e.g. readline() instead of raw recv() calls
        ## self.data = self.rfile.readline()#.strip()
        print('before pickle')
        request = pickle.load(self.rfile)
        print('after pickle')
        reply = self.treatSig(request)
        # Likewise, self.wfile is a file-like object used to write back
        # to the client
        ## self.wfile.write(self.data.upper())
        if isinstance(reply,Signal) :
            self.servPrint("returning {}-Signal:".format(type(reply)))
            pickle.dump(reply,self.wfile)

class MplServer2(SocketServer.TCPServer):
    
    allow_reuse_address=True
    
    # 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

    def __init__(self,*args, **kwargs) :
        '''Intialisation of the server, defines known functions, known
        figure classes. It needs to be called.'''

        # Define functions and figure classes
        self.defineKnownFunctions()
        self.defineKnownFigures()
        SocketServer.TCPServer.__init__(self,*args, **kwargs)
        
        self.initialized = True