-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server.py
69 lines (60 loc) · 2.38 KB
/
test_server.py
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
import multiprocessing
from multiprocessing.managers import SyncManager
import os
import ipaddress
class BorgTestServer:
_sharedState = {}
def __init__(self):
self.__dict__ = self._sharedState
class _TestServer:
def __init__(self):
self.typeString = type('asdf')
def add(self, a, b):
return a + b
def makeServerManager(self):
show = True
self.jobQ = multiprocessing.JoinableQueue(-1)
self.resultQ = multiprocessing.JoinableQueue(-1)
self.lock = multiprocessing.Lock()
class JobQueueManager(SyncManager):
pass
JobQueueManager.register('get_job_q', callable=lambda: self.jobQ)
JobQueueManager.register('get_result_q', callable=lambda: self.resultQ)
JobQueueManager.register('get_lock', callable=lambda: self.lock)
if show: print('starting manager with address =', ('', self.portNum), ', authkey', self.authKey)
manager = JobQueueManager(address=('', self.portNum), authkey = self.authKey)
manager.start()
print('started server manager')
return manager
def respondToClientRequest(self, methodNameAndArgs, sharedResultQ):
show = True
if show: print('in respondToClientRequest() with', methodNameAndArgs)
try:
if type(methodNameAndArgs) == self.typeString:
self.execResult = None
exec('self.execResult = ' + methodNameAndArgs)
else:
methodName = methodNameAndArgs[0]
sharedResultQ.put(_TestServer.__dict__[methodName](self, *methodNameAndArgs[1:]) )
except Exception as ex:
print('received exception in TestServer when', methodNameAndArgs, 'executed. returning exception', ex)
sharedResultQ.put(ex)
class TestServer(BorgTestServer, _TestServer):
portNum = 65001
#portNum = 8084
authKey = b'testMyServer'
def __init__(self):
BorgTestServer.__init__(self)
if self._sharedState:
return
_TestServer.__init__(self)
self.run()
def run(self):
show = True
self.serverManager = self.makeServerManager()
while True:
if show: print('waiting for method call...')
queueData = self.jobQ.get()
self.respondToClientRequest(queueData, self.resultQ)
if __name__ == '__main__':
ts = TestServer()