Beispiel #1
0
    def handle_agent_message(self, envelope: Envelope) -> None:
        """
        Dispatch the TACMessage to the right handler.

        If no handler is found for the provided type of TACMessage, return an "invalid TACMessage" error.
        If something bad happen, return a "generic" error.

        :param envelope: the envelope to handle
        :return: None
        """
        assert envelope.protocol_id == "tac"
        tac_msg = TACSerializer().decode(envelope.message)
        logger.debug(
            "[{}] on_message: origin={}".format(
                self.controller_agent.name, envelope.sender
            )
        )
        tac_msg_type = tac_msg.get("type")
        handle_tac_message = self.handlers.get(
            TACMessage.Type(tac_msg_type), None
        )  # type: Optional[TACMessageHandler]
        if handle_tac_message is None:
            logger.debug(
                "[{}]: Unknown message from {}".format(
                    self.controller_agent.name, envelope.sender
                )
            )
            tac_error = TACMessage(
                tac_type=TACMessage.Type.TAC_ERROR,
                error_code=TACMessage.ErrorCode.REQUEST_NOT_VALID.value,
            )
            tac_bytes = TACSerializer().encode(tac_error)
            self.controller_agent.mailbox.outbox.put_message(
                to=envelope.sender,
                sender=self.controller_agent.crypto.public_key,
                protocol_id=tac_error.protocol_id,
                message=tac_bytes,
            )
            return
        else:
            try:
                handle_tac_message(tac_msg, envelope.sender)
            except Exception as e:
                logger.debug(
                    "[{}]: Error caught: {}".format(self.controller_agent.name, str(e))
                )
                logger.exception(e)
                tac_error = TACMessage(
                    tac_type=TACMessage.Type.TAC_ERROR,
                    error_code=TACMessage.ErrorCode.GENERIC_ERROR.value,
                )
                tac_bytes = TACSerializer().encode(tac_error)
                self.controller_agent.mailbox.outbox.put_message(
                    to=envelope.sender,
                    sender=self.controller_agent.crypto.public_key,
                    protocol_id=tac_error.protocol_id,
                    message=tac_bytes,
                )
Beispiel #2
0
    def handle_controller_message(self, envelope: Envelope) -> None:
        """
        Handle messages from the controller.

        The controller does not expect a response for any of these messages.

        :param envelope: the controller message

        :return: None
        """
        assert envelope.protocol_id == "tac"
        tac_msg = TACSerializer().decode(envelope.message)
        tac_msg_type = TACMessage.Type(tac_msg.get("type"))
        logger.debug("[{}]: Handling controller response. type={}".format(
            self.agent_name, tac_msg_type))
        try:
            if envelope.sender != self.game_instance.controller_pbk:
                raise ValueError(
                    "The sender of the message is not the controller agent we registered with."
                )

            if tac_msg_type == TACMessage.Type.TAC_ERROR:
                self.on_tac_error(tac_msg, envelope.sender)
            elif self.game_instance.game_phase == GamePhase.PRE_GAME:
                raise ValueError(
                    "We do not expect a controller agent message in the pre game phase."
                )
            elif self.game_instance.game_phase == GamePhase.GAME_SETUP:
                if tac_msg_type == TACMessage.Type.GAME_DATA:
                    self.on_start(tac_msg, envelope.sender)
                elif tac_msg_type == TACMessage.Type.CANCELLED:
                    self.on_cancelled()
            elif self.game_instance.game_phase == GamePhase.GAME:
                if tac_msg_type == TACMessage.Type.TRANSACTION_CONFIRMATION:
                    self.on_transaction_confirmed(tac_msg, envelope.sender)
                elif tac_msg_type == TACMessage.Type.CANCELLED:
                    self.on_cancelled()
                elif tac_msg_type == TACMessage.Type.STATE_UPDATE:
                    self.on_state_update(tac_msg, envelope.sender)
            elif self.game_instance.game_phase == GamePhase.POST_GAME:
                raise ValueError(
                    "We do not expect a controller agent message in the post game phase."
                )
        except ValueError as e:
            logger.warning(str(e))