def decode(obj: bytes) -> Message: """ Decode bytes into a message using Protobuf. First, try to parse the input as a Protobuf 'Message'; if it fails, parse the bytes as struct. """ message_pb = ProtobufMessage() message_pb.ParseFromString(obj) message_type = message_pb.WhichOneof("message") if message_type == "body": body = dict(message_pb.body) # pylint: disable=no-member msg = TMessage(_body=body) return msg if message_type == "dialogue_message": dialogue_message_pb = ( message_pb.dialogue_message # pylint: disable=no-member ) message_id = dialogue_message_pb.message_id target = dialogue_message_pb.target dialogue_starter_reference = dialogue_message_pb.dialogue_starter_reference dialogue_responder_reference = ( dialogue_message_pb.dialogue_responder_reference ) body_json = Struct() body_json.ParseFromString(dialogue_message_pb.content) body = dict(body_json) body["message_id"] = message_id body["target"] = target body["dialogue_reference"] = ( dialogue_starter_reference, dialogue_responder_reference, ) return TMessage(_body=body) raise ValueError("Message type not recognized.") # pragma: nocover
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
def decode(obj: bytes) -> Message: """ Decode bytes into a 'OefSearch' message. :param obj: the bytes object. :return: the 'OefSearch' message. """ message_pb = ProtobufMessage() oef_search_pb = oef_search_pb2.OefSearchMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target oef_search_pb.ParseFromString(message_pb.dialogue_message.content) performative = oef_search_pb.WhichOneof("performative") performative_id = OefSearchMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == OefSearchMessage.Performative.REGISTER_SERVICE: pb2_service_description = oef_search_pb.register_service.service_description service_description = Description.decode(pb2_service_description) performative_content["service_description"] = service_description elif performative_id == OefSearchMessage.Performative.UNREGISTER_SERVICE: pb2_service_description = ( oef_search_pb.unregister_service.service_description) service_description = Description.decode(pb2_service_description) performative_content["service_description"] = service_description elif performative_id == OefSearchMessage.Performative.SEARCH_SERVICES: pb2_query = oef_search_pb.search_services.query query = Query.decode(pb2_query) performative_content["query"] = query elif performative_id == OefSearchMessage.Performative.SEARCH_RESULT: agents = oef_search_pb.search_result.agents agents_tuple = tuple(agents) performative_content["agents"] = agents_tuple pb2_agents_info = oef_search_pb.search_result.agents_info agents_info = AgentsInfo.decode(pb2_agents_info) performative_content["agents_info"] = agents_info elif performative_id == OefSearchMessage.Performative.SUCCESS: pb2_agents_info = oef_search_pb.success.agents_info agents_info = AgentsInfo.decode(pb2_agents_info) performative_content["agents_info"] = agents_info elif performative_id == OefSearchMessage.Performative.OEF_ERROR: pb2_oef_error_operation = oef_search_pb.oef_error.oef_error_operation oef_error_operation = OefErrorOperation.decode( pb2_oef_error_operation) performative_content["oef_error_operation"] = oef_error_operation else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return OefSearchMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
def decode(obj: bytes) -> Message: """ Decode bytes into a 'StateUpdate' message. :param obj: the bytes object. :return: the 'StateUpdate' message. """ message_pb = ProtobufMessage() state_update_pb = state_update_pb2.StateUpdateMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target state_update_pb.ParseFromString(message_pb.dialogue_message.content) performative = state_update_pb.WhichOneof("performative") performative_id = StateUpdateMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == StateUpdateMessage.Performative.INITIALIZE: exchange_params_by_currency_id = ( state_update_pb.initialize.exchange_params_by_currency_id) exchange_params_by_currency_id_dict = dict( exchange_params_by_currency_id) performative_content[ "exchange_params_by_currency_id"] = exchange_params_by_currency_id_dict utility_params_by_good_id = ( state_update_pb.initialize.utility_params_by_good_id) utility_params_by_good_id_dict = dict(utility_params_by_good_id) performative_content[ "utility_params_by_good_id"] = utility_params_by_good_id_dict amount_by_currency_id = state_update_pb.initialize.amount_by_currency_id amount_by_currency_id_dict = dict(amount_by_currency_id) performative_content[ "amount_by_currency_id"] = amount_by_currency_id_dict quantities_by_good_id = state_update_pb.initialize.quantities_by_good_id quantities_by_good_id_dict = dict(quantities_by_good_id) performative_content[ "quantities_by_good_id"] = quantities_by_good_id_dict elif performative_id == StateUpdateMessage.Performative.APPLY: amount_by_currency_id = state_update_pb.apply.amount_by_currency_id amount_by_currency_id_dict = dict(amount_by_currency_id) performative_content[ "amount_by_currency_id"] = amount_by_currency_id_dict quantities_by_good_id = state_update_pb.apply.quantities_by_good_id quantities_by_good_id_dict = dict(quantities_by_good_id) performative_content[ "quantities_by_good_id"] = quantities_by_good_id_dict else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return StateUpdateMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
def decode(obj: bytes) -> Message: """ Decode bytes into a 'Fipa' message. :param obj: the bytes object. :return: the 'Fipa' message. """ message_pb = ProtobufMessage() fipa_pb = fipa_pb2.FipaMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target fipa_pb.ParseFromString(message_pb.dialogue_message.content) performative = fipa_pb.WhichOneof("performative") performative_id = FipaMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == FipaMessage.Performative.CFP: pb2_query = fipa_pb.cfp.query query = Query.decode(pb2_query) performative_content["query"] = query elif performative_id == FipaMessage.Performative.PROPOSE: pb2_proposal = fipa_pb.propose.proposal proposal = Description.decode(pb2_proposal) performative_content["proposal"] = proposal elif performative_id == FipaMessage.Performative.ACCEPT_W_INFORM: info = fipa_pb.accept_w_inform.info info_dict = dict(info) performative_content["info"] = info_dict elif performative_id == FipaMessage.Performative.MATCH_ACCEPT_W_INFORM: info = fipa_pb.match_accept_w_inform.info info_dict = dict(info) performative_content["info"] = info_dict elif performative_id == FipaMessage.Performative.INFORM: info = fipa_pb.inform.info info_dict = dict(info) performative_content["info"] = info_dict elif performative_id == FipaMessage.Performative.ACCEPT: pass elif performative_id == FipaMessage.Performative.DECLINE: pass elif performative_id == FipaMessage.Performative.MATCH_ACCEPT: pass elif performative_id == FipaMessage.Performative.END: pass else: raise ValueError("Performative not valid: {}.".format(performative_id)) return FipaMessage( message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content )
def decode(obj: bytes) -> Message: """ Decode bytes into a 'Signing' message. :param obj: the bytes object. :return: the 'Signing' message. """ message_pb = ProtobufMessage() signing_pb = signing_pb2.SigningMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target signing_pb.ParseFromString(message_pb.dialogue_message.content) performative = signing_pb.WhichOneof("performative") performative_id = SigningMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == SigningMessage.Performative.SIGN_TRANSACTION: pb2_terms = signing_pb.sign_transaction.terms terms = Terms.decode(pb2_terms) performative_content["terms"] = terms pb2_raw_transaction = signing_pb.sign_transaction.raw_transaction raw_transaction = RawTransaction.decode(pb2_raw_transaction) performative_content["raw_transaction"] = raw_transaction elif performative_id == SigningMessage.Performative.SIGN_MESSAGE: pb2_terms = signing_pb.sign_message.terms terms = Terms.decode(pb2_terms) performative_content["terms"] = terms pb2_raw_message = signing_pb.sign_message.raw_message raw_message = RawMessage.decode(pb2_raw_message) performative_content["raw_message"] = raw_message elif performative_id == SigningMessage.Performative.SIGNED_TRANSACTION: pb2_signed_transaction = signing_pb.signed_transaction.signed_transaction signed_transaction = SignedTransaction.decode( pb2_signed_transaction) performative_content["signed_transaction"] = signed_transaction elif performative_id == SigningMessage.Performative.SIGNED_MESSAGE: pb2_signed_message = signing_pb.signed_message.signed_message signed_message = SignedMessage.decode(pb2_signed_message) performative_content["signed_message"] = signed_message elif performative_id == SigningMessage.Performative.ERROR: pb2_error_code = signing_pb.error.error_code error_code = ErrorCode.decode(pb2_error_code) performative_content["error_code"] = error_code else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return SigningMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
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
def decode(obj: bytes) -> Message: """ Decode bytes into a 'Gym' message. :param obj: the bytes object. :return: the 'Gym' message. """ message_pb = ProtobufMessage() gym_pb = gym_pb2.GymMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target gym_pb.ParseFromString(message_pb.dialogue_message.content) performative = gym_pb.WhichOneof("performative") performative_id = GymMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == GymMessage.Performative.ACT: pb2_action = gym_pb.act.action action = AnyObject.decode(pb2_action) performative_content["action"] = action step_id = gym_pb.act.step_id performative_content["step_id"] = step_id elif performative_id == GymMessage.Performative.PERCEPT: step_id = gym_pb.percept.step_id performative_content["step_id"] = step_id pb2_observation = gym_pb.percept.observation observation = AnyObject.decode(pb2_observation) performative_content["observation"] = observation reward = gym_pb.percept.reward performative_content["reward"] = reward done = gym_pb.percept.done performative_content["done"] = done pb2_info = gym_pb.percept.info info = AnyObject.decode(pb2_info) performative_content["info"] = info elif performative_id == GymMessage.Performative.STATUS: content = gym_pb.status.content content_dict = dict(content) performative_content["content"] = content_dict elif performative_id == GymMessage.Performative.RESET: pass elif performative_id == GymMessage.Performative.CLOSE: pass else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return GymMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
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
def decode(obj: bytes) -> Message: """ Decode bytes into a 'Prometheus' message. :param obj: the bytes object. :return: the 'Prometheus' message. """ message_pb = ProtobufMessage() prometheus_pb = prometheus_pb2.PrometheusMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target prometheus_pb.ParseFromString(message_pb.dialogue_message.content) performative = prometheus_pb.WhichOneof("performative") performative_id = PrometheusMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == PrometheusMessage.Performative.ADD_METRIC: type = prometheus_pb.add_metric.type performative_content["type"] = type title = prometheus_pb.add_metric.title performative_content["title"] = title description = prometheus_pb.add_metric.description performative_content["description"] = description labels = prometheus_pb.add_metric.labels labels_dict = dict(labels) performative_content["labels"] = labels_dict elif performative_id == PrometheusMessage.Performative.UPDATE_METRIC: title = prometheus_pb.update_metric.title performative_content["title"] = title callable = prometheus_pb.update_metric.callable performative_content["callable"] = callable value = prometheus_pb.update_metric.value performative_content["value"] = value labels = prometheus_pb.update_metric.labels labels_dict = dict(labels) performative_content["labels"] = labels_dict elif performative_id == PrometheusMessage.Performative.RESPONSE: code = prometheus_pb.response.code performative_content["code"] = code if prometheus_pb.response.message_is_set: message = prometheus_pb.response.message performative_content["message"] = message else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return PrometheusMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
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
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
def decode(obj: bytes) -> Message: """ Decode bytes into a 'Http' message. :param obj: the bytes object. :return: the 'Http' message. """ message_pb = ProtobufMessage() http_pb = http_pb2.HttpMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target http_pb.ParseFromString(message_pb.dialogue_message.content) performative = http_pb.WhichOneof("performative") performative_id = HttpMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == HttpMessage.Performative.REQUEST: method = http_pb.request.method performative_content["method"] = method url = http_pb.request.url performative_content["url"] = url version = http_pb.request.version performative_content["version"] = version headers = http_pb.request.headers performative_content["headers"] = headers body = http_pb.request.body performative_content["body"] = body elif performative_id == HttpMessage.Performative.RESPONSE: version = http_pb.response.version performative_content["version"] = version status_code = http_pb.response.status_code performative_content["status_code"] = status_code status_text = http_pb.response.status_text performative_content["status_text"] = status_text headers = http_pb.response.headers performative_content["headers"] = headers body = http_pb.response.body performative_content["body"] = body else: raise ValueError("Performative not valid: {}.".format(performative_id)) return HttpMessage( message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content )
def decode(obj: bytes) -> Message: """ Decode bytes into a 'AgentEnvironment' message. :param obj: the bytes object. :return: the 'AgentEnvironment' message. """ message_pb = ProtobufMessage() agent_environment_pb = agent_environment_pb2.AgentEnvironmentMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target agent_environment_pb.ParseFromString( message_pb.dialogue_message.content) performative = agent_environment_pb.WhichOneof("performative") performative_id = AgentEnvironmentMessage.Performative( str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == AgentEnvironmentMessage.Performative.TICK: tile_water = agent_environment_pb.tick.tile_water performative_content["tile_water"] = tile_water turn_number = agent_environment_pb.tick.turn_number performative_content["turn_number"] = turn_number agent_water = agent_environment_pb.tick.agent_water performative_content["agent_water"] = agent_water north_neighbour_id = agent_environment_pb.tick.north_neighbour_id performative_content["north_neighbour_id"] = north_neighbour_id east_neighbour_id = agent_environment_pb.tick.east_neighbour_id performative_content["east_neighbour_id"] = east_neighbour_id south_neighbour_id = agent_environment_pb.tick.south_neighbour_id performative_content["south_neighbour_id"] = south_neighbour_id west_neighbour_id = agent_environment_pb.tick.west_neighbour_id performative_content["west_neighbour_id"] = west_neighbour_id movement_last_turn = agent_environment_pb.tick.movement_last_turn performative_content["movement_last_turn"] = movement_last_turn elif performative_id == AgentEnvironmentMessage.Performative.ACTION: command = agent_environment_pb.action.command performative_content["command"] = command else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return AgentEnvironmentMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
def decode(obj: bytes) -> Message: """ Decode bytes into a 'MlTrade' message. :param obj: the bytes object. :return: the 'MlTrade' message. """ message_pb = ProtobufMessage() ml_trade_pb = ml_trade_pb2.MlTradeMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target ml_trade_pb.ParseFromString(message_pb.dialogue_message.content) performative = ml_trade_pb.WhichOneof("performative") performative_id = MlTradeMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == MlTradeMessage.Performative.CFP: pb2_query = ml_trade_pb.cfp.query query = Query.decode(pb2_query) performative_content["query"] = query elif performative_id == MlTradeMessage.Performative.TERMS: pb2_terms = ml_trade_pb.terms.terms terms = Description.decode(pb2_terms) performative_content["terms"] = terms elif performative_id == MlTradeMessage.Performative.ACCEPT: pb2_terms = ml_trade_pb.accept.terms terms = Description.decode(pb2_terms) performative_content["terms"] = terms tx_digest = ml_trade_pb.accept.tx_digest performative_content["tx_digest"] = tx_digest elif performative_id == MlTradeMessage.Performative.DATA: pb2_terms = ml_trade_pb.data.terms terms = Description.decode(pb2_terms) performative_content["terms"] = terms payload = ml_trade_pb.data.payload performative_content["payload"] = payload else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return MlTradeMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
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
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
def decode(obj: bytes) -> Message: """ Decode bytes into a 'Yoti' message. :param obj: the bytes object. :return: the 'Yoti' message. """ message_pb = ProtobufMessage() yoti_pb = yoti_pb2.YotiMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target yoti_pb.ParseFromString(message_pb.dialogue_message.content) performative = yoti_pb.WhichOneof("performative") performative_id = YotiMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == YotiMessage.Performative.GET_PROFILE: token = yoti_pb.get_profile.token performative_content["token"] = token dotted_path = yoti_pb.get_profile.dotted_path performative_content["dotted_path"] = dotted_path args = yoti_pb.get_profile.args args_tuple = tuple(args) performative_content["args"] = args_tuple elif performative_id == YotiMessage.Performative.PROFILE: info = yoti_pb.profile.info info_dict = dict(info) performative_content["info"] = info_dict elif performative_id == YotiMessage.Performative.ERROR: error_code = yoti_pb.error.error_code performative_content["error_code"] = error_code error_msg = yoti_pb.error.error_msg performative_content["error_msg"] = error_msg else: raise ValueError("Performative not valid: {}.".format(performative_id)) return YotiMessage( message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content )
def decode(obj: bytes) -> Message: """ Decode bytes into a 'Register' message. :param obj: the bytes object. :return: the 'Register' message. """ message_pb = ProtobufMessage() register_pb = register_pb2.RegisterMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target register_pb.ParseFromString(message_pb.dialogue_message.content) performative = register_pb.WhichOneof("performative") performative_id = RegisterMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == RegisterMessage.Performative.REGISTER: info = register_pb.register.info info_dict = dict(info) performative_content["info"] = info_dict elif performative_id == RegisterMessage.Performative.SUCCESS: info = register_pb.success.info info_dict = dict(info) performative_content["info"] = info_dict elif performative_id == RegisterMessage.Performative.ERROR: error_code = register_pb.error.error_code performative_content["error_code"] = error_code error_msg = register_pb.error.error_msg performative_content["error_msg"] = error_msg info = register_pb.error.info info_dict = dict(info) performative_content["info"] = info_dict else: raise ValueError("Performative not valid: {}.".format(performative_id)) return RegisterMessage( message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content )
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
def decode(obj: bytes) -> Message: """ Decode bytes into a 'Default' message. :param obj: the bytes object. :return: the 'Default' message. """ message_pb = ProtobufMessage() default_pb = default_pb2.DefaultMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target default_pb.ParseFromString(message_pb.dialogue_message.content) performative = default_pb.WhichOneof("performative") performative_id = DefaultMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == DefaultMessage.Performative.BYTES: content = default_pb.bytes.content performative_content["content"] = content elif performative_id == DefaultMessage.Performative.ERROR: pb2_error_code = default_pb.error.error_code error_code = ErrorCode.decode(pb2_error_code) performative_content["error_code"] = error_code error_msg = default_pb.error.error_msg performative_content["error_msg"] = error_msg error_data = default_pb.error.error_data error_data_dict = dict(error_data) performative_content["error_data"] = error_data_dict elif performative_id == DefaultMessage.Performative.END: pass else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return DefaultMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
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
def decode(obj: bytes) -> Message: """ Decode bytes into a 'AgentAgent' message. :param obj: the bytes object. :return: the 'AgentAgent' message. """ message_pb = ProtobufMessage() agent_agent_pb = agent_agent_pb2.AgentAgentMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target agent_agent_pb.ParseFromString(message_pb.dialogue_message.content) performative = agent_agent_pb.WhichOneof("performative") performative_id = AgentAgentMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == AgentAgentMessage.Performative.SENDER_REQUEST: request = agent_agent_pb.sender_request.request performative_content["request"] = request turn_number = agent_agent_pb.sender_request.turn_number performative_content["turn_number"] = turn_number elif performative_id == AgentAgentMessage.Performative.RECEIVER_REPLY: reply = agent_agent_pb.receiver_reply.reply performative_content["reply"] = reply else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return AgentAgentMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
def encode(msg: Message) -> bytes: """ Encode a message into bytes using Protobuf. - if one of message_id, target and dialogue_reference are not defined, serialize only the message body/ - otherwise, extract those fields from the body and instantiate a Message struct. """ message_pb = ProtobufMessage() if msg.has_dialogue_info: dialogue_message_pb = Pb2DialogueMessage() dialogue_message_pb.message_id = msg.message_id dialogue_message_pb.dialogue_starter_reference = msg.dialogue_reference[0] dialogue_message_pb.dialogue_responder_reference = msg.dialogue_reference[1] dialogue_message_pb.target = msg.target new_body = copy(msg._body) # pylint: disable=protected-access new_body.pop("message_id") new_body.pop("dialogue_reference") new_body.pop("target") body_json = Struct() body_json.update(new_body) # pylint: disable=no-member dialogue_message_pb.content = ( # pylint: disable=no-member body_json.SerializeToString() ) message_pb.dialogue_message.CopyFrom( # pylint: disable=no-member dialogue_message_pb ) else: body_json = Struct() body_json.update(msg._body) # pylint: disable=no-member,protected-access message_pb.body.CopyFrom(body_json) # pylint: disable=no-member return message_pb.SerializeToString()
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 decode(obj: bytes) -> Message: """ Decode bytes into a 'ContractApi' message. :param obj: the bytes object. :return: the 'ContractApi' message. """ message_pb = ProtobufMessage() contract_api_pb = contract_api_pb2.ContractApiMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target contract_api_pb.ParseFromString(message_pb.dialogue_message.content) performative = contract_api_pb.WhichOneof("performative") performative_id = ContractApiMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == ContractApiMessage.Performative.GET_DEPLOY_TRANSACTION: ledger_id = contract_api_pb.get_deploy_transaction.ledger_id performative_content["ledger_id"] = ledger_id contract_id = contract_api_pb.get_deploy_transaction.contract_id performative_content["contract_id"] = contract_id callable = contract_api_pb.get_deploy_transaction.callable performative_content["callable"] = callable pb2_kwargs = contract_api_pb.get_deploy_transaction.kwargs kwargs = Kwargs.decode(pb2_kwargs) performative_content["kwargs"] = kwargs elif performative_id == ContractApiMessage.Performative.GET_RAW_TRANSACTION: ledger_id = contract_api_pb.get_raw_transaction.ledger_id performative_content["ledger_id"] = ledger_id contract_id = contract_api_pb.get_raw_transaction.contract_id performative_content["contract_id"] = contract_id contract_address = contract_api_pb.get_raw_transaction.contract_address performative_content["contract_address"] = contract_address callable = contract_api_pb.get_raw_transaction.callable performative_content["callable"] = callable pb2_kwargs = contract_api_pb.get_raw_transaction.kwargs kwargs = Kwargs.decode(pb2_kwargs) performative_content["kwargs"] = kwargs elif performative_id == ContractApiMessage.Performative.GET_RAW_MESSAGE: ledger_id = contract_api_pb.get_raw_message.ledger_id performative_content["ledger_id"] = ledger_id contract_id = contract_api_pb.get_raw_message.contract_id performative_content["contract_id"] = contract_id contract_address = contract_api_pb.get_raw_message.contract_address performative_content["contract_address"] = contract_address callable = contract_api_pb.get_raw_message.callable performative_content["callable"] = callable pb2_kwargs = contract_api_pb.get_raw_message.kwargs kwargs = Kwargs.decode(pb2_kwargs) performative_content["kwargs"] = kwargs elif performative_id == ContractApiMessage.Performative.GET_STATE: ledger_id = contract_api_pb.get_state.ledger_id performative_content["ledger_id"] = ledger_id contract_id = contract_api_pb.get_state.contract_id performative_content["contract_id"] = contract_id contract_address = contract_api_pb.get_state.contract_address performative_content["contract_address"] = contract_address callable = contract_api_pb.get_state.callable performative_content["callable"] = callable pb2_kwargs = contract_api_pb.get_state.kwargs kwargs = Kwargs.decode(pb2_kwargs) performative_content["kwargs"] = kwargs elif performative_id == ContractApiMessage.Performative.STATE: pb2_state = contract_api_pb.state.state state = State.decode(pb2_state) performative_content["state"] = state elif performative_id == ContractApiMessage.Performative.RAW_TRANSACTION: pb2_raw_transaction = contract_api_pb.raw_transaction.raw_transaction raw_transaction = RawTransaction.decode(pb2_raw_transaction) performative_content["raw_transaction"] = raw_transaction elif performative_id == ContractApiMessage.Performative.RAW_MESSAGE: pb2_raw_message = contract_api_pb.raw_message.raw_message raw_message = RawMessage.decode(pb2_raw_message) performative_content["raw_message"] = raw_message elif performative_id == ContractApiMessage.Performative.ERROR: if contract_api_pb.error.code_is_set: code = contract_api_pb.error.code performative_content["code"] = code if contract_api_pb.error.message_is_set: message = contract_api_pb.error.message performative_content["message"] = message data = contract_api_pb.error.data performative_content["data"] = data else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return ContractApiMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
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
def decode(obj: bytes) -> Message: """ Decode bytes into a 'TProtocol' message. :param obj: the bytes object. :return: the 'TProtocol' message. """ message_pb = ProtobufMessage() t_protocol_pb = t_protocol_pb2.TProtocolMessage() message_pb.ParseFromString(obj) message_id = message_pb.dialogue_message.message_id dialogue_reference = ( message_pb.dialogue_message.dialogue_starter_reference, message_pb.dialogue_message.dialogue_responder_reference, ) target = message_pb.dialogue_message.target t_protocol_pb.ParseFromString(message_pb.dialogue_message.content) performative = t_protocol_pb.WhichOneof("performative") performative_id = TProtocolMessage.Performative(str(performative)) performative_content = dict() # type: Dict[str, Any] if performative_id == TProtocolMessage.Performative.PERFORMATIVE_CT: pb2_content_ct = t_protocol_pb.performative_ct.content_ct content_ct = DataModel.decode(pb2_content_ct) performative_content["content_ct"] = content_ct elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PT: content_bytes = t_protocol_pb.performative_pt.content_bytes performative_content["content_bytes"] = content_bytes content_int = t_protocol_pb.performative_pt.content_int performative_content["content_int"] = content_int content_float = t_protocol_pb.performative_pt.content_float performative_content["content_float"] = content_float content_bool = t_protocol_pb.performative_pt.content_bool performative_content["content_bool"] = content_bool content_str = t_protocol_pb.performative_pt.content_str performative_content["content_str"] = content_str elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PCT: content_set_bytes = t_protocol_pb.performative_pct.content_set_bytes content_set_bytes_frozenset = frozenset(content_set_bytes) performative_content[ "content_set_bytes"] = content_set_bytes_frozenset content_set_int = t_protocol_pb.performative_pct.content_set_int content_set_int_frozenset = frozenset(content_set_int) performative_content["content_set_int"] = content_set_int_frozenset content_set_float = t_protocol_pb.performative_pct.content_set_float content_set_float_frozenset = frozenset(content_set_float) performative_content[ "content_set_float"] = content_set_float_frozenset content_set_bool = t_protocol_pb.performative_pct.content_set_bool content_set_bool_frozenset = frozenset(content_set_bool) performative_content[ "content_set_bool"] = content_set_bool_frozenset content_set_str = t_protocol_pb.performative_pct.content_set_str content_set_str_frozenset = frozenset(content_set_str) performative_content["content_set_str"] = content_set_str_frozenset content_list_bytes = t_protocol_pb.performative_pct.content_list_bytes content_list_bytes_tuple = tuple(content_list_bytes) performative_content[ "content_list_bytes"] = content_list_bytes_tuple content_list_int = t_protocol_pb.performative_pct.content_list_int content_list_int_tuple = tuple(content_list_int) performative_content["content_list_int"] = content_list_int_tuple content_list_float = t_protocol_pb.performative_pct.content_list_float content_list_float_tuple = tuple(content_list_float) performative_content[ "content_list_float"] = content_list_float_tuple content_list_bool = t_protocol_pb.performative_pct.content_list_bool content_list_bool_tuple = tuple(content_list_bool) performative_content["content_list_bool"] = content_list_bool_tuple content_list_str = t_protocol_pb.performative_pct.content_list_str content_list_str_tuple = tuple(content_list_str) performative_content["content_list_str"] = content_list_str_tuple elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_PMT: content_dict_int_bytes = ( t_protocol_pb.performative_pmt.content_dict_int_bytes) content_dict_int_bytes_dict = dict(content_dict_int_bytes) performative_content[ "content_dict_int_bytes"] = content_dict_int_bytes_dict content_dict_int_int = t_protocol_pb.performative_pmt.content_dict_int_int content_dict_int_int_dict = dict(content_dict_int_int) performative_content[ "content_dict_int_int"] = content_dict_int_int_dict content_dict_int_float = ( t_protocol_pb.performative_pmt.content_dict_int_float) content_dict_int_float_dict = dict(content_dict_int_float) performative_content[ "content_dict_int_float"] = content_dict_int_float_dict content_dict_int_bool = t_protocol_pb.performative_pmt.content_dict_int_bool content_dict_int_bool_dict = dict(content_dict_int_bool) performative_content[ "content_dict_int_bool"] = content_dict_int_bool_dict content_dict_int_str = t_protocol_pb.performative_pmt.content_dict_int_str content_dict_int_str_dict = dict(content_dict_int_str) performative_content[ "content_dict_int_str"] = content_dict_int_str_dict content_dict_bool_bytes = ( t_protocol_pb.performative_pmt.content_dict_bool_bytes) content_dict_bool_bytes_dict = dict(content_dict_bool_bytes) performative_content[ "content_dict_bool_bytes"] = content_dict_bool_bytes_dict content_dict_bool_int = t_protocol_pb.performative_pmt.content_dict_bool_int content_dict_bool_int_dict = dict(content_dict_bool_int) performative_content[ "content_dict_bool_int"] = content_dict_bool_int_dict content_dict_bool_float = ( t_protocol_pb.performative_pmt.content_dict_bool_float) content_dict_bool_float_dict = dict(content_dict_bool_float) performative_content[ "content_dict_bool_float"] = content_dict_bool_float_dict content_dict_bool_bool = ( t_protocol_pb.performative_pmt.content_dict_bool_bool) content_dict_bool_bool_dict = dict(content_dict_bool_bool) performative_content[ "content_dict_bool_bool"] = content_dict_bool_bool_dict content_dict_bool_str = t_protocol_pb.performative_pmt.content_dict_bool_str content_dict_bool_str_dict = dict(content_dict_bool_str) performative_content[ "content_dict_bool_str"] = content_dict_bool_str_dict content_dict_str_bytes = ( t_protocol_pb.performative_pmt.content_dict_str_bytes) content_dict_str_bytes_dict = dict(content_dict_str_bytes) performative_content[ "content_dict_str_bytes"] = content_dict_str_bytes_dict content_dict_str_int = t_protocol_pb.performative_pmt.content_dict_str_int content_dict_str_int_dict = dict(content_dict_str_int) performative_content[ "content_dict_str_int"] = content_dict_str_int_dict content_dict_str_float = ( t_protocol_pb.performative_pmt.content_dict_str_float) content_dict_str_float_dict = dict(content_dict_str_float) performative_content[ "content_dict_str_float"] = content_dict_str_float_dict content_dict_str_bool = t_protocol_pb.performative_pmt.content_dict_str_bool content_dict_str_bool_dict = dict(content_dict_str_bool) performative_content[ "content_dict_str_bool"] = content_dict_str_bool_dict content_dict_str_str = t_protocol_pb.performative_pmt.content_dict_str_str content_dict_str_str_dict = dict(content_dict_str_str) performative_content[ "content_dict_str_str"] = content_dict_str_str_dict elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_MT: if t_protocol_pb.performative_mt.content_union_1_type_DataModel_is_set: pb2_content_union_1_type_DataModel = ( t_protocol_pb.performative_mt. content_union_1_type_DataModel) content_union_1 = DataModel.decode( pb2_content_union_1_type_DataModel) performative_content["content_union_1"] = content_union_1 if t_protocol_pb.performative_mt.content_union_1_type_bytes_is_set: content_union_1 = ( t_protocol_pb.performative_mt.content_union_1_type_bytes) performative_content["content_union_1"] = content_union_1 if t_protocol_pb.performative_mt.content_union_1_type_int_is_set: content_union_1 = t_protocol_pb.performative_mt.content_union_1_type_int performative_content["content_union_1"] = content_union_1 if t_protocol_pb.performative_mt.content_union_1_type_float_is_set: content_union_1 = ( t_protocol_pb.performative_mt.content_union_1_type_float) performative_content["content_union_1"] = content_union_1 if t_protocol_pb.performative_mt.content_union_1_type_bool_is_set: content_union_1 = ( t_protocol_pb.performative_mt.content_union_1_type_bool) performative_content["content_union_1"] = content_union_1 if t_protocol_pb.performative_mt.content_union_1_type_str_is_set: content_union_1 = t_protocol_pb.performative_mt.content_union_1_type_str performative_content["content_union_1"] = content_union_1 if t_protocol_pb.performative_mt.content_union_1_type_set_of_int_is_set: content_union_1 = t_protocol_pb.performative_mt.content_union_1 content_union_1_frozenset = frozenset(content_union_1) performative_content[ "content_union_1"] = content_union_1_frozenset if t_protocol_pb.performative_mt.content_union_1_type_list_of_bool_is_set: content_union_1 = t_protocol_pb.performative_mt.content_union_1 content_union_1_tuple = tuple(content_union_1) performative_content["content_union_1"] = content_union_1_tuple if (t_protocol_pb.performative_mt. content_union_1_type_dict_of_str_int_is_set): content_union_1 = t_protocol_pb.performative_mt.content_union_1 content_union_1_dict = dict(content_union_1) performative_content["content_union_1"] = content_union_1_dict if t_protocol_pb.performative_mt.content_union_2_type_set_of_bytes_is_set: content_union_2 = t_protocol_pb.performative_mt.content_union_2 content_union_2_frozenset = frozenset(content_union_2) performative_content[ "content_union_2"] = content_union_2_frozenset if t_protocol_pb.performative_mt.content_union_2_type_set_of_int_is_set: content_union_2 = t_protocol_pb.performative_mt.content_union_2 content_union_2_frozenset = frozenset(content_union_2) performative_content[ "content_union_2"] = content_union_2_frozenset if t_protocol_pb.performative_mt.content_union_2_type_set_of_str_is_set: content_union_2 = t_protocol_pb.performative_mt.content_union_2 content_union_2_frozenset = frozenset(content_union_2) performative_content[ "content_union_2"] = content_union_2_frozenset if t_protocol_pb.performative_mt.content_union_2_type_list_of_float_is_set: content_union_2 = t_protocol_pb.performative_mt.content_union_2 content_union_2_tuple = tuple(content_union_2) performative_content["content_union_2"] = content_union_2_tuple if t_protocol_pb.performative_mt.content_union_2_type_list_of_bool_is_set: content_union_2 = t_protocol_pb.performative_mt.content_union_2 content_union_2_tuple = tuple(content_union_2) performative_content["content_union_2"] = content_union_2_tuple if t_protocol_pb.performative_mt.content_union_2_type_list_of_bytes_is_set: content_union_2 = t_protocol_pb.performative_mt.content_union_2 content_union_2_tuple = tuple(content_union_2) performative_content["content_union_2"] = content_union_2_tuple if (t_protocol_pb.performative_mt. content_union_2_type_dict_of_str_int_is_set): content_union_2 = t_protocol_pb.performative_mt.content_union_2 content_union_2_dict = dict(content_union_2) performative_content["content_union_2"] = content_union_2_dict if (t_protocol_pb.performative_mt. content_union_2_type_dict_of_int_float_is_set): content_union_2 = t_protocol_pb.performative_mt.content_union_2 content_union_2_dict = dict(content_union_2) performative_content["content_union_2"] = content_union_2_dict if (t_protocol_pb.performative_mt. content_union_2_type_dict_of_bool_bytes_is_set): content_union_2 = t_protocol_pb.performative_mt.content_union_2 content_union_2_dict = dict(content_union_2) performative_content["content_union_2"] = content_union_2_dict elif performative_id == TProtocolMessage.Performative.PERFORMATIVE_O: if t_protocol_pb.performative_o.content_o_ct_is_set: pb2_content_o_ct = t_protocol_pb.performative_o.content_o_ct content_o_ct = DataModel.decode(pb2_content_o_ct) performative_content["content_o_ct"] = content_o_ct if t_protocol_pb.performative_o.content_o_bool_is_set: content_o_bool = t_protocol_pb.performative_o.content_o_bool performative_content["content_o_bool"] = content_o_bool if t_protocol_pb.performative_o.content_o_set_int_is_set: content_o_set_int = t_protocol_pb.performative_o.content_o_set_int content_o_set_int_frozenset = frozenset(content_o_set_int) performative_content[ "content_o_set_int"] = content_o_set_int_frozenset if t_protocol_pb.performative_o.content_o_list_bytes_is_set: content_o_list_bytes = t_protocol_pb.performative_o.content_o_list_bytes content_o_list_bytes_tuple = tuple(content_o_list_bytes) performative_content[ "content_o_list_bytes"] = content_o_list_bytes_tuple if t_protocol_pb.performative_o.content_o_dict_str_int_is_set: content_o_dict_str_int = ( t_protocol_pb.performative_o.content_o_dict_str_int) content_o_dict_str_int_dict = dict(content_o_dict_str_int) performative_content[ "content_o_dict_str_int"] = content_o_dict_str_int_dict elif (performative_id == TProtocolMessage.Performative.PERFORMATIVE_EMPTY_CONTENTS): pass else: raise ValueError( "Performative not valid: {}.".format(performative_id)) return TProtocolMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
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
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