35 @param parent parent object |
35 @param parent parent object |
36 @type QObject |
36 @type QObject |
37 """ |
37 """ |
38 super(JsonServer, self).__init__(parent) |
38 super(JsonServer, self).__init__(parent) |
39 |
39 |
|
40 self.__name = name |
40 self.__multiplex = multiplex |
41 self.__multiplex = multiplex |
41 if self.__multiplex: |
42 if self.__multiplex: |
42 self.__clientProcesses = {} |
43 self.__clientProcesses = {} |
43 self.__connections = {} |
44 self.__connections = {} |
44 else: |
45 else: |
56 self.listen(QHostAddress(self.__hostAddress)) |
57 self.listen(QHostAddress(self.__hostAddress)) |
57 |
58 |
58 self.newConnection.connect(self.handleNewConnection) |
59 self.newConnection.connect(self.handleNewConnection) |
59 |
60 |
60 port = self.serverPort() |
61 port = self.serverPort() |
61 ## Note: Need the port if started external in debugger: |
62 ## Note: Need the port if client is started external in debugger. |
62 print('JSON server ({1}) listening on: {0:d}'.format(port, name)) |
63 print('JSON server ({1}) listening on: {0:d}' # __IGNORE_WARNING__ |
63 # __IGNORE_WARNING__ |
64 .format(port, self.__name)) |
64 |
65 |
65 @pyqtSlot() |
66 @pyqtSlot() |
66 def handleNewConnection(self): |
67 def handleNewConnection(self): |
67 """ |
68 """ |
68 Public slot for new incoming connections from a client. |
69 Public slot for new incoming connections from a client. |
135 |
136 |
136 while connection and connection.canReadLine(): |
137 while connection and connection.canReadLine(): |
137 data = connection.readLine() |
138 data = connection.readLine() |
138 jsonLine = bytes(data).decode("utf-8", 'backslashreplace') |
139 jsonLine = bytes(data).decode("utf-8", 'backslashreplace') |
139 |
140 |
140 ## print("JSON Server: ", jsonLine) ##debug |
141 ## print("JSON Server ({0}): {1}".format( |
|
142 ## self.__name, jsonLine)) ##debug |
141 |
143 |
142 try: |
144 try: |
143 clientDict = json.loads(jsonLine.strip()) |
145 clientDict = json.loads(jsonLine.strip()) |
144 except (TypeError, ValueError) as err: |
146 except (TypeError, ValueError) as err: |
145 E5MessageBox.critical( |
147 E5MessageBox.critical( |
233 @type str |
235 @type str |
234 """ |
236 """ |
235 self.sendJson("Exit", {}, flush=True, idString=idString) |
237 self.sendJson("Exit", {}, flush=True, idString=idString) |
236 |
238 |
237 if idString: |
239 if idString: |
238 connection = self.__connections[idString] |
240 try: |
|
241 connection = self.__connections[idString] |
|
242 except KeyError: |
|
243 connection = None |
239 else: |
244 else: |
240 connection = self.__connection |
245 connection = self.__connection |
241 if connection is not None: |
246 if connection is not None: |
242 connection.waitForDisconnected() |
247 connection.waitForDisconnected() |
243 |
248 |
244 if idString: |
249 if idString: |
245 self .__clientProcesses[idString].close() |
250 try: |
246 del self.__clientProcesses[idString] |
251 self .__clientProcesses[idString].close() |
|
252 del self.__clientProcesses[idString] |
|
253 except KeyError: |
|
254 pass |
247 else: |
255 else: |
248 if self.__clientProcess is not None: |
256 if self.__clientProcess is not None: |
249 self.__clientProcess.close() |
257 self.__clientProcess.close() |
250 self.__clientProcess = None |
258 self.__clientProcess = None |
251 |
259 |