def handle_envelope(self, envelope: Envelope) -> None: """ Implement the reaction to an envelope. :param envelope: the envelope :return: None """ msg = FIPASerializer().decode(envelope.message) msg_performative = FIPAMessage.Performative(msg.get('performative')) proposals = cast(List[Description], msg.get("proposal")) message_id = cast(int, msg.get("id")) dialogue_id = cast(int, msg.get("dialogue_id")) if msg_performative == FIPAMessage.Performative.PROPOSE: if proposals is not []: for item in proposals: logger.info( "[{}]: received proposal={} in dialogue={}".format( self.context.agent_name, item.values, dialogue_id)) if "Price" in item.values.keys(): if item.values["Price"] < self.max_price: self.handle_accept(envelope.sender, message_id, dialogue_id) else: self.handle_decline(envelope.sender, message_id, dialogue_id)
def handle(self, message: Message, sender: str) -> None: """ Implement the reaction to a message. :param message: the message :param sender: the sender :return: None """ # convenience representations fipa_msg = cast(FIPAMessage, message) msg_performative = FIPAMessage.Performative(message.get('performative')) message_id = cast(int, message.get("message_id")) # recover dialogue dialogues = cast(Dialogues, self.context.dialogues) if dialogues.is_belonging_to_registered_dialogue(fipa_msg, sender, self.context.agent_public_key): dialogue = cast(Dialogue, dialogues.get_dialogue(fipa_msg, sender, self.context.agent_public_key)) dialogue.incoming_extend(fipa_msg) else: self._handle_unidentified_dialogue(fipa_msg, sender) return # handle message if msg_performative == FIPAMessage.Performative.PROPOSE: self._handle_propose(fipa_msg, sender, message_id, dialogue) elif msg_performative == FIPAMessage.Performative.DECLINE: self._handle_decline(fipa_msg, sender, message_id, dialogue) elif msg_performative == FIPAMessage.Performative.MATCH_ACCEPT_W_ADDRESS: self._handle_match_accept(fipa_msg, sender, message_id, dialogue) elif msg_performative == FIPAMessage.Performative.INFORM: self._handle_inform(fipa_msg, sender, message_id, dialogue)
def send_fipa_message(self, envelope: Envelope) -> None: """ Send fipa message handler. :param envelope: the message. :return: None """ fipa_message = FIPASerializer().decode(envelope.message) id = fipa_message.get("message_id") dialogue_id = fipa_message.get("dialogue_id") destination = envelope.to target = fipa_message.get("target") performative = FIPAMessage.Performative( fipa_message.get("performative")) if performative == FIPAMessage.Performative.CFP: query = fipa_message.get("query") query = b"" if query is None else query if type(query) == Query: query = pickle.dumps(query) self.send_cfp(id, dialogue_id, destination, target, query) elif performative == FIPAMessage.Performative.PROPOSE: proposal = cast(List[Description], fipa_message.get("proposal")) proposal_b = pickle.dumps(proposal) # type: bytes self.send_propose(id, dialogue_id, destination, target, proposal_b) elif performative == FIPAMessage.Performative.ACCEPT: self.send_accept(id, dialogue_id, destination, target) elif performative == FIPAMessage.Performative.MATCH_ACCEPT: self.send_accept(id, dialogue_id, destination, target) elif performative == FIPAMessage.Performative.DECLINE: self.send_decline(id, dialogue_id, destination, target) else: raise ValueError("OEF FIPA message not recognized.")
def decode(self, obj: bytes) -> Message: """Decode bytes into a FIPA message.""" fipa_pb = fipa_pb2.FIPAMessage() fipa_pb.ParseFromString(obj) message_id = fipa_pb.message_id dialogue_reference = (fipa_pb.dialogue_starter_reference, fipa_pb.dialogue_responder_reference) target = fipa_pb.target performative = fipa_pb.WhichOneof("performative") performative_id = FIPAMessage.Performative(str(performative)) performative_content = dict() if performative_id == FIPAMessage.Performative.CFP: query_type = fipa_pb.cfp.WhichOneof("query") if query_type == "nothing": query = None elif query_type == "query_bytes": query = pickle.loads(fipa_pb.cfp.query_bytes) elif query_type == "bytes": query = fipa_pb.cfp.bytes else: raise ValueError("Query type not recognized.") # pragma: no cover performative_content["query"] = query elif performative_id == FIPAMessage.Performative.PROPOSE: descriptions = [] for p_bytes in fipa_pb.propose.proposal: p = pickle.loads(p_bytes) # type: Description descriptions.append(p) performative_content["proposal"] = descriptions elif performative_id == FIPAMessage.Performative.ACCEPT: pass elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT: pass elif performative_id == FIPAMessage.Performative.ACCEPT_W_ADDRESS: address = fipa_pb.accept_w_address.address performative_content['address'] = address elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT_W_ADDRESS: address = fipa_pb.match_accept_w_address.address performative_content['address'] = address elif performative_id == FIPAMessage.Performative.DECLINE: pass elif performative_id == FIPAMessage.Performative.INFORM: data = json.loads(fipa_pb.inform.bytes) performative_content["json_data"] = data else: raise ValueError("Performative not valid: {}.".format(performative)) return FIPAMessage(message_id=message_id, dialogue_reference=dialogue_reference, target=target, performative=performative, **performative_content)
def handle_envelope(self, envelope: Envelope) -> None: """ Implement the reaction to an envelope. :param envelope: the envelope :return: None """ msg = FIPASerializer().decode(envelope.message) msg = cast(FIPAMessage, msg) msg_performative = FIPAMessage.Performative(msg.get('performative')) message_id = cast(int, msg.get('id')) dialogue_id = cast(int, msg.get('dialogue_id')) if msg_performative == FIPAMessage.Performative.CFP: self.handle_cfp(msg, envelope.sender, message_id, dialogue_id) elif msg_performative == FIPAMessage.Performative.ACCEPT: self.handle_accept(envelope.sender)
def encode(self, msg: Message) -> bytes: """Encode a FIPA message into bytes.""" fipa_msg = fipa_pb2.FIPAMessage() fipa_msg.message_id = msg.get("id") fipa_msg.dialogue_id = msg.get("dialogue_id") fipa_msg.target = msg.get("target") performative_id = FIPAMessage.Performative(msg.get("performative")) if performative_id == FIPAMessage.Performative.CFP: performative = fipa_pb2.FIPAMessage.CFP() # type: ignore query = msg.get("query") if query is None or query == b"": nothing = fipa_pb2.FIPAMessage.CFP.Nothing() # type: ignore performative.nothing.CopyFrom(nothing) elif type(query) == Query: query = pickle.dumps(query) performative.query_bytes = query elif type(query) == bytes: performative.bytes = query else: raise ValueError("Query type not supported: {}".format( type(query))) fipa_msg.cfp.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.PROPOSE: performative = fipa_pb2.FIPAMessage.Propose() # type: ignore proposal = cast(Description, msg.get("proposal")) p_array_bytes = [pickle.dumps(p) for p in proposal] performative.proposal.extend(p_array_bytes) fipa_msg.propose.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.ACCEPT: performative = fipa_pb2.FIPAMessage.Accept() # type: ignore fipa_msg.accept.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT: performative = fipa_pb2.FIPAMessage.MatchAccept() # type: ignore fipa_msg.match_accept.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.DECLINE: performative = fipa_pb2.FIPAMessage.Decline() # type: ignore fipa_msg.decline.CopyFrom(performative) else: raise ValueError( "Performative not valid: {}".format(performative_id)) fipa_bytes = fipa_msg.SerializeToString() return fipa_bytes
def handle(self, message: Message, sender: str) -> None: """ Implement the reaction to a message. :param message: the message :param sender: the sender :return: None """ # convenience representations fipa_msg = cast(FIPAMessage, message) msg_performative = FIPAMessage.Performative( fipa_msg.get('performative')) message_id = cast(int, fipa_msg.get('message_id')) dialogue_reference = cast(Tuple[str, str], fipa_msg.get('dialogue_reference')) # recover dialogue dialogues = cast(Dialogues, self.context.dialogues) if dialogues.is_belonging_to_registered_dialogue( fipa_msg, sender, self.context.agent_public_key): dialogue = cast( Dialogue, dialogues.get_dialogue(fipa_msg, sender, self.context.agent_public_key)) dialogue.incoming_extend(fipa_msg) elif dialogues.is_permitted_for_new_dialogue(fipa_msg, sender): dialogue = cast( Dialogue, dialogues.create_opponent_initiated(sender, dialogue_reference, is_seller=True)) dialogue.incoming_extend(fipa_msg) else: self._handle_unidentified_dialogue(fipa_msg, sender) return # handle message if msg_performative == FIPAMessage.Performative.CFP: self._handle_cfp(fipa_msg, sender, message_id, dialogue) elif msg_performative == FIPAMessage.Performative.DECLINE: self._handle_decline(fipa_msg, sender, message_id, dialogue) elif msg_performative == FIPAMessage.Performative.ACCEPT: self._handle_accept(fipa_msg, sender, message_id, dialogue) elif msg_performative == FIPAMessage.Performative.INFORM: self._handle_inform(fipa_msg, sender, message_id, dialogue)
def decode(self, obj: bytes) -> Message: """Decode bytes into a FIPA message.""" fipa_pb = fipa_pb2.FIPAMessage() fipa_pb.ParseFromString(obj) message_id = fipa_pb.message_id dialogue_id = fipa_pb.dialogue_id target = fipa_pb.target performative = fipa_pb.WhichOneof("performative") performative_id = FIPAMessage.Performative(str(performative)) performative_content = dict() if performative_id == FIPAMessage.Performative.CFP: query_type = fipa_pb.cfp.WhichOneof("query") if query_type == "nothing": query = None elif query_type == "query_bytes": query = pickle.loads(fipa_pb.cfp.query_bytes) elif query_type == "bytes": query = fipa_pb.cfp.bytes else: raise ValueError("Query type not recognized.") performative_content["query"] = query elif performative_id == FIPAMessage.Performative.PROPOSE: descriptions = [] for p_bytes in fipa_pb.propose.proposal: p = pickle.loads(p_bytes) # type: Description descriptions.append(p) performative_content["proposal"] = descriptions elif performative_id == FIPAMessage.Performative.ACCEPT: pass elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT: pass elif performative_id == FIPAMessage.Performative.DECLINE: pass else: raise ValueError( "Performative not valid: {}.".format(performative)) return FIPAMessage(message_id=message_id, dialogue_id=dialogue_id, target=target, performative=performative, **performative_content)
def encode(self, msg: Message) -> bytes: """Encode a FIPA message into bytes.""" fipa_msg = fipa_pb2.FIPAMessage() fipa_msg.message_id = msg.get("message_id") dialogue_reference = cast(Tuple[str, str], msg.get("dialogue_reference")) fipa_msg.dialogue_starter_reference = dialogue_reference[0] fipa_msg.dialogue_responder_reference = dialogue_reference[1] fipa_msg.target = msg.get("target") performative_id = FIPAMessage.Performative(msg.get("performative")) if performative_id == FIPAMessage.Performative.CFP: performative = fipa_pb2.FIPAMessage.CFP() # type: ignore query = msg.get("query") if query is None or query == b"": nothing = fipa_pb2.FIPAMessage.CFP.Nothing() # type: ignore performative.nothing.CopyFrom(nothing) elif type(query) == Query: query = pickle.dumps(query) performative.query_bytes = query elif type(query) == bytes: performative.bytes = query else: raise ValueError("Query type not supported: {}".format(type(query))) fipa_msg.cfp.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.PROPOSE: performative = fipa_pb2.FIPAMessage.Propose() # type: ignore proposal = cast(Description, msg.get("proposal")) p_array_bytes = [pickle.dumps(p) for p in proposal] performative.proposal.extend(p_array_bytes) fipa_msg.propose.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.ACCEPT: performative = fipa_pb2.FIPAMessage.Accept() # type: ignore fipa_msg.accept.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT: performative = fipa_pb2.FIPAMessage.MatchAccept() # type: ignore fipa_msg.match_accept.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.ACCEPT_W_ADDRESS: performative = fipa_pb2.FIPAMessage.AcceptWAddress() # type: ignore address = msg.get("address") if type(address) == str: performative.address = address fipa_msg.accept_w_address.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.MATCH_ACCEPT_W_ADDRESS: performative = fipa_pb2.FIPAMessage.MatchAcceptWAddress() # type: ignore address = msg.get("address") if type(address) == str: performative.address = address fipa_msg.match_accept_w_address.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.DECLINE: performative = fipa_pb2.FIPAMessage.Decline() # type: ignore fipa_msg.decline.CopyFrom(performative) elif performative_id == FIPAMessage.Performative.INFORM: performative = fipa_pb2.FIPAMessage.Inform() # type: ignore data = msg.get("json_data") data_bytes = json.dumps(data).encode("utf-8") performative.bytes = data_bytes fipa_msg.inform.CopyFrom(performative) else: raise ValueError("Performative not valid: {}".format(performative_id)) fipa_bytes = fipa_msg.SerializeToString() return fipa_bytes