128 @return flag indicating a successful transmission |
128 @return flag indicating a successful transmission |
129 @rtype bool |
129 @rtype bool |
130 """ |
130 """ |
131 if isinstance(jsonCommand, dict): |
131 if isinstance(jsonCommand, dict): |
132 jsonCommand = json.dumps(jsonCommand) |
132 jsonCommand = json.dumps(jsonCommand) |
133 print("Eric Server Send:", jsonCommand) |
133 # - print("Eric Server Send:", jsonCommand) # for debugging |
134 |
134 |
135 data = jsonCommand.encode("utf8", "backslashreplace") |
135 data = jsonCommand.encode("utf8", "backslashreplace") |
136 header = struct.pack(b"!II", len(data), zlib.adler32(data) & 0xFFFFFFFF) |
136 header = struct.pack(b"!II", len(data), zlib.adler32(data) & 0xFFFFFFFF) |
137 try: |
137 try: |
138 sock.sendall(header) |
138 sock.sendall(header) |
202 }, |
201 }, |
203 ) |
202 ) |
204 return {} |
203 return {} |
205 |
204 |
206 jsonStr = data.decode("utf8", "backslashreplace") |
205 jsonStr = data.decode("utf8", "backslashreplace") |
207 print("Eric Server Receive:", jsonStr) |
206 # - print("Eric Server Receive:", jsonStr) # for debugging |
208 try: |
207 try: |
209 return json.loads(jsonStr.strip()) |
208 return json.loads(jsonStr.strip()) |
210 except (TypeError, ValueError) as err: |
209 except (TypeError, ValueError) as err: |
211 self.sendJson( |
210 self.sendJson( |
212 category=EricRequestCategory.Error, |
211 category=EricRequestCategory.Error, |
281 ) |
280 ) |
282 |
281 |
283 self.__socket.listen(0) |
282 self.__socket.listen(0) |
284 self.__socket.setblocking(False) |
283 self.__socket.setblocking(False) |
285 print(f"Listening for 'eric-ide' connections on {self.__socket.getsockname()}") |
284 print(f"Listening for 'eric-ide' connections on {self.__socket.getsockname()}") |
|
285 # noqa: M801 |
286 data = types.SimpleNamespace( |
286 data = types.SimpleNamespace( |
287 name="server", acceptHandler=self.__acceptIdeConnection |
287 name="server", acceptHandler=self.__acceptIdeConnection |
288 ) |
288 ) |
289 self.__selector.register(self.__socket, selectors.EVENT_READ, data=data) |
289 self.__selector.register(self.__socket, selectors.EVENT_READ, data=data) |
290 |
290 |
302 """ |
302 """ |
303 Private method to shut down the server. |
303 Private method to shut down the server. |
304 """ |
304 """ |
305 self.__closeIdeConnection(shutdown=True) |
305 self.__closeIdeConnection(shutdown=True) |
306 |
306 |
307 print("Stop listening for 'eric-ide' connections.") |
307 print("Stop listening for 'eric-ide' connections.") # noqa: M801 |
308 if self.__socket is not None: |
308 if self.__socket is not None: |
309 self.__socket.shutdown(socket.SHUT_RDWR) |
309 self.__socket.shutdown(socket.SHUT_RDWR) |
310 self.__socket.close() |
310 self.__socket.close() |
311 |
311 |
312 self.__selector.close() |
312 self.__selector.close() |
319 @type socket.socket |
319 @type socket.socket |
320 """ |
320 """ |
321 connection, address = sock.accept() # Should be ready to read |
321 connection, address = sock.accept() # Should be ready to read |
322 if self.__connection is None: |
322 if self.__connection is None: |
323 print(f"'eric-ide' connection from {address[0]}, port {address[1]}") |
323 print(f"'eric-ide' connection from {address[0]}, port {address[1]}") |
|
324 # noqa: M801 |
324 self.__connection = connection |
325 self.__connection = connection |
325 self.__connection.setblocking(False) |
326 self.__connection.setblocking(False) |
326 data = types.SimpleNamespace( |
327 data = types.SimpleNamespace( |
327 name="eric-ide", address=address, handler=self.__serviceIdeConnection |
328 name="eric-ide", address=address, handler=self.__serviceIdeConnection |
328 ) |
329 ) |
329 events = selectors.EVENT_READ |
330 events = selectors.EVENT_READ |
330 self.__selector.register(self.__connection, events, data=data) |
331 self.__selector.register(self.__connection, events, data=data) |
331 |
332 |
332 self.__unregisterIdeSocket() |
333 self.__unregisterIdeSocket() |
333 else: |
334 else: |
334 print( |
335 print( # noqa: M801 |
335 f"'eric-ide' connection from {address[0]}, port {address[1]} rejected" |
336 f"'eric-ide' connection from {address[0]}, port {address[1]} rejected" |
336 ) |
337 ) |
337 connection.close() |
338 connection.close() |
338 |
339 |
339 def __closeIdeConnection(self, shutdown=False): |
340 def __closeIdeConnection(self, shutdown=False): |
344 @type bool |
345 @type bool |
345 """ |
346 """ |
346 if self.__connection is not None: |
347 if self.__connection is not None: |
347 self.__selector.unregister(self.__connection) |
348 self.__selector.unregister(self.__connection) |
348 try: |
349 try: |
349 print( |
350 print( # noqa: M801 |
350 f"Closing 'eric-ide' connection to" |
351 f"Closing 'eric-ide' connection to" |
351 f" {self.__connection.getpeername()}." |
352 f" {self.__connection.getpeername()}." |
352 ) |
353 ) |
353 self.__connection.shutdown(socket.SHUT_RDWR) |
354 self.__connection.shutdown(socket.SHUT_RDWR) |
354 self.__connection.close() |
355 self.__connection.close() |
355 except OSError: |
356 except OSError: |
356 print("'eric-ide' connection gone.") |
357 print("'eric-ide' connection gone.") # noqa: M801 |
357 self.__connection = None |
358 self.__connection = None |
358 |
359 |
359 self.__debuggerRequestHandler.shutdownClients() |
360 self.__debuggerRequestHandler.shutdownClients() |
360 |
361 |
361 if not shutdown: |
362 if not shutdown: |
423 tbinfofile = io.StringIO() |
424 tbinfofile = io.StringIO() |
424 traceback.print_tb(exctb, None, tbinfofile) |
425 traceback.print_tb(exctb, None, tbinfofile) |
425 tbinfofile.seek(0) |
426 tbinfofile.seek(0) |
426 tbinfo = tbinfofile.read() |
427 tbinfo = tbinfofile.read() |
427 |
428 |
428 print(f"{str(exctype)} / {str(excval)} / {tbinfo}") |
429 print("Stopping due to an exception.\nDetails:") # noqa: M801 |
|
430 print(f"{str(exctype)} / {str(excval)} / {tbinfo}") # noqa: M801 |
429 |
431 |
430 self.__shouldStop = True |
432 self.__shouldStop = True |
431 cleanExit = False |
433 cleanExit = False |
432 |
434 |
433 if self.__shouldStop: |
435 if self.__shouldStop: |