Пример #1
0
 async def __on_message(client_entry: WebsocketConnectedClientEntry, message: MessageTyping) -> None:
     response: Optional[MessageTyping] = None
     if isinstance(message, str):
         client_entry.logger.debug5("Receiving message: {}", message.strip())
         splitup = message.split(";", 1)
         message_id = int(splitup[0])
         response = splitup[1]
     else:
         logger.debug("Received binary values.")
         message_id = int.from_bytes(message[:4], byteorder='big', signed=False)
         response = message[4:]
     await client_entry.set_message_response(message_id, response)
Пример #2
0
    async def send_and_wait_async(
            self,
            message: MessageTyping,
            timeout: float,
            worker_instance: AbstractWorker,
            byte_command: Optional[int] = None) -> Optional[MessageTyping]:
        if self.worker_instance is None or self.worker_instance != worker_instance and worker_instance != 'madmin':
            # TODO: consider changing this...
            raise WebsocketWorkerRemovedException
        elif not self.websocket_client_connection.open:
            raise WebsocketWorkerConnectionClosedException

        # install new ReceivedMessageEntry
        message_id: int = await self.__get_new_message_id()
        new_entry = ReceivedMessageEntry()
        async with self.received_mutex:
            self.received_messages[message_id] = new_entry

        if isinstance(message, bytes):
            self.logger.debug("sending binary: {}", message[:10])
        else:
            self.logger.debug("sending command: {}", message.strip())
        # send message
        await self.__send_message(message_id, message, byte_command)

        # wait for it to trigger...
        self.logger.debug2("Timeout towards: {}", timeout)
        response = None
        try:
            event_triggered = await asyncio.wait_for(
                new_entry.message_received_event.wait(), timeout=timeout)
            if event_triggered:
                self.logger.debug("Received answer in time, popping response")
                self.fail_counter = 0
                if isinstance(new_entry.message, str):
                    self.logger.debug4("Response: {}",
                                       new_entry.message.strip())
                else:
                    self.logger.debug4(
                        "Received binary data , starting with {}",
                        new_entry.message[:10])
                response = new_entry.message
        except asyncio.TimeoutError:
            self.logger.warning("Timeout, increasing timeout-counter")
            self.fail_counter += 1
            if self.fail_counter > 5:
                self.logger.error(
                    "5 consecutive timeouts or origin is no longer connected, cleanup"
                )
                raise WebsocketWorkerTimeoutException
        finally:
            self.logger.debug2("Cleaning up received messaged.")
            async with self.received_mutex:
                self.received_messages.pop(message_id)
        self.logger.debug("Done sending command")
        return response
    async def send_and_wait_async(self, message: MessageTyping, timeout: float, worker_instance: AbstractWorker,
                                  byte_command: Optional[int] = None) -> Optional[MessageTyping]:
        if self.worker_instance is None or self.worker_instance != worker_instance and worker_instance != 'madmin':
            # TODO: consider changing this...
            raise WebsocketWorkerRemovedException
        elif self.websocket_client_connection.closed:
            raise WebsocketWorkerConnectionClosedException

        # install new ReceivedMessageEntry
        message_id: int = await self.__get_new_message_id()
        new_entry = ReceivedMessageEntry()
        async with self.received_mutex:
            self.received_messages[message_id] = new_entry

        if isinstance(message, bytes):
            logger.debug("{} sending binary: {}", self.origin, str(message[:10]))
        else:
            logger.debug("{} sending command: {}", self.origin, message.strip())
        # send message
        await self.__send_message(message_id, message, byte_command)

        # wait for it to trigger...
        logger.debug("Timeout towards {}: {}", self.origin, str(timeout))
        response = None
        try:
            event_triggered = await asyncio.wait_for(new_entry.message_received_event.wait(), timeout=timeout)
            if event_triggered:
                logger.debug("Received answer from {} in time, popping response", self.origin)
                self.fail_counter = 0
                if isinstance(new_entry.message, str):
                    logger.debug("Response to {}: {}",
                                 str(id), str(new_entry.message.strip()))
                else:
                    logger.debug("Received binary data to {}, starting with {}", str(
                        id), str(new_entry.message[:10]))
                response = new_entry.message
        except asyncio.TimeoutError:
            logger.warning("Timeout, increasing timeout-counter of {}", self.origin)
            # TODO: why is the user removed here?
            self.fail_counter += 1
            if self.fail_counter > 5:
                logger.error("5 consecutive timeouts to {} or origin is not longer connected, cleanup",
                             str(id))
                raise WebsocketWorkerTimeoutException
        finally:
            async with self.received_mutex:
                self.received_messages.pop(message_id)
        return response