server.py 5.41 KB
Newer Older
1
from . import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
2
3
from . import D_HOST
from . import D_PORT
Yori Fournier's avatar
Yori Fournier committed
4
from . import INFO, DBUG, WARN, SEVR, SPCE
5

6
from . import readStupidData
7
from . import FigTest
8
9

G_RAWDATA = {}
10
G_FIGURES = {}
11
12
13
14
15
16

class Server():

    def __init__(self):

        self.soc = socket(AF_INET, SOCK_STREAM)
17
        self.soc.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) # requires some extra import
18
19
20
21
22
23
24
25
26
27
        self.host = D_HOST
        self.port = D_PORT

    def init(self):

        self.soc.bind((self.host, self.port))
        self.soc.listen(1)
        
    def run(self):

28
        signal = False
Yori Fournier's avatar
Yori Fournier committed
29

30
31
        while True:

32
            if not signal:
33
34
35
                conn, addr = self.soc.accept()
                print(INFO + 'Connected by ' + str(addr))

36
            signal = conn.recv(1024)
37

38
            if signal in ('KILL', 'kill', 'Kill'):
39
40
41
                print(INFO+"Recieved 'Kill' signal.")
                conn.close()
                break
42
43
44
            elif signal != '':
                answer = self.treatAnswer(signal)
                conn.sendall(str(answer))
45
            else:
46
                pass
47

48
    def treatAnswer(self, signal):
49

50
51
        answer = None
        
52
        try:
53
            (header, content) = eval(str(signal))
54
        except:
55
            print(WARN+"I could not execute the command")
56
            print(SPCE+'Received: '+str(signal))
57
            print(SPCE+'Received: '+eval(str(signal)))
58
59
            print(SPCE+"I expect '(header, content)'")
            answer = ('readData', None, 'answer not correct format')
60
            return(answer)
61

62
        if header in ('readData', 'READDATA', 'readdata'):
63
            try:
64
                (dataName, functionName, args, kwargs) = eval(str(content))
65
66
67
68
69
            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')
70
                return(answer)
71
72
73

            try:
                
74
75
                print(str(functionName)+'('+str(args)+', '+str(kwargs)+')')

76
77
                if args is not None:
                    if kwargs is not None:
78
                        G_RAWDATA[dataName] = eval(str(functionName)+'('+str(args)+', '+str(kwargs)+')')
79
                    else:
80
                        G_RAWDATA[dataName] = eval(str(functionName)+'('+str(args)+')')
81
82
                else:
                    if kwargs is not None:
83
                        G_RAWDATA[dataName] = eval(str(functionName)+'('+str(kwargs)+')')
84
                    else:
85
                        G_RAWDATA[dataName] = eval(str(functionName)+'()')
86

87
88
                print(G_RAWDATA[dataName].data)
                answer = ('readData', dataName, 'no error')
89
90
91
92
                
            except:
                print(WARN+"The read function could not be executed")
                answer = ('readData', None, 'function could not be executed')
93
                return(answer)
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
        elif header in ('newSyncFigure'):

            try:
                (figClass, 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 = "("
                for raw in eval(rawdata):
                    datas = datas + "G_RAWDATA['"+raw+"'],"
                datas = datas + ")"
                print(datas)
                print(figClass+'('+str(datas)+', '+kwargs+')')
                G_FIGURES[FigID] = eval(figClass+'('+str(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)

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        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():
                datas.append(ax.data)

            answer = ('syncFigFormatRawData', datas, '')

153
154
        else:
            print(WARN+"I don't know ths signal, sorry")
155
156
            answer = ('readData', None, 'signal unknown')
            return(answer)
157
158

        return(answer)