32 class EricJsonServer(QTcpServer): |
32 class EricJsonServer(QTcpServer): |
33 """ |
33 """ |
34 Class implementing a JSON based server base class. |
34 Class implementing a JSON based server base class. |
35 """ |
35 """ |
36 |
36 |
37 def __init__(self, name="", interface="127.0.0.1", multiplex=False, parent=None): |
37 def __init__( |
|
38 self, |
|
39 name="", |
|
40 interface="127.0.0.1", |
|
41 multiplex=False, |
|
42 parent=None, |
|
43 releaseMode=False, |
|
44 ): |
38 """ |
45 """ |
39 Constructor |
46 Constructor |
40 |
47 |
41 @param name name of the server (used for output only) (defaults to "") |
48 @param name name of the server (used for output only) (defaults to "") |
42 @type str (optional) |
49 @type str (optional) |
45 @type str (optional) |
52 @type str (optional) |
46 @param multiplex flag indicating a multiplexing server (defaults to False) |
53 @param multiplex flag indicating a multiplexing server (defaults to False) |
47 @type bool (optional) |
54 @type bool (optional) |
48 @param parent reference to the parent object (defaults to None) |
55 @param parent reference to the parent object (defaults to None) |
49 @type QObject (optional) |
56 @type QObject (optional) |
|
57 @param releaseMode flag indicating the mode of operations (defaults to False) |
|
58 @type bool (optional) |
50 """ |
59 """ |
51 super().__init__(parent) |
60 super().__init__(parent) |
52 |
61 |
53 self.__name = name |
62 self.__name = name |
54 self.__multiplex = multiplex |
63 self.__multiplex = multiplex |
70 self.__hostAddress = interface |
79 self.__hostAddress = interface |
71 self.listen(QHostAddress(self.__hostAddress)) |
80 self.listen(QHostAddress(self.__hostAddress)) |
72 |
81 |
73 self.newConnection.connect(self.handleNewConnection) |
82 self.newConnection.connect(self.handleNewConnection) |
74 |
83 |
75 ## Note: Need the address and port if client is started external in debugger. |
84 if not releaseMode: |
76 hostAddressStr = ( |
85 ## Note: Need the address and port if client is started external in |
77 "[{0}]".format(self.__hostAddress) |
86 ## debugger. |
78 if ":" in self.__hostAddress |
87 hostAddressStr = ( |
79 else self.__hostAddress |
88 "[{0}]".format(self.__hostAddress) |
80 ) |
89 if ":" in self.__hostAddress |
81 print( # __IGNORE_WARNING_M-801__ |
90 else self.__hostAddress |
82 "JSON server ({2}) listening on: {0}:{1:d}".format( |
|
83 hostAddressStr, self.serverPort(), self.__name |
|
84 ) |
91 ) |
85 ) |
92 print( # __IGNORE_WARNING_M-801__ |
|
93 f"JSON server ({self.__name}) listening on:" |
|
94 f" {hostAddressStr}:{self.serverPort():d}" |
|
95 ) |
86 |
96 |
87 @pyqtSlot() |
97 @pyqtSlot() |
88 def handleNewConnection(self): |
98 def handleNewConnection(self): |
89 """ |
99 """ |
90 Public slot for new incoming connections from a client. |
100 Public slot for new incoming connections from a client. |
188 # corrupted data -> discard and continue |
198 # corrupted data -> discard and continue |
189 continue |
199 continue |
190 |
200 |
191 jsonString = data.decode("utf-8", "backslashreplace") |
201 jsonString = data.decode("utf-8", "backslashreplace") |
192 |
202 |
193 # - print("JSON Server ({0}): {1}".format(self.__name, jsonString)) |
203 # - print(f"JSON Server ({self.__name}): {jsonString}") |
194 # - this is for debugging only |
204 # - this is for debugging only |
195 |
205 |
196 try: |
206 try: |
197 clientDict = json.loads(jsonString.strip()) |
207 clientDict = json.loads(jsonString.strip()) |
198 except (TypeError, ValueError) as err: |
208 except (TypeError, ValueError) as err: |