Skip to content

Commit

Permalink
ADD: on_* methods to allow easy use of both inheritance and composition
Browse files Browse the repository at this point in the history
  • Loading branch information
desty2k committed Jun 14, 2021
1 parent 0dc7cc9 commit 83c52a5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
36 changes: 30 additions & 6 deletions QtPyNetwork/client/QThreadedClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,39 @@ def start(self, ip: str, port: int):

self.__client.moveToThread(self.__client_thread)

self.__client.connected.connect(self.connected.emit)
self.__client.failed_to_connect.connect(self.failed_to_connect.emit)
self.__client.message.connect(self.message.emit)
self.__client.disconnected.connect(self.disconnected.emit)
self.__client.error.connect(self.error.emit)
self.__client.closed.connect(self.closed.emit)
self.__client.connected.connect(self.on_connected)
self.__client.failed_to_connect.connect(self.on_failed_to_connect)
self.__client.message.connect(self.on_message)
self.__client.disconnected.connect(self.on_disconnected)
self.__client.error.connect(self.on_error)
self.__client.closed.connect(self.on_closed)

self.__client_thread.start()

@Slot(str, int)
def on_connected(self, ip, port):
self.connected.emit(ip, port)

@Slot(str, int)
def on_failed_to_connect(self, ip, port):
self.failed_to_connect.emit(ip, port)

@Slot(bytes)
def on_message(self, message: bytes):
self.message.emit(message)

@Slot()
def on_disconnected(self):
self.disconnected.emit()

@Slot(str)
def on_error(self, error: str):
self.error.emit(error)

@Slot()
def on_closed(self):
self.closed.emit()

@Slot(bytes)
def write(self, data: bytes):
"""Write data to server.
Expand Down
37 changes: 28 additions & 9 deletions QtPyNetwork/server/BaseServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def start(self, ip: str, port: int):
self.__handler_thread = QThread()
self.__handler.moveToThread(self.__handler_thread)

self.__handler.connected.connect(self.on_successful_connection)
self.__handler.message.connect(self.on_message)
self.__handler.error.connect(self.error.emit)
self.__handler.disconnected.connect(self.on_device_disconnected)
self.__handler.connected.connect(self.__on_handler_successful_connection)
self.__handler.message.connect(self.__on_handler_device_message)
self.__handler.error.connect(self.__on_handler_device_error)
self.__handler.disconnected.connect(self.__on_handler_device_disconnected)
self.__handler.closed.connect(self.on_closed)

self.__handler_thread.started.connect(self.__handler.start)
Expand All @@ -83,29 +83,48 @@ def __setup_server(self):
self.server_error.emit(e)

@Slot(int, str, int)
def on_successful_connection(self, device_id, ip, port):
def __on_handler_successful_connection(self, device_id, ip, port):
"""When client connects to server successfully."""
device = self.__deviceModel(device_id, ip, port)
device._write.connect(lambda data: self.write(device, data))
device._kick.connect(lambda: self.kick(device))
self.__devices.append(device)
self.connected.emit(device, ip, port)
self.__logger.info("Added new CLIENT-{} with address {}:{}".format(device_id, ip, port))
self.on_connected(device, ip, port)

@Slot(int, bytes)
def on_message(self, device_id: int, message: bytes):
def __on_handler_device_message(self, device_id: int, message: bytes):
"""When server receives message from bot."""
self.message.emit(self.get_device_by_id(device_id), message)
self.on_message(self.get_device_by_id(device_id), message)

@Slot(int)
def on_device_disconnected(self, device_id):
def __on_handler_device_disconnected(self, device_id):
"""When bot disconnects from server."""
device = self.get_device_by_id(device_id)
device.set_connected(False)
if device in self.__devices:
self.__devices.remove(device)
self.on_disconnected(device)

@Slot(int, str)
def __on_handler_device_error(self, device_id, error):
self.on_error(self.get_device_by_id(device_id), error)

@Slot(Device, str, int)
def on_connected(self, device: Device, ip: str, port: int):
self.connected.emit(device, ip, port)

@Slot(Device, bytes)
def on_message(self, device: Device, message: bytes):
self.message.emit(device, message)

@Slot(Device)
def on_disconnected(self, device: Device):
self.disconnected.emit(device)

@Slot(Device, str)
def on_error(self, device: Device, error: str):
self.error.emit(device, error)

@Slot()
def on_closed(self):
Expand Down

0 comments on commit 83c52a5

Please sign in to comment.