eric6/E5Network/E5JsonServer.py

changeset 8300
72ba9635ec5c
child 8301
952a05857e81
equal deleted inserted replaced
8299:58cf45497dc0 8300:72ba9635ec5c
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 PyQt5.QtCore import (
14 pyqtSlot, QProcess, QProcessEnvironment, QCoreApplication, QEventLoop,
15 QTimer
16 )
17 from PyQt5.QtNetwork import QTcpServer, QHostAddress
18
19 from E5Gui import E5MessageBox
20
21 import Preferences
22 import Utilities
23
24
25 class E5JsonServer(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.StandardButtons(
166 E5MessageBox.Ok))
167 return
168
169 self.handleCall(clientDict["method"], clientDict["params"])
170
171 def sendJson(self, command, params, flush=False, idString=""):
172 """
173 Public method to send a single command to a client.
174
175 @param command command name to be sent
176 @type str
177 @param params dictionary of named parameters for the command
178 @type dict
179 @param flush flag indicating to flush the data to the socket
180 @type bool
181 @param idString id of the connection to send data to
182 @type str
183 """
184 commandDict = {
185 "jsonrpc": "2.0",
186 "method": command,
187 "params": params,
188 }
189 cmd = json.dumps(commandDict) + '\n'
190
191 if idString:
192 try:
193 connection = self.__connections[idString]
194 except KeyError:
195 connection = None
196 else:
197 connection = self.__connection
198
199 if connection is not None:
200 data = cmd.encode('utf8', 'backslashreplace')
201 length = "{0:09d}".format(len(data))
202 connection.write(length.encode() + data)
203 if flush:
204 connection.flush()
205
206 def startClient(self, interpreter, clientScript, clientArgs, idString="",
207 environment=None):
208 """
209 Public method to start a client process.
210
211 @param interpreter interpreter to be used for the client
212 @type str
213 @param clientScript path to the client script
214 @type str
215 @param clientArgs list of arguments for the client
216 @param idString id of the client to be started
217 @type str
218 @param environment dictionary of environment settings to pass
219 @type dict
220 @return flag indicating a successful client start
221 @rtype bool
222 """
223 if interpreter == "" or not Utilities.isinpath(interpreter):
224 return False
225
226 proc = QProcess()
227 proc.setProcessChannelMode(
228 QProcess.ProcessChannelMode.ForwardedChannels)
229 if environment is not None:
230 env = QProcessEnvironment()
231 for key, value in list(environment.items()):
232 env.insert(key, value)
233 proc.setProcessEnvironment(env)
234 args = [clientScript, self.__hostAddress, str(self.serverPort())]
235 if idString:
236 args.append(idString)
237 args.extend(clientArgs)
238 proc.start(interpreter, args)
239 if not proc.waitForStarted(10000):
240 proc = None
241
242 if idString:
243 self.__clientProcesses[idString] = proc
244 if proc:
245 timer = QTimer()
246 timer.setSingleShot(True)
247 timer.start(30000) # 30s timeout
248 while (
249 idString not in self.connectionNames() and
250 timer.isActive()
251 ):
252 # Give the event loop the chance to process the new
253 # connection of the client (= slow start).
254 QCoreApplication.processEvents(
255 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents)
256 else:
257 self.__clientProcess = proc
258
259 return proc is not None
260
261 def stopClient(self, idString=""):
262 """
263 Public method to stop a client process.
264
265 @param idString id of the client to be stopped
266 @type str
267 """
268 self.sendJson("Exit", {}, flush=True, idString=idString)
269
270 if idString:
271 try:
272 connection = self.__connections[idString]
273 except KeyError:
274 connection = None
275 else:
276 connection = self.__connection
277 if connection is not None:
278 connection.waitForDisconnected()
279
280 if idString:
281 with contextlib.suppress(KeyError):
282 self .__clientProcesses[idString].close()
283 del self.__clientProcesses[idString]
284 else:
285 if self.__clientProcess is not None:
286 self.__clientProcess.close()
287 self.__clientProcess = None
288
289 def stopAllClients(self):
290 """
291 Public method to stop all clients.
292 """
293 clientNames = self.connectionNames()[:]
294 for clientName in clientNames:
295 self.stopClient(clientName)
296
297 #######################################################################
298 ## The following methods should be overridden by derived classes
299 #######################################################################
300
301 def handleCall(self, method, params):
302 """
303 Public method to handle a method call from the client.
304
305 Note: This is an empty implementation that must be overridden in
306 derived classes.
307
308 @param method requested method name
309 @type str
310 @param params dictionary with method specific parameters
311 @type dict
312 """
313 pass

eric ide

mercurial