Esempio n. 1
0
    def handle(self, message: Message) -> None:
        """
        Implement the reaction to a message.

        :param message: the message
        :return: None
        """
        # convenience representations
        fipa_msg = cast(FIPAMessage, message)
        msg_performative = FIPAMessage.Performative(
            message.get("performative"))

        # recover dialogue
        dialogues = cast(Dialogues, self.context.dialogues)
        if dialogues.is_belonging_to_registered_dialogue(
                fipa_msg, self.context.agent_address):
            dialogue = cast(
                Dialogue,
                dialogues.get_dialogue(fipa_msg, self.context.agent_address))
            dialogue.incoming_extend(fipa_msg)
        else:
            self._handle_unidentified_dialogue(fipa_msg)
            return

        # handle message
        if msg_performative == FIPAMessage.Performative.PROPOSE:
            self._handle_propose(fipa_msg, dialogue)
        elif msg_performative == FIPAMessage.Performative.DECLINE:
            self._handle_decline(fipa_msg, dialogue)
        elif msg_performative == FIPAMessage.Performative.MATCH_ACCEPT_W_INFORM:
            self._handle_match_accept(fipa_msg, dialogue)
        elif msg_performative == FIPAMessage.Performative.INFORM:
            self._handle_inform(fipa_msg, dialogue)
Esempio n. 2
0
    def decode(self, obj: bytes) -> Message:
        """Decode bytes into a FIPA message."""
        fipa_pb = fipa_pb2.FIPAMessage()
        fipa_pb.ParseFromString(obj)
        message_id = fipa_pb.message_id
        dialogue_reference = (
            fipa_pb.dialogue_starter_reference,
            fipa_pb.dialogue_responder_reference,
        )
        target = fipa_pb.target

        performative = fipa_pb.WhichOneof("performative")
        performative_id = FIPAMessage.Performative(str(performative))
        performative_content = dict()
        if performative_id == FIPAMessage.Performative.CFP:
            query_type = fipa_pb.cfp.WhichOneof("query")
            if query_type == "nothing":
                query = None
            elif query_type == "query_bytes":
                query = pickle.loads(fipa_pb.cfp.query_bytes)  # nosec
            elif query_type == "bytes":
                query = fipa_pb.cfp.bytes
            else:
                raise ValueError("Query type not recognized.")  # pragma: no cover
            performative_content["query"] = query
        elif performative_id == FIPAMessage.Performative.PROPOSE:
            descriptions = []
            for p_bytes in fipa_pb.propose.proposal:
                p = pickle.loads(p_bytes)  # nosec
                p = cast(Description, p)
                descriptions.append(p)
            performative_content["proposal"] = descriptions
        elif performative_id == FIPAMessage.Performative.ACCEPT:
            pass
        elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT:
            pass
        elif performative_id == FIPAMessage.Performative.ACCEPT_W_INFORM:
            info = pickle.loads(fipa_pb.accept_w_inform.bytes)  # nosec
            performative_content["info"] = info
        elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT_W_INFORM:
            info = pickle.loads(fipa_pb.match_accept_w_inform.bytes)  # nosec
            performative_content["info"] = info
        elif performative_id == FIPAMessage.Performative.DECLINE:
            pass
        elif performative_id == FIPAMessage.Performative.INFORM:
            info = pickle.loads(fipa_pb.inform.bytes)  # nosec
            performative_content["info"] = info
        else:
            raise ValueError("Performative not valid: {}.".format(performative))

        return FIPAMessage(
            message_id=message_id,
            dialogue_reference=dialogue_reference,
            target=target,
            performative=performative,
            **performative_content
        )