Ejemplo n.º 1
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"])
        new_body = copy.copy(json_msg)
        new_body["type"] = oef_type

        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)
            new_body["service_description"] = service_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)
            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(oef_type=oef_type, body=new_body)
        return oef_message
Ejemplo n.º 2
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(oef_type=OEFMessage.Type.OEF_ERROR,
                         id=answer_id,
                         operation=operation)
        msg_bytes = OEFSerializer().encode(msg)
        envelope = Envelope(to=self.public_key,
                            sender=DEFAULT_OEF,
                            protocol_id=OEFMessage.protocol_id,
                            message=msg_bytes)
        asyncio.run_coroutine_threadsafe(self.in_queue.put(envelope),
                                         self.loop).result()