Example #1
0
        def _encode_fipa_cfp(msg: FipaMessage) -> bytes:
            """Helper function to serialize FIPA CFP message."""
            message_pb = ProtobufMessage()
            dialogue_message_pb = DialogueMessage()
            fipa_msg = fipa_pb2.FipaMessage()

            dialogue_message_pb.message_id = msg.message_id
            dialogue_reference = msg.dialogue_reference
            dialogue_message_pb.dialogue_starter_reference = dialogue_reference[
                0]
            dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
                1]
            dialogue_message_pb.target = msg.target

            performative = fipa_pb2.FipaMessage.Cfp_Performative(
            )  # type: ignore
            # the following are commented to make the decoding to fail.
            # query = msg.query  # noqa: E800
            # Query.encode(performative.query, query)  # noqa: E800
            fipa_msg.cfp.CopyFrom(performative)
            dialogue_message_pb.content = fipa_msg.SerializeToString()

            message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
            fipa_bytes = message_pb.SerializeToString()
            return fipa_bytes
Example #2
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'MlTrade' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(MlTradeMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        ml_trade_msg = ml_trade_pb2.MlTradeMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == MlTradeMessage.Performative.CFP:
            performative = ml_trade_pb2.MlTradeMessage.Cfp_Performative(
            )  # type: ignore
            query = msg.query
            Query.encode(performative.query, query)
            ml_trade_msg.cfp.CopyFrom(performative)
        elif performative_id == MlTradeMessage.Performative.TERMS:
            performative = ml_trade_pb2.MlTradeMessage.Terms_Performative(
            )  # type: ignore
            terms = msg.terms
            Description.encode(performative.terms, terms)
            ml_trade_msg.terms.CopyFrom(performative)
        elif performative_id == MlTradeMessage.Performative.ACCEPT:
            performative = ml_trade_pb2.MlTradeMessage.Accept_Performative(
            )  # type: ignore
            terms = msg.terms
            Description.encode(performative.terms, terms)
            tx_digest = msg.tx_digest
            performative.tx_digest = tx_digest
            ml_trade_msg.accept.CopyFrom(performative)
        elif performative_id == MlTradeMessage.Performative.DATA:
            performative = ml_trade_pb2.MlTradeMessage.Data_Performative(
            )  # type: ignore
            terms = msg.terms
            Description.encode(performative.terms, terms)
            payload = msg.payload
            performative.payload = payload
            ml_trade_msg.data.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = ml_trade_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #3
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Http' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(HttpMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        http_msg = http_pb2.HttpMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == HttpMessage.Performative.REQUEST:
            performative = http_pb2.HttpMessage.Request_Performative(
            )  # type: ignore
            method = msg.method
            performative.method = method
            url = msg.url
            performative.url = url
            version = msg.version
            performative.version = version
            headers = msg.headers
            performative.headers = headers
            body = msg.body
            performative.body = body
            http_msg.request.CopyFrom(performative)
        elif performative_id == HttpMessage.Performative.RESPONSE:
            performative = http_pb2.HttpMessage.Response_Performative(
            )  # type: ignore
            version = msg.version
            performative.version = version
            status_code = msg.status_code
            performative.status_code = status_code
            status_text = msg.status_text
            performative.status_text = status_text
            headers = msg.headers
            performative.headers = headers
            body = msg.body
            performative.body = body
            http_msg.response.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = http_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #4
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'AgentEnvironment' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(AgentEnvironmentMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        agent_environment_msg = agent_environment_pb2.AgentEnvironmentMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == AgentEnvironmentMessage.Performative.TICK:
            performative = agent_environment_pb2.AgentEnvironmentMessage.Tick_Performative(
            )  # type: ignore
            tile_water = msg.tile_water
            performative.tile_water = tile_water
            turn_number = msg.turn_number
            performative.turn_number = turn_number
            agent_water = msg.agent_water
            performative.agent_water = agent_water
            north_neighbour_id = msg.north_neighbour_id
            performative.north_neighbour_id = north_neighbour_id
            east_neighbour_id = msg.east_neighbour_id
            performative.east_neighbour_id = east_neighbour_id
            south_neighbour_id = msg.south_neighbour_id
            performative.south_neighbour_id = south_neighbour_id
            west_neighbour_id = msg.west_neighbour_id
            performative.west_neighbour_id = west_neighbour_id
            movement_last_turn = msg.movement_last_turn
            performative.movement_last_turn = movement_last_turn
            agent_environment_msg.tick.CopyFrom(performative)
        elif performative_id == AgentEnvironmentMessage.Performative.ACTION:
            performative = agent_environment_pb2.AgentEnvironmentMessage.Action_Performative(
            )  # type: ignore
            command = msg.command
            performative.command = command
            agent_environment_msg.action.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = agent_environment_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #5
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'StateUpdate' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(StateUpdateMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        state_update_msg = state_update_pb2.StateUpdateMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == StateUpdateMessage.Performative.INITIALIZE:
            performative = state_update_pb2.StateUpdateMessage.Initialize_Performative(
            )  # type: ignore
            exchange_params_by_currency_id = msg.exchange_params_by_currency_id
            performative.exchange_params_by_currency_id.update(
                exchange_params_by_currency_id)
            utility_params_by_good_id = msg.utility_params_by_good_id
            performative.utility_params_by_good_id.update(
                utility_params_by_good_id)
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            state_update_msg.initialize.CopyFrom(performative)
        elif performative_id == StateUpdateMessage.Performative.APPLY:
            performative = state_update_pb2.StateUpdateMessage.Apply_Performative(
            )  # type: ignore
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            state_update_msg.apply.CopyFrom(performative)
        elif performative_id == StateUpdateMessage.Performative.END:
            performative = state_update_pb2.StateUpdateMessage.End_Performative(
            )  # type: ignore
            state_update_msg.end.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = state_update_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #6
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Default' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(DefaultMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        default_msg = default_pb2.DefaultMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == DefaultMessage.Performative.BYTES:
            performative = default_pb2.DefaultMessage.Bytes_Performative(
            )  # type: ignore
            content = msg.content
            performative.content = content
            default_msg.bytes.CopyFrom(performative)
        elif performative_id == DefaultMessage.Performative.ERROR:
            performative = default_pb2.DefaultMessage.Error_Performative(
            )  # type: ignore
            error_code = msg.error_code
            ErrorCode.encode(performative.error_code, error_code)
            error_msg = msg.error_msg
            performative.error_msg = error_msg
            error_data = msg.error_data
            performative.error_data.update(error_data)
            default_msg.error.CopyFrom(performative)
        elif performative_id == DefaultMessage.Performative.END:
            performative = default_pb2.DefaultMessage.End_Performative(
            )  # type: ignore
            default_msg.end.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = default_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #7
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Yoti' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(YotiMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        yoti_msg = yoti_pb2.YotiMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == YotiMessage.Performative.GET_PROFILE:
            performative = yoti_pb2.YotiMessage.Get_Profile_Performative()  # type: ignore
            token = msg.token
            performative.token = token
            dotted_path = msg.dotted_path
            performative.dotted_path = dotted_path
            args = msg.args
            performative.args.extend(args)
            yoti_msg.get_profile.CopyFrom(performative)
        elif performative_id == YotiMessage.Performative.PROFILE:
            performative = yoti_pb2.YotiMessage.Profile_Performative()  # type: ignore
            info = msg.info
            performative.info.update(info)
            yoti_msg.profile.CopyFrom(performative)
        elif performative_id == YotiMessage.Performative.ERROR:
            performative = yoti_pb2.YotiMessage.Error_Performative()  # type: ignore
            error_code = msg.error_code
            performative.error_code = error_code
            error_msg = msg.error_msg
            performative.error_msg = error_msg
            yoti_msg.error.CopyFrom(performative)
        else:
            raise ValueError("Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = yoti_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #8
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Register' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(RegisterMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        register_msg = register_pb2.RegisterMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == RegisterMessage.Performative.REGISTER:
            performative = register_pb2.RegisterMessage.Register_Performative()  # type: ignore
            info = msg.info
            performative.info.update(info)
            register_msg.register.CopyFrom(performative)
        elif performative_id == RegisterMessage.Performative.SUCCESS:
            performative = register_pb2.RegisterMessage.Success_Performative()  # type: ignore
            info = msg.info
            performative.info.update(info)
            register_msg.success.CopyFrom(performative)
        elif performative_id == RegisterMessage.Performative.ERROR:
            performative = register_pb2.RegisterMessage.Error_Performative()  # type: ignore
            error_code = msg.error_code
            performative.error_code = error_code
            error_msg = msg.error_msg
            performative.error_msg = error_msg
            info = msg.info
            performative.info.update(info)
            register_msg.error.CopyFrom(performative)
        else:
            raise ValueError("Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = register_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #9
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'AgentAgent' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(AgentAgentMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        agent_agent_msg = agent_agent_pb2.AgentAgentMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == AgentAgentMessage.Performative.SENDER_REQUEST:
            performative = agent_agent_pb2.AgentAgentMessage.Sender_Request_Performative(
            )  # type: ignore
            request = msg.request
            performative.request = request
            turn_number = msg.turn_number
            performative.turn_number = turn_number
            agent_agent_msg.sender_request.CopyFrom(performative)
        elif performative_id == AgentAgentMessage.Performative.RECEIVER_REPLY:
            performative = agent_agent_pb2.AgentAgentMessage.Receiver_Reply_Performative(
            )  # type: ignore
            reply = msg.reply
            performative.reply = reply
            agent_agent_msg.receiver_reply.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = agent_agent_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #10
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'LedgerApi' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(LedgerApiMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        ledger_api_msg = ledger_api_pb2.LedgerApiMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == LedgerApiMessage.Performative.GET_BALANCE:
            performative = ledger_api_pb2.LedgerApiMessage.Get_Balance_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            address = msg.address
            performative.address = address
            ledger_api_msg.get_balance.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.GET_RAW_TRANSACTION:
            performative = ledger_api_pb2.LedgerApiMessage.Get_Raw_Transaction_Performative(
            )  # type: ignore
            terms = msg.terms
            Terms.encode(performative.terms, terms)
            ledger_api_msg.get_raw_transaction.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.SEND_SIGNED_TRANSACTION:
            performative = ledger_api_pb2.LedgerApiMessage.Send_Signed_Transaction_Performative(
            )  # type: ignore
            signed_transaction = msg.signed_transaction
            SignedTransaction.encode(performative.signed_transaction,
                                     signed_transaction)
            ledger_api_msg.send_signed_transaction.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.GET_TRANSACTION_RECEIPT:
            performative = ledger_api_pb2.LedgerApiMessage.Get_Transaction_Receipt_Performative(
            )  # type: ignore
            transaction_digest = msg.transaction_digest
            TransactionDigest.encode(performative.transaction_digest,
                                     transaction_digest)
            ledger_api_msg.get_transaction_receipt.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.BALANCE:
            performative = ledger_api_pb2.LedgerApiMessage.Balance_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            balance = msg.balance
            performative.balance = balance
            ledger_api_msg.balance.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.RAW_TRANSACTION:
            performative = ledger_api_pb2.LedgerApiMessage.Raw_Transaction_Performative(
            )  # type: ignore
            raw_transaction = msg.raw_transaction
            RawTransaction.encode(performative.raw_transaction,
                                  raw_transaction)
            ledger_api_msg.raw_transaction.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.TRANSACTION_DIGEST:
            performative = ledger_api_pb2.LedgerApiMessage.Transaction_Digest_Performative(
            )  # type: ignore
            transaction_digest = msg.transaction_digest
            TransactionDigest.encode(performative.transaction_digest,
                                     transaction_digest)
            ledger_api_msg.transaction_digest.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.TRANSACTION_RECEIPT:
            performative = ledger_api_pb2.LedgerApiMessage.Transaction_Receipt_Performative(
            )  # type: ignore
            transaction_receipt = msg.transaction_receipt
            TransactionReceipt.encode(performative.transaction_receipt,
                                      transaction_receipt)
            ledger_api_msg.transaction_receipt.CopyFrom(performative)
        elif performative_id == LedgerApiMessage.Performative.ERROR:
            performative = ledger_api_pb2.LedgerApiMessage.Error_Performative(
            )  # type: ignore
            code = msg.code
            performative.code = code
            if msg.is_set("message"):
                performative.message_is_set = True
                message = msg.message
                performative.message = message
            if msg.is_set("data"):
                performative.data_is_set = True
                data = msg.data
                performative.data = data
            ledger_api_msg.error.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = ledger_api_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
    def encode(msg: Message) -> bytes:
        """
        Encode a 'ContractApi' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(ContractApiMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        contract_api_msg = contract_api_pb2.ContractApiMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == ContractApiMessage.Performative.GET_DEPLOY_TRANSACTION:
            performative = contract_api_pb2.ContractApiMessage.Get_Deploy_Transaction_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            contract_id = msg.contract_id
            performative.contract_id = contract_id
            callable = msg.callable
            performative.callable = callable
            kwargs = msg.kwargs
            Kwargs.encode(performative.kwargs, kwargs)
            contract_api_msg.get_deploy_transaction.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.GET_RAW_TRANSACTION:
            performative = contract_api_pb2.ContractApiMessage.Get_Raw_Transaction_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            contract_id = msg.contract_id
            performative.contract_id = contract_id
            contract_address = msg.contract_address
            performative.contract_address = contract_address
            callable = msg.callable
            performative.callable = callable
            kwargs = msg.kwargs
            Kwargs.encode(performative.kwargs, kwargs)
            contract_api_msg.get_raw_transaction.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.GET_RAW_MESSAGE:
            performative = contract_api_pb2.ContractApiMessage.Get_Raw_Message_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            contract_id = msg.contract_id
            performative.contract_id = contract_id
            contract_address = msg.contract_address
            performative.contract_address = contract_address
            callable = msg.callable
            performative.callable = callable
            kwargs = msg.kwargs
            Kwargs.encode(performative.kwargs, kwargs)
            contract_api_msg.get_raw_message.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.GET_STATE:
            performative = contract_api_pb2.ContractApiMessage.Get_State_Performative(
            )  # type: ignore
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            contract_id = msg.contract_id
            performative.contract_id = contract_id
            contract_address = msg.contract_address
            performative.contract_address = contract_address
            callable = msg.callable
            performative.callable = callable
            kwargs = msg.kwargs
            Kwargs.encode(performative.kwargs, kwargs)
            contract_api_msg.get_state.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.STATE:
            performative = contract_api_pb2.ContractApiMessage.State_Performative(
            )  # type: ignore
            state = msg.state
            State.encode(performative.state, state)
            contract_api_msg.state.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.RAW_TRANSACTION:
            performative = contract_api_pb2.ContractApiMessage.Raw_Transaction_Performative(
            )  # type: ignore
            raw_transaction = msg.raw_transaction
            RawTransaction.encode(performative.raw_transaction,
                                  raw_transaction)
            contract_api_msg.raw_transaction.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.RAW_MESSAGE:
            performative = contract_api_pb2.ContractApiMessage.Raw_Message_Performative(
            )  # type: ignore
            raw_message = msg.raw_message
            RawMessage.encode(performative.raw_message, raw_message)
            contract_api_msg.raw_message.CopyFrom(performative)
        elif performative_id == ContractApiMessage.Performative.ERROR:
            performative = contract_api_pb2.ContractApiMessage.Error_Performative(
            )  # type: ignore
            if msg.is_set("code"):
                performative.code_is_set = True
                code = msg.code
                performative.code = code
            if msg.is_set("message"):
                performative.message_is_set = True
                message = msg.message
                performative.message = message
            data = msg.data
            performative.data = data
            contract_api_msg.error.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = contract_api_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Fipa' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(FipaMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        fipa_msg = fipa_pb2.FipaMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == FipaMessage.Performative.CFP:
            performative = fipa_pb2.FipaMessage.Cfp_Performative()  # type: ignore
            query = msg.query
            Query.encode(performative.query, query)
            fipa_msg.cfp.CopyFrom(performative)
        elif performative_id == FipaMessage.Performative.PROPOSE:
            performative = fipa_pb2.FipaMessage.Propose_Performative()  # type: ignore
            proposal = msg.proposal
            Description.encode(performative.proposal, proposal)
            fipa_msg.propose.CopyFrom(performative)
        elif performative_id == FipaMessage.Performative.ACCEPT_W_INFORM:
            performative = fipa_pb2.FipaMessage.Accept_W_Inform_Performative()  # type: ignore
            info = msg.info
            performative.info.update(info)
            fipa_msg.accept_w_inform.CopyFrom(performative)
        elif performative_id == FipaMessage.Performative.MATCH_ACCEPT_W_INFORM:
            performative = fipa_pb2.FipaMessage.Match_Accept_W_Inform_Performative()  # type: ignore
            info = msg.info
            performative.info.update(info)
            fipa_msg.match_accept_w_inform.CopyFrom(performative)
        elif performative_id == FipaMessage.Performative.INFORM:
            performative = fipa_pb2.FipaMessage.Inform_Performative()  # type: ignore
            info = msg.info
            performative.info.update(info)
            fipa_msg.inform.CopyFrom(performative)
        elif performative_id == FipaMessage.Performative.ACCEPT:
            performative = fipa_pb2.FipaMessage.Accept_Performative()  # type: ignore
            fipa_msg.accept.CopyFrom(performative)
        elif performative_id == FipaMessage.Performative.DECLINE:
            performative = fipa_pb2.FipaMessage.Decline_Performative()  # type: ignore
            fipa_msg.decline.CopyFrom(performative)
        elif performative_id == FipaMessage.Performative.MATCH_ACCEPT:
            performative = fipa_pb2.FipaMessage.Match_Accept_Performative()  # type: ignore
            fipa_msg.match_accept.CopyFrom(performative)
        elif performative_id == FipaMessage.Performative.END:
            performative = fipa_pb2.FipaMessage.End_Performative()  # type: ignore
            fipa_msg.end.CopyFrom(performative)
        else:
            raise ValueError("Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = fipa_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #13
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Gym' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(GymMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        gym_msg = gym_pb2.GymMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == GymMessage.Performative.ACT:
            performative = gym_pb2.GymMessage.Act_Performative(
            )  # type: ignore
            action = msg.action
            AnyObject.encode(performative.action, action)
            step_id = msg.step_id
            performative.step_id = step_id
            gym_msg.act.CopyFrom(performative)
        elif performative_id == GymMessage.Performative.PERCEPT:
            performative = gym_pb2.GymMessage.Percept_Performative(
            )  # type: ignore
            step_id = msg.step_id
            performative.step_id = step_id
            observation = msg.observation
            AnyObject.encode(performative.observation, observation)
            reward = msg.reward
            performative.reward = reward
            done = msg.done
            performative.done = done
            info = msg.info
            AnyObject.encode(performative.info, info)
            gym_msg.percept.CopyFrom(performative)
        elif performative_id == GymMessage.Performative.STATUS:
            performative = gym_pb2.GymMessage.Status_Performative(
            )  # type: ignore
            content = msg.content
            performative.content.update(content)
            gym_msg.status.CopyFrom(performative)
        elif performative_id == GymMessage.Performative.RESET:
            performative = gym_pb2.GymMessage.Reset_Performative(
            )  # type: ignore
            gym_msg.reset.CopyFrom(performative)
        elif performative_id == GymMessage.Performative.CLOSE:
            performative = gym_pb2.GymMessage.Close_Performative(
            )  # type: ignore
            gym_msg.close.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = gym_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #14
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'TProtocol' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(TProtocolMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        t_protocol_msg = t_protocol_pb2.TProtocolMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == TProtocolMessage.Performative.PERFORMATIVE_CT:
            performative = t_protocol_pb2.TProtocolMessage.Performative_Ct_Performative(
            )  # type: ignore
            content_ct = msg.content_ct
            DataModel.encode(performative.content_ct, content_ct)
            t_protocol_msg.performative_ct.CopyFrom(performative)
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PT:
            performative = t_protocol_pb2.TProtocolMessage.Performative_Pt_Performative(
            )  # type: ignore
            content_bytes = msg.content_bytes
            performative.content_bytes = content_bytes
            content_int = msg.content_int
            performative.content_int = content_int
            content_float = msg.content_float
            performative.content_float = content_float
            content_bool = msg.content_bool
            performative.content_bool = content_bool
            content_str = msg.content_str
            performative.content_str = content_str
            t_protocol_msg.performative_pt.CopyFrom(performative)
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PCT:
            performative = t_protocol_pb2.TProtocolMessage.Performative_Pct_Performative(
            )  # type: ignore
            content_set_bytes = msg.content_set_bytes
            performative.content_set_bytes.extend(content_set_bytes)
            content_set_int = msg.content_set_int
            performative.content_set_int.extend(content_set_int)
            content_set_float = msg.content_set_float
            performative.content_set_float.extend(content_set_float)
            content_set_bool = msg.content_set_bool
            performative.content_set_bool.extend(content_set_bool)
            content_set_str = msg.content_set_str
            performative.content_set_str.extend(content_set_str)
            content_list_bytes = msg.content_list_bytes
            performative.content_list_bytes.extend(content_list_bytes)
            content_list_int = msg.content_list_int
            performative.content_list_int.extend(content_list_int)
            content_list_float = msg.content_list_float
            performative.content_list_float.extend(content_list_float)
            content_list_bool = msg.content_list_bool
            performative.content_list_bool.extend(content_list_bool)
            content_list_str = msg.content_list_str
            performative.content_list_str.extend(content_list_str)
            t_protocol_msg.performative_pct.CopyFrom(performative)
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PMT:
            performative = t_protocol_pb2.TProtocolMessage.Performative_Pmt_Performative(
            )  # type: ignore
            content_dict_int_bytes = msg.content_dict_int_bytes
            performative.content_dict_int_bytes.update(content_dict_int_bytes)
            content_dict_int_int = msg.content_dict_int_int
            performative.content_dict_int_int.update(content_dict_int_int)
            content_dict_int_float = msg.content_dict_int_float
            performative.content_dict_int_float.update(content_dict_int_float)
            content_dict_int_bool = msg.content_dict_int_bool
            performative.content_dict_int_bool.update(content_dict_int_bool)
            content_dict_int_str = msg.content_dict_int_str
            performative.content_dict_int_str.update(content_dict_int_str)
            content_dict_bool_bytes = msg.content_dict_bool_bytes
            performative.content_dict_bool_bytes.update(
                content_dict_bool_bytes)
            content_dict_bool_int = msg.content_dict_bool_int
            performative.content_dict_bool_int.update(content_dict_bool_int)
            content_dict_bool_float = msg.content_dict_bool_float
            performative.content_dict_bool_float.update(
                content_dict_bool_float)
            content_dict_bool_bool = msg.content_dict_bool_bool
            performative.content_dict_bool_bool.update(content_dict_bool_bool)
            content_dict_bool_str = msg.content_dict_bool_str
            performative.content_dict_bool_str.update(content_dict_bool_str)
            content_dict_str_bytes = msg.content_dict_str_bytes
            performative.content_dict_str_bytes.update(content_dict_str_bytes)
            content_dict_str_int = msg.content_dict_str_int
            performative.content_dict_str_int.update(content_dict_str_int)
            content_dict_str_float = msg.content_dict_str_float
            performative.content_dict_str_float.update(content_dict_str_float)
            content_dict_str_bool = msg.content_dict_str_bool
            performative.content_dict_str_bool.update(content_dict_str_bool)
            content_dict_str_str = msg.content_dict_str_str
            performative.content_dict_str_str.update(content_dict_str_str)
            t_protocol_msg.performative_pmt.CopyFrom(performative)
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_MT:
            performative = t_protocol_pb2.TProtocolMessage.Performative_Mt_Performative(
            )  # type: ignore
            if msg.is_set("content_union_1_type_DataModel"):
                performative.content_union_1_type_DataModel_is_set = True
                content_union_1_type_DataModel = msg.content_union_1_type_DataModel
                DataModel.encode(
                    performative.content_union_1_type_DataModel,
                    content_union_1_type_DataModel,
                )
            if msg.is_set("content_union_1_type_bytes"):
                performative.content_union_1_type_bytes_is_set = True
                content_union_1_type_bytes = msg.content_union_1_type_bytes
                performative.content_union_1_type_bytes = content_union_1_type_bytes
            if msg.is_set("content_union_1_type_int"):
                performative.content_union_1_type_int_is_set = True
                content_union_1_type_int = msg.content_union_1_type_int
                performative.content_union_1_type_int = content_union_1_type_int
            if msg.is_set("content_union_1_type_float"):
                performative.content_union_1_type_float_is_set = True
                content_union_1_type_float = msg.content_union_1_type_float
                performative.content_union_1_type_float = content_union_1_type_float
            if msg.is_set("content_union_1_type_bool"):
                performative.content_union_1_type_bool_is_set = True
                content_union_1_type_bool = msg.content_union_1_type_bool
                performative.content_union_1_type_bool = content_union_1_type_bool
            if msg.is_set("content_union_1_type_str"):
                performative.content_union_1_type_str_is_set = True
                content_union_1_type_str = msg.content_union_1_type_str
                performative.content_union_1_type_str = content_union_1_type_str
            if msg.is_set("content_union_1_type_set_of_int"):
                performative.content_union_1_type_set_of_int_is_set = True
                content_union_1_type_set_of_int = msg.content_union_1_type_set_of_int
                performative.content_union_1_type_set_of_int.extend(
                    content_union_1_type_set_of_int)
            if msg.is_set("content_union_1_type_list_of_bool"):
                performative.content_union_1_type_list_of_bool_is_set = True
                content_union_1_type_list_of_bool = (
                    msg.content_union_1_type_list_of_bool)
                performative.content_union_1_type_list_of_bool.extend(
                    content_union_1_type_list_of_bool)
            if msg.is_set("content_union_1_type_dict_of_str_int"):
                performative.content_union_1_type_dict_of_str_int_is_set = True
                content_union_1_type_dict_of_str_int = (
                    msg.content_union_1_type_dict_of_str_int)
                performative.content_union_1_type_dict_of_str_int.update(
                    content_union_1_type_dict_of_str_int)
            if msg.is_set("content_union_2_type_set_of_bytes"):
                performative.content_union_2_type_set_of_bytes_is_set = True
                content_union_2_type_set_of_bytes = (
                    msg.content_union_2_type_set_of_bytes)
                performative.content_union_2_type_set_of_bytes.extend(
                    content_union_2_type_set_of_bytes)
            if msg.is_set("content_union_2_type_set_of_int"):
                performative.content_union_2_type_set_of_int_is_set = True
                content_union_2_type_set_of_int = msg.content_union_2_type_set_of_int
                performative.content_union_2_type_set_of_int.extend(
                    content_union_2_type_set_of_int)
            if msg.is_set("content_union_2_type_set_of_str"):
                performative.content_union_2_type_set_of_str_is_set = True
                content_union_2_type_set_of_str = msg.content_union_2_type_set_of_str
                performative.content_union_2_type_set_of_str.extend(
                    content_union_2_type_set_of_str)
            if msg.is_set("content_union_2_type_list_of_float"):
                performative.content_union_2_type_list_of_float_is_set = True
                content_union_2_type_list_of_float = (
                    msg.content_union_2_type_list_of_float)
                performative.content_union_2_type_list_of_float.extend(
                    content_union_2_type_list_of_float)
            if msg.is_set("content_union_2_type_list_of_bool"):
                performative.content_union_2_type_list_of_bool_is_set = True
                content_union_2_type_list_of_bool = (
                    msg.content_union_2_type_list_of_bool)
                performative.content_union_2_type_list_of_bool.extend(
                    content_union_2_type_list_of_bool)
            if msg.is_set("content_union_2_type_list_of_bytes"):
                performative.content_union_2_type_list_of_bytes_is_set = True
                content_union_2_type_list_of_bytes = (
                    msg.content_union_2_type_list_of_bytes)
                performative.content_union_2_type_list_of_bytes.extend(
                    content_union_2_type_list_of_bytes)
            if msg.is_set("content_union_2_type_dict_of_str_int"):
                performative.content_union_2_type_dict_of_str_int_is_set = True
                content_union_2_type_dict_of_str_int = (
                    msg.content_union_2_type_dict_of_str_int)
                performative.content_union_2_type_dict_of_str_int.update(
                    content_union_2_type_dict_of_str_int)
            if msg.is_set("content_union_2_type_dict_of_int_float"):
                performative.content_union_2_type_dict_of_int_float_is_set = True
                content_union_2_type_dict_of_int_float = (
                    msg.content_union_2_type_dict_of_int_float)
                performative.content_union_2_type_dict_of_int_float.update(
                    content_union_2_type_dict_of_int_float)
            if msg.is_set("content_union_2_type_dict_of_bool_bytes"):
                performative.content_union_2_type_dict_of_bool_bytes_is_set = True
                content_union_2_type_dict_of_bool_bytes = (
                    msg.content_union_2_type_dict_of_bool_bytes)
                performative.content_union_2_type_dict_of_bool_bytes.update(
                    content_union_2_type_dict_of_bool_bytes)
            t_protocol_msg.performative_mt.CopyFrom(performative)
        elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_O:
            performative = t_protocol_pb2.TProtocolMessage.Performative_O_Performative(
            )  # type: ignore
            if msg.is_set("content_o_ct"):
                performative.content_o_ct_is_set = True
                content_o_ct = msg.content_o_ct
                DataModel.encode(performative.content_o_ct, content_o_ct)
            if msg.is_set("content_o_bool"):
                performative.content_o_bool_is_set = True
                content_o_bool = msg.content_o_bool
                performative.content_o_bool = content_o_bool
            if msg.is_set("content_o_set_int"):
                performative.content_o_set_int_is_set = True
                content_o_set_int = msg.content_o_set_int
                performative.content_o_set_int.extend(content_o_set_int)
            if msg.is_set("content_o_list_bytes"):
                performative.content_o_list_bytes_is_set = True
                content_o_list_bytes = msg.content_o_list_bytes
                performative.content_o_list_bytes.extend(content_o_list_bytes)
            if msg.is_set("content_o_dict_str_int"):
                performative.content_o_dict_str_int_is_set = True
                content_o_dict_str_int = msg.content_o_dict_str_int
                performative.content_o_dict_str_int.update(
                    content_o_dict_str_int)
            t_protocol_msg.performative_o.CopyFrom(performative)
        elif (performative_id ==
              TProtocolMessage.Performative.PERFORMATIVE_EMPTY_CONTENTS):
            performative = t_protocol_pb2.TProtocolMessage.Performative_Empty_Contents_Performative(
            )  # type: ignore
            t_protocol_msg.performative_empty_contents.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = t_protocol_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #15
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Tac' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(TacMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        tac_msg = tac_pb2.TacMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == TacMessage.Performative.REGISTER:
            performative = tac_pb2.TacMessage.Register_Performative(
            )  # type: ignore
            agent_name = msg.agent_name
            performative.agent_name = agent_name
            tac_msg.register.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.UNREGISTER:
            performative = tac_pb2.TacMessage.Unregister_Performative(
            )  # type: ignore
            tac_msg.unregister.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.TRANSACTION:
            performative = tac_pb2.TacMessage.Transaction_Performative(
            )  # type: ignore
            transaction_id = msg.transaction_id
            performative.transaction_id = transaction_id
            ledger_id = msg.ledger_id
            performative.ledger_id = ledger_id
            sender_address = msg.sender_address
            performative.sender_address = sender_address
            counterparty_address = msg.counterparty_address
            performative.counterparty_address = counterparty_address
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            fee_by_currency_id = msg.fee_by_currency_id
            performative.fee_by_currency_id.update(fee_by_currency_id)
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            nonce = msg.nonce
            performative.nonce = nonce
            sender_signature = msg.sender_signature
            performative.sender_signature = sender_signature
            counterparty_signature = msg.counterparty_signature
            performative.counterparty_signature = counterparty_signature
            tac_msg.transaction.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.CANCELLED:
            performative = tac_pb2.TacMessage.Cancelled_Performative(
            )  # type: ignore
            tac_msg.cancelled.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.GAME_DATA:
            performative = tac_pb2.TacMessage.Game_Data_Performative(
            )  # type: ignore
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            exchange_params_by_currency_id = msg.exchange_params_by_currency_id
            performative.exchange_params_by_currency_id.update(
                exchange_params_by_currency_id)
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            utility_params_by_good_id = msg.utility_params_by_good_id
            performative.utility_params_by_good_id.update(
                utility_params_by_good_id)
            fee_by_currency_id = msg.fee_by_currency_id
            performative.fee_by_currency_id.update(fee_by_currency_id)
            agent_addr_to_name = msg.agent_addr_to_name
            performative.agent_addr_to_name.update(agent_addr_to_name)
            currency_id_to_name = msg.currency_id_to_name
            performative.currency_id_to_name.update(currency_id_to_name)
            good_id_to_name = msg.good_id_to_name
            performative.good_id_to_name.update(good_id_to_name)
            version_id = msg.version_id
            performative.version_id = version_id
            if msg.is_set("info"):
                performative.info_is_set = True
                info = msg.info
                performative.info.update(info)
            tac_msg.game_data.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.TRANSACTION_CONFIRMATION:
            performative = tac_pb2.TacMessage.Transaction_Confirmation_Performative(
            )  # type: ignore
            transaction_id = msg.transaction_id
            performative.transaction_id = transaction_id
            amount_by_currency_id = msg.amount_by_currency_id
            performative.amount_by_currency_id.update(amount_by_currency_id)
            quantities_by_good_id = msg.quantities_by_good_id
            performative.quantities_by_good_id.update(quantities_by_good_id)
            tac_msg.transaction_confirmation.CopyFrom(performative)
        elif performative_id == TacMessage.Performative.TAC_ERROR:
            performative = tac_pb2.TacMessage.Tac_Error_Performative(
            )  # type: ignore
            error_code = msg.error_code
            ErrorCode.encode(performative.error_code, error_code)
            if msg.is_set("info"):
                performative.info_is_set = True
                info = msg.info
                performative.info.update(info)
            tac_msg.tac_error.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = tac_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
Example #16
0
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Signing' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(SigningMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        signing_msg = signing_pb2.SigningMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == SigningMessage.Performative.SIGN_TRANSACTION:
            performative = signing_pb2.SigningMessage.Sign_Transaction_Performative(
            )  # type: ignore
            terms = msg.terms
            Terms.encode(performative.terms, terms)
            raw_transaction = msg.raw_transaction
            RawTransaction.encode(performative.raw_transaction,
                                  raw_transaction)
            signing_msg.sign_transaction.CopyFrom(performative)
        elif performative_id == SigningMessage.Performative.SIGN_MESSAGE:
            performative = signing_pb2.SigningMessage.Sign_Message_Performative(
            )  # type: ignore
            terms = msg.terms
            Terms.encode(performative.terms, terms)
            raw_message = msg.raw_message
            RawMessage.encode(performative.raw_message, raw_message)
            signing_msg.sign_message.CopyFrom(performative)
        elif performative_id == SigningMessage.Performative.SIGNED_TRANSACTION:
            performative = signing_pb2.SigningMessage.Signed_Transaction_Performative(
            )  # type: ignore
            signed_transaction = msg.signed_transaction
            SignedTransaction.encode(performative.signed_transaction,
                                     signed_transaction)
            signing_msg.signed_transaction.CopyFrom(performative)
        elif performative_id == SigningMessage.Performative.SIGNED_MESSAGE:
            performative = signing_pb2.SigningMessage.Signed_Message_Performative(
            )  # type: ignore
            signed_message = msg.signed_message
            SignedMessage.encode(performative.signed_message, signed_message)
            signing_msg.signed_message.CopyFrom(performative)
        elif performative_id == SigningMessage.Performative.ERROR:
            performative = signing_pb2.SigningMessage.Error_Performative(
            )  # type: ignore
            error_code = msg.error_code
            ErrorCode.encode(performative.error_code, error_code)
            signing_msg.error.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = signing_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
    def encode(msg: Message) -> bytes:
        """
        Encode a 'Prometheus' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(PrometheusMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        prometheus_msg = prometheus_pb2.PrometheusMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == PrometheusMessage.Performative.ADD_METRIC:
            performative = prometheus_pb2.PrometheusMessage.Add_Metric_Performative(
            )  # type: ignore
            type = msg.type
            performative.type = type
            title = msg.title
            performative.title = title
            description = msg.description
            performative.description = description
            labels = msg.labels
            performative.labels.update(labels)
            prometheus_msg.add_metric.CopyFrom(performative)
        elif performative_id == PrometheusMessage.Performative.UPDATE_METRIC:
            performative = prometheus_pb2.PrometheusMessage.Update_Metric_Performative(
            )  # type: ignore
            title = msg.title
            performative.title = title
            callable = msg.callable
            performative.callable = callable
            value = msg.value
            performative.value = value
            labels = msg.labels
            performative.labels.update(labels)
            prometheus_msg.update_metric.CopyFrom(performative)
        elif performative_id == PrometheusMessage.Performative.RESPONSE:
            performative = prometheus_pb2.PrometheusMessage.Response_Performative(
            )  # type: ignore
            code = msg.code
            performative.code = code
            if msg.is_set("message"):
                performative.message_is_set = True
                message = msg.message
                performative.message = message
            prometheus_msg.response.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = prometheus_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes
    def encode(msg: Message) -> bytes:
        """
        Encode a 'OefSearch' message into bytes.

        :param msg: the message object.
        :return: the bytes.
        """
        msg = cast(OefSearchMessage, msg)
        message_pb = ProtobufMessage()
        dialogue_message_pb = DialogueMessage()
        oef_search_msg = oef_search_pb2.OefSearchMessage()

        dialogue_message_pb.message_id = msg.message_id
        dialogue_reference = msg.dialogue_reference
        dialogue_message_pb.dialogue_starter_reference = dialogue_reference[0]
        dialogue_message_pb.dialogue_responder_reference = dialogue_reference[
            1]
        dialogue_message_pb.target = msg.target

        performative_id = msg.performative
        if performative_id == OefSearchMessage.Performative.REGISTER_SERVICE:
            performative = oef_search_pb2.OefSearchMessage.Register_Service_Performative(
            )  # type: ignore
            service_description = msg.service_description
            Description.encode(performative.service_description,
                               service_description)
            oef_search_msg.register_service.CopyFrom(performative)
        elif performative_id == OefSearchMessage.Performative.UNREGISTER_SERVICE:
            performative = oef_search_pb2.OefSearchMessage.Unregister_Service_Performative(
            )  # type: ignore
            service_description = msg.service_description
            Description.encode(performative.service_description,
                               service_description)
            oef_search_msg.unregister_service.CopyFrom(performative)
        elif performative_id == OefSearchMessage.Performative.SEARCH_SERVICES:
            performative = oef_search_pb2.OefSearchMessage.Search_Services_Performative(
            )  # type: ignore
            query = msg.query
            Query.encode(performative.query, query)
            oef_search_msg.search_services.CopyFrom(performative)
        elif performative_id == OefSearchMessage.Performative.SEARCH_RESULT:
            performative = oef_search_pb2.OefSearchMessage.Search_Result_Performative(
            )  # type: ignore
            agents = msg.agents
            performative.agents.extend(agents)
            agents_info = msg.agents_info
            AgentsInfo.encode(performative.agents_info, agents_info)
            oef_search_msg.search_result.CopyFrom(performative)
        elif performative_id == OefSearchMessage.Performative.SUCCESS:
            performative = oef_search_pb2.OefSearchMessage.Success_Performative(
            )  # type: ignore
            agents_info = msg.agents_info
            AgentsInfo.encode(performative.agents_info, agents_info)
            oef_search_msg.success.CopyFrom(performative)
        elif performative_id == OefSearchMessage.Performative.OEF_ERROR:
            performative = oef_search_pb2.OefSearchMessage.Oef_Error_Performative(
            )  # type: ignore
            oef_error_operation = msg.oef_error_operation
            OefErrorOperation.encode(performative.oef_error_operation,
                                     oef_error_operation)
            oef_search_msg.oef_error.CopyFrom(performative)
        else:
            raise ValueError(
                "Performative not valid: {}".format(performative_id))

        dialogue_message_pb.content = oef_search_msg.SerializeToString()

        message_pb.dialogue_message.CopyFrom(dialogue_message_pb)
        message_bytes = message_pb.SerializeToString()
        return message_bytes