From b588ca104d3fe9dd3bbaf986e51f22d6366cf96b Mon Sep 17 00:00:00 2001 From: Jourdelune <64205064+Jourdelune@users.noreply.github.com> Date: Tue, 22 Aug 2023 18:09:47 +0200 Subject: [PATCH 1/7] Update client.py --- winerp/client.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/winerp/client.py b/winerp/client.py index 6be7e73..1cf4aa7 100644 --- a/winerp/client.py +++ b/winerp/client.py @@ -384,6 +384,40 @@ async def request( else: raise ClientNotReadyError("The client has not been started or has disconnected") + async def get_clients(self, timeout: int = 60) -> list: + """|coro| + + Gets all the connected clients. + + Raises + ------- + ClientNotReadyError + The client is currently not ready to send or accept requests. + UnauthorizedError + The client isn't authorized by the server. + + Returns + -------- + :class:`list` + A list of connected clients. + """ + if self._on_hold or self.websocket is None or not self.websocket.open: + raise ClientNotReadyError("The client is currently not ready to send or accept requests.") + if not self._authorized: + raise UnauthorizedError("Client is not authorized!") + + logger.debug("Getting all connected clients") + _uuid = str(uuid.uuid4()) + payload = MessagePayload( + type=Payloads.question, + id=self.local_name, + uuid=_uuid + ) + await self.send_message(payload) + resp = await self.__get_response(_uuid, asyncio.get_event_loop(), timeout=timeout) + return resp + + async def inform( self, data: Any, @@ -558,6 +592,10 @@ async def __on_message(self): logger.debug("Received an information bit from client: %s", message.id) self.__events.dispatch_event('winerp_information', message.data, message.id) + elif message.type.question: + logger.debug("Received the response of the qusetion from server") + asyncio.create_task(self._dispatch(message)) + elif message.type.function_call: logger.debug("Received an object function call.") logger.debug(message.data) From eb8f82a24f10d06b8864e72b59298ad57bc1d4e4 Mon Sep 17 00:00:00 2001 From: Jourdelune <64205064+Jourdelune@users.noreply.github.com> Date: Tue, 22 Aug 2023 18:10:09 +0200 Subject: [PATCH 2/7] Update server.py --- winerp/server.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/winerp/server.py b/winerp/server.py index 00cb4b2..37a4cb9 100644 --- a/winerp/server.py +++ b/winerp/server.py @@ -103,6 +103,7 @@ def __send_error(self, client, payload): def __on_message(self, client, _, msg): msg = WsMessage(orjson.loads(msg)) payload = MessagePayload().from_message(msg) + if msg.type.verification: if msg.id in self.active_clients: logger.info("Connection from duplicate client has benn put on hold connection id %s and local id %s" % (client['address'][1], msg.id)) @@ -180,6 +181,17 @@ def __on_message(self, client, _, msg): ) logger.debug("Request Message Forwarded to %s" % self.active_clients[msg.destination]["client"]['address'][1]) + if msg.type.question: + logger.debug("Received Question Message from client %s" % client['address'][1]) + payload.type = Payloads.question + + payload.data = list(self.active_clients.keys()) + + self.__send_message( + client, + payload + ) + if msg.type.response or msg.type.error or msg.type.function_call: logger.debug("Received Response Message from client %s" % client['address'][1]) if msg.destination not in self.active_clients: From cbd8f27b65d2f394058cd9e7072f38cc15c7962c Mon Sep 17 00:00:00 2001 From: Jourdelune <64205064+Jourdelune@users.noreply.github.com> Date: Tue, 22 Aug 2023 18:10:35 +0200 Subject: [PATCH 3/7] Update payload.py --- winerp/lib/payload.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/winerp/lib/payload.py b/winerp/lib/payload.py index a9b10ca..c1fe930 100644 --- a/winerp/lib/payload.py +++ b/winerp/lib/payload.py @@ -14,7 +14,8 @@ class Payloads: error = 4 ping = 5 information = 6 - function_call = 7 + question = 7 + function_call = 8 class PayloadTypes: ''' @@ -25,6 +26,7 @@ class PayloadTypes: | ``response``: Response to a request. | ``error``: Error response. | ``ping``: Ping message. + | ``question``: Question message. ''' def __init__(self, type: int) -> None: self._type = type @@ -81,10 +83,17 @@ def information(self) -> bool: ''' return self._type == Payloads.information + @property + def question(self) -> bool: + ''' + :class:`bool`: Returns ``True`` if the message is a question message. + ''' + return self._type == Payloads.question + @property def function_call(self) -> bool: ''' - :class:`bool`: Returns ``True`` if the message is an information message. + :class:`bool`: Returns ``True`` if the message is a function call message. ''' return self._type == Payloads.function_call From f6aa896f157a177b8d9cccf6c1c8adc9349ea8c5 Mon Sep 17 00:00:00 2001 From: Jourdelune <64205064+Jourdelune@users.noreply.github.com> Date: Fri, 1 Sep 2023 16:56:42 +0200 Subject: [PATCH 4/7] Update payload.py --- winerp/lib/payload.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/winerp/lib/payload.py b/winerp/lib/payload.py index c1fe930..3b8793d 100644 --- a/winerp/lib/payload.py +++ b/winerp/lib/payload.py @@ -14,8 +14,9 @@ class Payloads: error = 4 ping = 5 information = 6 - question = 7 - function_call = 8 + function_call = 7 + client_count = 9 + class PayloadTypes: ''' @@ -84,11 +85,11 @@ def information(self) -> bool: return self._type == Payloads.information @property - def question(self) -> bool: + def client_count(self) -> bool: ''' - :class:`bool`: Returns ``True`` if the message is a question message. + :class:`bool`: Returns ``True`` if the message is a client_count message. ''' - return self._type == Payloads.question + return self._type == Payloads.client_count @property def function_call(self) -> bool: From 3bbe91a758178e283fb1a4fccea797ac6d27e6d5 Mon Sep 17 00:00:00 2001 From: Jourdelune <64205064+Jourdelune@users.noreply.github.com> Date: Fri, 1 Sep 2023 16:57:31 +0200 Subject: [PATCH 5/7] Update server.py --- winerp/server.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/winerp/server.py b/winerp/server.py index 37a4cb9..761f004 100644 --- a/winerp/server.py +++ b/winerp/server.py @@ -181,9 +181,9 @@ def __on_message(self, client, _, msg): ) logger.debug("Request Message Forwarded to %s" % self.active_clients[msg.destination]["client"]['address'][1]) - if msg.type.question: - logger.debug("Received Question Message from client %s" % client['address'][1]) - payload.type = Payloads.question + if msg.type.client_count: + logger.debug("Received Client_count Message from client %s" % client['address'][1]) + payload.type = Payloads.client_count payload.data = list(self.active_clients.keys()) From 3efdeb0c9ebed41562ee803efeef9ba1305de772 Mon Sep 17 00:00:00 2001 From: Jourdelune <64205064+Jourdelune@users.noreply.github.com> Date: Fri, 1 Sep 2023 16:58:59 +0200 Subject: [PATCH 6/7] Update client.py --- winerp/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/winerp/client.py b/winerp/client.py index 1cf4aa7..421021b 100644 --- a/winerp/client.py +++ b/winerp/client.py @@ -592,8 +592,8 @@ async def __on_message(self): logger.debug("Received an information bit from client: %s", message.id) self.__events.dispatch_event('winerp_information', message.data, message.id) - elif message.type.question: - logger.debug("Received the response of the qusetion from server") + elif message.type.client_count: + logger.debug("Received the response of the client_count from server") asyncio.create_task(self._dispatch(message)) elif message.type.function_call: From 8408d0886d978783457915f5e52ee05c57c9a598 Mon Sep 17 00:00:00 2001 From: Jourdelune <64205064+Jourdelune@users.noreply.github.com> Date: Fri, 1 Sep 2023 17:00:59 +0200 Subject: [PATCH 7/7] Update client.py --- winerp/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/winerp/client.py b/winerp/client.py index 421021b..4bb4ef7 100644 --- a/winerp/client.py +++ b/winerp/client.py @@ -409,7 +409,7 @@ async def get_clients(self, timeout: int = 60) -> list: logger.debug("Getting all connected clients") _uuid = str(uuid.uuid4()) payload = MessagePayload( - type=Payloads.question, + type=Payloads.client_count, id=self.local_name, uuid=_uuid )