eric7/EricNetwork/EricJsonServer.py

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

eric ide

mercurial