35 |
35 |
36 @param host ip address the background service is listening |
36 @param host ip address the background service is listening |
37 @param port port of the background service |
37 @param port port of the background service |
38 """ |
38 """ |
39 self.services = {} |
39 self.services = {} |
|
40 self.batchServices = {} |
40 |
41 |
41 self.connection = socket.create_connection((host, port)) |
42 self.connection = socket.create_connection((host, port)) |
42 ver = b'Python2' if sys.version_info[0] == 2 else b'Python3' |
43 ver = b'Python2' if sys.version_info[0] == 2 else b'Python3' |
43 self.connection.sendall(ver) |
44 self.connection.sendall(ver) |
44 |
45 |
53 """ |
54 """ |
54 sys.path.insert(1, path) |
55 sys.path.insert(1, path) |
55 try: |
56 try: |
56 importedModule = __import__(module, globals(), locals(), [], 0) |
57 importedModule = __import__(module, globals(), locals(), [], 0) |
57 self.services[fn] = importedModule.initService() |
58 self.services[fn] = importedModule.initService() |
|
59 try: |
|
60 self.batchServices["batch_" + fn] = \ |
|
61 importedModule.initBatchService() |
|
62 except AttributeError: |
|
63 pass |
58 return 'ok' |
64 return 'ok' |
59 except ImportError: |
65 except ImportError: |
60 return 'Import Error' |
66 return 'Import Error' |
61 |
67 |
62 def __send(self, fx, fn, data): |
68 def __send(self, fx, fn, data): |
110 packedData = packedData.decode('utf-8') |
116 packedData = packedData.decode('utf-8') |
111 |
117 |
112 fx, fn, data = json.loads(packedData) |
118 fx, fn, data = json.loads(packedData) |
113 if fx == 'INIT': |
119 if fx == 'INIT': |
114 ret = self.__initClientService(fn, *data) |
120 ret = self.__initClientService(fn, *data) |
|
121 elif fx.startswith("batch_"): |
|
122 callback = self.batchServices.get(fx) |
|
123 if callback: |
|
124 callback(data, self.__send, fx) |
|
125 ret = "__DONE__" |
|
126 else: |
|
127 ret = 'Unknown batch service.' |
115 else: |
128 else: |
116 callback = self.services.get(fx) |
129 callback = self.services.get(fx) |
117 if callback: |
130 if callback: |
118 ret = callback(fn, *data) |
131 ret = callback(fn, *data) |
119 else: |
132 else: |