Beispiel #1
0
    def test_from_pb(self, query: Query):
        """Test that Query objects are correctly unpacked from the associated protobuf type."""

        expected_query = query
        query_pb = expected_query.to_pb()
        actual_query = Query.from_pb(query_pb)

        assert expected_query == actual_query
Beispiel #2
0
    async def loop(self, agent: AgentInterface) -> None:  # noqa: C901
        """
        Event loop to wait for messages and to dispatch the arrived messages to the proper handler.

        :param agent: the implementation of the message handlers specified in AgentInterface.
        :return: ``None``
        """
        while True:
            try:
                data = await self._receive()
            except asyncio.CancelledError:
                logger.debug("Proxy {}: loop cancelled".format(self.public_key))
                break
            msg = agent_pb2.Server.AgentMessage()
            msg.ParseFromString(data)
            case = msg.WhichOneof("payload")
            logger.debug("loop {0}".format(case))
            if case == "agents":
                agent.on_search_result(msg.answer_id, msg.agents.agents)
            elif case == "oef_error":
                agent.on_oef_error(msg.answer_id, OEFErrorOperation(msg.oef_error.operation))
            elif case == "dialogue_error":
                agent.on_dialogue_error(msg.answer_id, msg.dialogue_error.dialogue_id, msg.dialogue_error.origin)
            elif case == "content":
                content_case = msg.content.WhichOneof("payload")
                logger.debug("msg content {0}".format(content_case))
                if content_case == "content":
                    agent.on_message(msg.answer_id, msg.content.dialogue_id, msg.content.origin, msg.content.content)
                elif content_case == "fipa":
                    fipa = msg.content.fipa
                    fipa_case = fipa.WhichOneof("msg")
                    if fipa_case == "cfp":
                        cfp_case = fipa.cfp.WhichOneof("payload")
                        if cfp_case == "nothing":
                            query = None
                        elif cfp_case == "content":
                            query = fipa.cfp.content
                        elif cfp_case == "query":
                            query = Query.from_pb(fipa.cfp.query)
                        else:
                            raise Exception("Query type not valid.")
                        agent.on_cfp(msg.answer_id, msg.content.dialogue_id, msg.content.origin, fipa.target, query)
                    elif fipa_case == "propose":
                        propose_case = fipa.propose.WhichOneof("payload")
                        if propose_case == "content":
                            proposals = fipa.propose.content
                        else:
                            proposals = [Description.from_pb(propose) for propose in fipa.propose.proposals.objects]
                        agent.on_propose(msg.answer_id, msg.content.dialogue_id, msg.content.origin, fipa.target,
                                         proposals)
                    elif fipa_case == "accept":
                        agent.on_accept(msg.answer_id, msg.content.dialogue_id, msg.content.origin, fipa.target)
                    elif fipa_case == "decline":
                        agent.on_decline(msg.answer_id, msg.content.dialogue_id, msg.content.origin, fipa.target)
                    else:
                        logger.warning("Not implemented yet: fipa {0}".format(fipa_case))