Wed, 13 Jul 2022 14:55:47 +0200
Reformatted the source code using the 'Black' utility.
8300 | 1 | # -*- coding: utf-8 -*- |
2 | ||
8881
54e42bc2437a
Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
3 | # Copyright (c) 2017 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
8300 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing the JSON based server base class. | |
8 | """ | |
9 | ||
10 | import contextlib | |
11 | import json | |
12 | ||
8318
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
13 | from PyQt6.QtCore import ( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
14 | pyqtSlot, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
15 | QProcess, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
16 | QProcessEnvironment, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
17 | QCoreApplication, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
18 | QEventLoop, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
19 | QTimer, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
20 | QThread, |
8300 | 21 | ) |
8318
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
22 | from PyQt6.QtNetwork import QTcpServer, QHostAddress |
8300 | 23 | |
8358
144a6b854f70
Sorted the eric specific extensions into packages named like the corresponding PyQt packages (i.e. EricCore,EricGui and EricWidgets).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8356
diff
changeset
|
24 | from EricWidgets import EricMessageBox |
8300 | 25 | |
26 | import Preferences | |
27 | import Utilities | |
28 | ||
29 | ||
8354
12ebd3934fef
Renamed 'E5Utilities' to 'EricUtilities' and 'E5Network' to 'EricNetwork'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8322
diff
changeset
|
30 | class EricJsonServer(QTcpServer): |
8300 | 31 | """ |
32 | Class implementing a JSON based server base class. | |
33 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
34 | |
8300 | 35 | def __init__(self, name="", multiplex=False, parent=None): |
36 | """ | |
37 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
38 | |
8300 | 39 | @param name name of the server (used for output only) |
40 | @type str | |
41 | @param multiplex flag indicating a multiplexing server | |
42 | @type bool | |
43 | @param parent parent object | |
44 | @type QObject | |
45 | """ | |
46 | super().__init__(parent) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
47 | |
8300 | 48 | self.__name = name |
49 | self.__multiplex = multiplex | |
50 | if self.__multiplex: | |
51 | self.__clientProcesses = {} | |
52 | self.__connections = {} | |
53 | else: | |
54 | self.__clientProcess = None | |
55 | self.__connection = None | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
56 | |
8300 | 57 | # setup the network interface |
58 | networkInterface = Preferences.getDebugger("NetworkInterface") | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
59 | if networkInterface == "all" or "." in networkInterface: |
8300 | 60 | # IPv4 |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
61 | self.__hostAddress = "127.0.0.1" |
8300 | 62 | else: |
63 | # IPv6 | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
64 | self.__hostAddress = "::1" |
8300 | 65 | self.listen(QHostAddress(self.__hostAddress)) |
66 | ||
67 | self.newConnection.connect(self.handleNewConnection) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
68 | |
8300 | 69 | ## Note: Need the port if client is started external in debugger. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
70 | print( # __IGNORE_WARNING_M801__ |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
71 | "JSON server ({1}) listening on: {0:d}".format( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
72 | self.serverPort(), self.__name |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
73 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
74 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
75 | |
8300 | 76 | @pyqtSlot() |
77 | def handleNewConnection(self): | |
78 | """ | |
79 | Public slot for new incoming connections from a client. | |
80 | """ | |
81 | connection = self.nextPendingConnection() | |
82 | if not connection.isValid(): | |
83 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
84 | |
8300 | 85 | if self.__multiplex: |
86 | if not connection.waitForReadyRead(3000): | |
87 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
88 | idString = ( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
89 | bytes(connection.readLine()).decode("utf-8", "backslashreplace").strip() |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
90 | ) |
8300 | 91 | if idString in self.__connections: |
92 | self.__connections[idString].close() | |
93 | self.__connections[idString] = connection | |
94 | else: | |
95 | idString = "" | |
96 | if self.__connection is not None: | |
97 | self.__connection.close() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
98 | |
8300 | 99 | self.__connection = connection |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
100 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
101 | connection.readyRead.connect(lambda: self.__receiveJson(idString)) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
102 | connection.disconnected.connect(lambda: self.__handleDisconnect(idString)) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
103 | |
8300 | 104 | @pyqtSlot() |
105 | def __handleDisconnect(self, idString): | |
106 | """ | |
107 | Private slot handling a disconnect of the client. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
108 | |
8300 | 109 | @param idString id of the connection been disconnected |
110 | @type str | |
111 | """ | |
112 | if idString: | |
113 | if idString in self.__connections: | |
114 | self.__connections[idString].close() | |
115 | del self.__connections[idString] | |
116 | else: | |
117 | if self.__connection is not None: | |
118 | self.__connection.close() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
119 | |
8300 | 120 | self.__connection = None |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
121 | |
8300 | 122 | def connectionNames(self): |
123 | """ | |
124 | Public method to get the list of active connection names. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
125 | |
8300 | 126 | If this is not a multiplexing server, an empty list is returned. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
127 | |
8300 | 128 | @return list of active connection names |
129 | @rtype list of str | |
130 | """ | |
131 | if self.__multiplex: | |
132 | return list(self.__connections.keys()) | |
133 | else: | |
134 | return [] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
135 | |
8300 | 136 | @pyqtSlot() |
137 | def __receiveJson(self, idString): | |
138 | """ | |
139 | Private slot handling received data from the client. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
140 | |
9057
ddc46e93ccc4
Added classes to realize a JSON based stream between two processes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8943
diff
changeset
|
141 | @param idString id of the connection |
8300 | 142 | @type str |
143 | """ | |
144 | if idString: | |
145 | try: | |
146 | connection = self.__connections[idString] | |
147 | except KeyError: | |
148 | connection = None | |
149 | else: | |
150 | connection = self.__connection | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
151 | |
8300 | 152 | while connection and connection.canReadLine(): |
153 | data = connection.readLine() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
154 | jsonLine = bytes(data).decode("utf-8", "backslashreplace") |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
155 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
156 | # - print("JSON Server ({0}): {1}".format(self.__name, jsonLine)) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
157 | # - this is for debugging only |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
158 | |
8300 | 159 | try: |
160 | clientDict = json.loads(jsonLine.strip()) | |
161 | except (TypeError, ValueError) as err: | |
8356
68ec9c3d4de5
Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8354
diff
changeset
|
162 | EricMessageBox.critical( |
8300 | 163 | None, |
164 | self.tr("JSON Protocol Error"), | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
165 | self.tr( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
166 | """<p>The response received from the client""" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
167 | """ could not be decoded. Please report""" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
168 | """ this issue with the received data to the""" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
169 | """ eric bugs email address.</p>""" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
170 | """<p>Error: {0}</p>""" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
171 | """<p>Data:<br/>{1}</p>""" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
172 | ).format(str(err), Utilities.html_encode(jsonLine.strip())), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
173 | EricMessageBox.Ok, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
174 | ) |
8300 | 175 | return |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
176 | |
8300 | 177 | self.handleCall(clientDict["method"], clientDict["params"]) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
178 | |
8300 | 179 | def sendJson(self, command, params, flush=False, idString=""): |
180 | """ | |
181 | Public method to send a single command to a client. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
182 | |
8300 | 183 | @param command command name to be sent |
184 | @type str | |
185 | @param params dictionary of named parameters for the command | |
186 | @type dict | |
187 | @param flush flag indicating to flush the data to the socket | |
188 | @type bool | |
189 | @param idString id of the connection to send data to | |
190 | @type str | |
191 | """ | |
192 | commandDict = { | |
193 | "jsonrpc": "2.0", | |
194 | "method": command, | |
195 | "params": params, | |
196 | } | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
197 | cmd = json.dumps(commandDict) + "\n" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
198 | |
8300 | 199 | if idString: |
200 | try: | |
201 | connection = self.__connections[idString] | |
202 | except KeyError: | |
203 | connection = None | |
204 | else: | |
205 | connection = self.__connection | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
206 | |
8300 | 207 | if connection is not None: |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
208 | data = cmd.encode("utf8", "backslashreplace") |
8300 | 209 | length = "{0:09d}".format(len(data)) |
210 | connection.write(length.encode() + data) | |
211 | if flush: | |
212 | connection.flush() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
213 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
214 | def startClient( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
215 | self, interpreter, clientScript, clientArgs, idString="", environment=None |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
216 | ): |
8300 | 217 | """ |
218 | Public method to start a client process. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
219 | |
8300 | 220 | @param interpreter interpreter to be used for the client |
221 | @type str | |
222 | @param clientScript path to the client script | |
223 | @type str | |
224 | @param clientArgs list of arguments for the client | |
225 | @param idString id of the client to be started | |
226 | @type str | |
227 | @param environment dictionary of environment settings to pass | |
228 | @type dict | |
8301
952a05857e81
E5JsonServer: changed code to return the exit code in case of a premature exit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8300
diff
changeset
|
229 | @return flag indicating a successful client start and the exit code |
952a05857e81
E5JsonServer: changed code to return the exit code in case of a premature exit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8300
diff
changeset
|
230 | in case of an issue |
952a05857e81
E5JsonServer: changed code to return the exit code in case of a premature exit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8300
diff
changeset
|
231 | @rtype bool, int |
8300 | 232 | """ |
233 | if interpreter == "" or not Utilities.isinpath(interpreter): | |
234 | return False | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
235 | |
8301
952a05857e81
E5JsonServer: changed code to return the exit code in case of a premature exit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8300
diff
changeset
|
236 | exitCode = None |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
237 | |
8300 | 238 | proc = QProcess() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
239 | proc.setProcessChannelMode(QProcess.ProcessChannelMode.ForwardedChannels) |
8300 | 240 | if environment is not None: |
241 | env = QProcessEnvironment() | |
242 | for key, value in list(environment.items()): | |
243 | env.insert(key, value) | |
244 | proc.setProcessEnvironment(env) | |
245 | args = [clientScript, self.__hostAddress, str(self.serverPort())] | |
246 | if idString: | |
247 | args.append(idString) | |
248 | args.extend(clientArgs) | |
249 | proc.start(interpreter, args) | |
250 | if not proc.waitForStarted(10000): | |
251 | proc = None | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
252 | |
8300 | 253 | if idString: |
254 | self.__clientProcesses[idString] = proc | |
255 | if proc: | |
256 | timer = QTimer() | |
257 | timer.setSingleShot(True) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
258 | timer.start(30000) # 30s timeout |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
259 | while idString not in self.connectionNames() and timer.isActive(): |
8300 | 260 | # Give the event loop the chance to process the new |
261 | # connection of the client (= slow start). | |
262 | QCoreApplication.processEvents( | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
263 | QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
264 | ) |
8934
d3798915e0d2
Changed some code to not call QCoreApplication.processEvents() too often.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8881
diff
changeset
|
265 | QThread.msleep(100) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
266 | |
8301
952a05857e81
E5JsonServer: changed code to return the exit code in case of a premature exit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8300
diff
changeset
|
267 | # check if client exited prematurely |
8303
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
268 | if proc.state() == QProcess.ProcessState.NotRunning: |
8301
952a05857e81
E5JsonServer: changed code to return the exit code in case of a premature exit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8300
diff
changeset
|
269 | exitCode = proc.exitCode() |
952a05857e81
E5JsonServer: changed code to return the exit code in case of a premature exit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8300
diff
changeset
|
270 | proc = None |
952a05857e81
E5JsonServer: changed code to return the exit code in case of a premature exit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8300
diff
changeset
|
271 | self.__clientProcesses[idString] = None |
952a05857e81
E5JsonServer: changed code to return the exit code in case of a premature exit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8300
diff
changeset
|
272 | break |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
273 | |
8936
dca47d2dde1c
Some performance optimizations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8934
diff
changeset
|
274 | QThread.msleep(500) |
8300 | 275 | else: |
8303
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
276 | if proc: |
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
277 | timer = QTimer() |
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
278 | timer.setSingleShot(True) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
279 | timer.start(1000) # 1s timeout |
8303
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
280 | while timer.isActive(): |
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
281 | # check if client exited prematurely |
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
282 | QCoreApplication.processEvents( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
283 | QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
284 | ) |
8943
23f9c7b9e18e
Implemented some performance improvements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8936
diff
changeset
|
285 | QThread.msleep(100) |
8303
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
286 | if proc.state() == QProcess.ProcessState.NotRunning: |
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
287 | exitCode = proc.exitCode() |
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
288 | proc = None |
0cbba94590d2
E5JsonServer: fixed an issue introduced by the latest change.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8302
diff
changeset
|
289 | break |
8300 | 290 | self.__clientProcess = proc |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
291 | |
8301
952a05857e81
E5JsonServer: changed code to return the exit code in case of a premature exit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8300
diff
changeset
|
292 | return proc is not None, exitCode |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
293 | |
8300 | 294 | def stopClient(self, idString=""): |
295 | """ | |
296 | Public method to stop a client process. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
297 | |
8300 | 298 | @param idString id of the client to be stopped |
299 | @type str | |
300 | """ | |
301 | self.sendJson("Exit", {}, flush=True, idString=idString) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
302 | |
8300 | 303 | if idString: |
304 | try: | |
305 | connection = self.__connections[idString] | |
306 | except KeyError: | |
307 | connection = None | |
308 | else: | |
309 | connection = self.__connection | |
310 | if connection is not None: | |
311 | connection.waitForDisconnected() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
312 | |
8300 | 313 | if idString: |
314 | with contextlib.suppress(KeyError): | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
315 | if self.__clientProcesses[idString] is not None: |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
316 | self.__clientProcesses[idString].close() |
8300 | 317 | del self.__clientProcesses[idString] |
318 | else: | |
319 | if self.__clientProcess is not None: | |
320 | self.__clientProcess.close() | |
321 | self.__clientProcess = None | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
322 | |
8300 | 323 | def stopAllClients(self): |
324 | """ | |
325 | Public method to stop all clients. | |
326 | """ | |
327 | clientNames = self.connectionNames()[:] | |
328 | for clientName in clientNames: | |
329 | self.stopClient(clientName) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
330 | |
8300 | 331 | ####################################################################### |
332 | ## The following methods should be overridden by derived classes | |
333 | ####################################################################### | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
334 | |
8300 | 335 | def handleCall(self, method, params): |
336 | """ | |
337 | Public method to handle a method call from the client. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
338 | |
8300 | 339 | Note: This is an empty implementation that must be overridden in |
340 | derived classes. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
341 | |
8300 | 342 | @param method requested method name |
343 | @type str | |
344 | @param params dictionary with method specific parameters | |
345 | @type dict | |
346 | """ | |
347 | pass |