Exemple #1
0
    async def __send_and_wait_internal(self, id, worker_instance, message, timeout, byte_command: int = None):
        async with self.__users_mutex:
            user_entry = self.__current_users.get(id, None)

        if user_entry is None or user_entry[1] != worker_instance and worker_instance != 'madmin':
            raise WebsocketWorkerRemovedException

        message_id = await self.__get_new_message_id()
        message_event = asyncio.Event()
        message_event.clear()

        await self.__set_request(message_id, message_event)

        if isinstance(message, str):
            to_be_sent: str = u"%s;%s" % (str(message_id), message)
            logger.debug("To be sent to {}: {}", id, to_be_sent.strip())
        elif byte_command is not None:
            to_be_sent: bytes = (int(message_id)).to_bytes(4, byteorder='big')
            to_be_sent += (int(byte_command)).to_bytes(4, byteorder='big')
            to_be_sent += message
            logger.debug("To be sent to {} (message ID: {}): {}", id, message_id, str(to_be_sent[:10]))
        else:
            logger.fatal("Tried to send invalid message (bytes without byte command or no byte/str passed)")
            return None
        await self.__send(id, to_be_sent)

        # now wait for the response!
        result = None
        logger.debug("Timeout: {}", str(timeout))
        event_triggered = None
        try:
            event_triggered = await asyncio.wait_for(message_event.wait(), timeout=timeout)
        except asyncio.TimeoutError as te:
            logger.warning("Timeout, increasing timeout-counter")
            # TODO: why is the user removed here?
            new_count = await self.__increase_fail_counter(id)
            if new_count > 5:
                logger.error("5 consecutive timeouts to {} or origin is not longer connected, cleanup", str(id))
                await self.__internal_clean_up_user(id, None)
                await self.__reset_fail_counter(id)
                await self.__remove_request(message_id)
                raise WebsocketWorkerTimeoutException

        if event_triggered:
            logger.debug("Received answer in time, popping response")
            await self.__reset_fail_counter(id)
            result = await self.__pop_response(message_id)
            if isinstance(result, str):
                logger.debug("Response to {}: {}",
                             str(id), str(result.strip()))
            else:
                logger.debug("Received binary data to {}, starting with {}", str(
                        id), str(result[:10]))
        return result
Exemple #2
0
 def shutdown(self):
     logger.fatal("MappingManager exiting")
     self.__stop_file_watcher_event.set()
     self.__t_file_watcher.join()
     self.__devicesettings_setter_consumer_thread.join()