194 try: |
194 try: |
195 msgClientId = str(msgClientIdBytes, encoding="utf-8") |
195 msgClientId = str(msgClientIdBytes, encoding="utf-8") |
196 except UnicodeDecodeError: |
196 except UnicodeDecodeError: |
197 msgClientId = str(bytes(msgClientIdBytes)) |
197 msgClientId = str(bytes(msgClientIdBytes)) |
198 if msgClientId != self.__clientId: |
198 if msgClientId != self.__clientId: |
199 print(f"Received illegal client ID '{msgClientId}'.") # noqa: M801 |
199 print(f"Received illegal client ID '{msgClientId}'.") # noqa: M-801 |
200 return {} |
200 return {} |
201 |
201 |
202 header = self.__receiveBytes(struct.calcsize(b"!II"), sock) |
202 header = self.__receiveBytes(struct.calcsize(b"!II"), sock) |
203 if not header: |
203 if not header: |
204 return {} |
204 return {} |
221 }, |
221 }, |
222 ) |
222 ) |
223 return {} |
223 return {} |
224 |
224 |
225 jsonStr = data.decode("utf8", "backslashreplace") |
225 jsonStr = data.decode("utf8", "backslashreplace") |
226 # - print("Eric Server Receive:", jsonStr) # for debugging # noqa: M801 |
226 # - print("Eric Server Receive:", jsonStr) # for debugging # noqa: M-801 |
227 try: |
227 try: |
228 return json.loads(jsonStr.strip()) |
228 return json.loads(jsonStr.strip()) |
229 except (TypeError, ValueError) as err: |
229 except (TypeError, ValueError) as err: |
230 self.sendJson( |
230 self.sendJson( |
231 category=EricRequestCategory.Error, |
231 category=EricRequestCategory.Error, |
300 ) |
300 ) |
301 |
301 |
302 self.__socket.listen(0) |
302 self.__socket.listen(0) |
303 self.__socket.setblocking(False) |
303 self.__socket.setblocking(False) |
304 address = self.__socket.getsockname() |
304 address = self.__socket.getsockname() |
305 print( # noqa: M801 |
305 print( # noqa: M-801 |
306 f"Listening for 'eric-ide' connections on {address[0]}, port {address[1]}" |
306 f"Listening for 'eric-ide' connections on {address[0]}, port {address[1]}" |
307 ) |
307 ) |
308 data = types.SimpleNamespace( |
308 data = types.SimpleNamespace( |
309 name="server", acceptHandler=self.__acceptIdeConnection |
309 name="server", acceptHandler=self.__acceptIdeConnection |
310 ) |
310 ) |
324 """ |
324 """ |
325 Private method to shut down the server. |
325 Private method to shut down the server. |
326 """ |
326 """ |
327 self.__closeIdeConnection(shutdown=True) |
327 self.__closeIdeConnection(shutdown=True) |
328 |
328 |
329 print("Stop listening for 'eric-ide' connections.") # noqa: M801 |
329 print("Stop listening for 'eric-ide' connections.") # noqa: M-801 |
330 if self.__socket is not None: |
330 if self.__socket is not None: |
331 self.__socket.shutdown(socket.SHUT_RDWR) |
331 self.__socket.shutdown(socket.SHUT_RDWR) |
332 self.__socket.close() |
332 self.__socket.close() |
333 |
333 |
334 self.__selector.close() |
334 self.__selector.close() |
341 @type socket.socket |
341 @type socket.socket |
342 """ |
342 """ |
343 connection, address = sock.accept() # Should be ready to read. |
343 connection, address = sock.accept() # Should be ready to read. |
344 if self.__connection is None: |
344 if self.__connection is None: |
345 print(f"'eric-ide' connection from {address[0]}, port {address[1]}") |
345 print(f"'eric-ide' connection from {address[0]}, port {address[1]}") |
346 # noqa: M801 |
346 # noqa: M-801 |
347 self.__connection = connection |
347 self.__connection = connection |
348 self.__connection.settimeout(10) |
348 self.__connection.settimeout(10) |
349 data = types.SimpleNamespace( |
349 data = types.SimpleNamespace( |
350 name="eric-ide", address=address, handler=self.__serviceIdeConnection |
350 name="eric-ide", address=address, handler=self.__serviceIdeConnection |
351 ) |
351 ) |
352 events = selectors.EVENT_READ |
352 events = selectors.EVENT_READ |
353 self.__selector.register(self.__connection, events, data=data) |
353 self.__selector.register(self.__connection, events, data=data) |
354 |
354 |
355 self.__unregisterIdeSocket() |
355 self.__unregisterIdeSocket() |
356 else: |
356 else: |
357 print( # noqa: M801 |
357 print( # noqa: M-801 |
358 f"'eric-ide' connection from {address[0]}, port {address[1]} rejected" |
358 f"'eric-ide' connection from {address[0]}, port {address[1]} rejected" |
359 ) |
359 ) |
360 connection.close() |
360 connection.close() |
361 |
361 |
362 def __closeIdeConnection(self, shutdown=False): |
362 def __closeIdeConnection(self, shutdown=False): |
368 """ |
368 """ |
369 if self.__connection is not None: |
369 if self.__connection is not None: |
370 self.__selector.unregister(self.__connection) |
370 self.__selector.unregister(self.__connection) |
371 try: |
371 try: |
372 address = self.__connection.getpeername() |
372 address = self.__connection.getpeername() |
373 print( # noqa: M801 |
373 print( # noqa: M-801 |
374 f"Closing 'eric-ide' connection to {address[0]}, port {address[1]}." |
374 f"Closing 'eric-ide' connection to {address[0]}, port {address[1]}." |
375 ) |
375 ) |
376 self.__connection.shutdown(socket.SHUT_RDWR) |
376 self.__connection.shutdown(socket.SHUT_RDWR) |
377 self.__connection.close() |
377 self.__connection.close() |
378 except OSError: |
378 except OSError: |
379 print("'eric-ide' connection gone.") # noqa: M801 |
379 print("'eric-ide' connection gone.") # noqa: M-801 |
380 self.__connection = None |
380 self.__connection = None |
381 |
381 |
382 self.__debuggerRequestHandler.shutdownClients() |
382 self.__debuggerRequestHandler.shutdownClients() |
383 |
383 |
384 if not shutdown: |
384 if not shutdown: |
446 tbinfofile = io.StringIO() |
446 tbinfofile = io.StringIO() |
447 traceback.print_tb(exctb, None, tbinfofile) |
447 traceback.print_tb(exctb, None, tbinfofile) |
448 tbinfofile.seek(0) |
448 tbinfofile.seek(0) |
449 tbinfo = tbinfofile.read() |
449 tbinfo = tbinfofile.read() |
450 |
450 |
451 print("Stopping due to an exception.\nDetails:") # noqa: M801 |
451 print("Stopping due to an exception.\nDetails:") # noqa: M-801 |
452 print(f"{str(exctype)} / {str(excval)} / {tbinfo}") # noqa: M801 |
452 print(f"{str(exctype)} / {str(excval)} / {tbinfo}") # noqa: M-801 |
453 |
453 |
454 self.__shouldStop = True |
454 self.__shouldStop = True |
455 cleanExit = False |
455 cleanExit = False |
456 |
456 |
457 if self.__shouldStop: |
457 if self.__shouldStop: |
537 category=EricRequestCategory.Error, |
537 category=EricRequestCategory.Error, |
538 reply="UnsupportedServiceCategory", |
538 reply="UnsupportedServiceCategory", |
539 params={"Category": category}, |
539 params={"Category": category}, |
540 ) |
540 ) |
541 |
541 |
542 def __handleEchoRequest(self, request, params, reqestUuid): # noqa: U100 |
542 def __handleEchoRequest(self, request, params, reqestUuid): # noqa: U-100 |
543 """ |
543 """ |
544 Private method to handle an 'Echo' request. |
544 Private method to handle an 'Echo' request. |
545 |
545 |
546 @param request request name |
546 @param request request name |
547 @type str |
547 @type str |
556 reply="Echo", |
556 reply="Echo", |
557 params=params, |
557 params=params, |
558 reqestUuid=reqestUuid, |
558 reqestUuid=reqestUuid, |
559 ) |
559 ) |
560 |
560 |
561 def __handleServerRequest(self, request, params, reqestUuid): # noqa: U100 |
561 def __handleServerRequest(self, request, params, reqestUuid): # noqa: U-100 |
562 """ |
562 """ |
563 Private method to handle a 'Server' request. |
563 Private method to handle a 'Server' request. |
564 |
564 |
565 @param request request name |
565 @param request request name |
566 @type str |
566 @type str |