예제 #1
0
    def on_oef_error(
        self, answer_id: int, operation: oef.messages.OEFErrorOperation
    ) -> None:
        """
        On oef error event handler.

        :param answer_id: the answer id.
        :param operation: the error operation.
        :return: None
        """
        assert self.in_queue is not None
        assert self.loop is not None
        try:
            operation = OEFMessage.OEFErrorOperation(operation)
        except ValueError:
            operation = OEFMessage.OEFErrorOperation.OTHER

        msg = OEFMessage(
            type=OEFMessage.Type.OEF_ERROR, id=answer_id, operation=operation
        )
        msg_bytes = OEFSerializer().encode(msg)
        envelope = Envelope(
            to=self.address,
            sender=DEFAULT_OEF,
            protocol_id=OEFMessage.protocol_id,
            message=msg_bytes,
        )
        asyncio.run_coroutine_threadsafe(
            self.in_queue.put(envelope), self.loop
        ).result()
예제 #2
0
    def decode(self, obj: bytes) -> Message:
        """
        Decode the message.

        :param obj: the bytes object
        :return: the message
        """
        json_msg = json.loads(obj.decode("utf-8"))
        oef_type = OEFMessage.Type(json_msg["type"])
        oef_id = json_msg["id"]
        new_body = copy.copy(json_msg)

        if oef_type in {
                OEFMessage.Type.REGISTER_SERVICE,
                OEFMessage.Type.UNREGISTER_SERVICE,
        }:
            service_description_bytes = base64.b64decode(
                json_msg["service_description"])
            service_description = pickle.loads(
                service_description_bytes)  # nosec
            new_body["service_description"] = service_description
        elif oef_type in {
                OEFMessage.Type.REGISTER_AGENT,
                OEFMessage.Type.UNREGISTER_AGENT,
        }:
            agent_description_bytes = base64.b64decode(
                json_msg["agent_description"])
            agent_description = pickle.loads(agent_description_bytes)  # nosec
            new_body["agent_description"] = agent_description
        elif oef_type in {
                OEFMessage.Type.SEARCH_SERVICES,
                OEFMessage.Type.SEARCH_AGENTS,
        }:
            query_bytes = base64.b64decode(json_msg["query"])
            query = pickle.loads(query_bytes)  # nosec
            new_body["query"] = query
        elif oef_type in {OEFMessage.Type.SEARCH_RESULT}:
            new_body["agents"] = list(json_msg["agents"])
        elif oef_type in {OEFMessage.Type.OEF_ERROR}:
            operation = json_msg["operation"]
            new_body["operation"] = OEFMessage.OEFErrorOperation(
                int(operation))

        oef_message = OEFMessage(type=oef_type, id=oef_id, body=new_body)
        return oef_message