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 test_fipa_propose_serialization(): """Test that the serialization for the 'fipa' protocol works.""" proposal = [ Description({"foo1": 1, "bar1": 2}), Description({"foo2": 1, "bar2": 2}), ] msg = FIPAMessage(message_id=0, dialogue_reference=(str(0), ''), target=0, performative=FIPAMessage.Performative.PROPOSE, proposal=proposal) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope(to="receiver", sender="sender", protocol_id=FIPAMessage.protocol_id, message=msg_bytes) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope == actual_envelope actual_msg = FIPASerializer().decode(actual_envelope.message) expected_msg = msg p1 = actual_msg.get("proposal") p2 = expected_msg.get("proposal") assert p1[0].values == p2[0].values assert p1[1].values == p2[1].values
def test_fipa_cfp_serialization_bytes(): """Test that the serialization - deserialization for the 'fipa' protocol works.""" query = b'Hello' msg = FIPAMessage(message_id=0, dialogue_reference=(str(0), ''), target=0, performative=FIPAMessage.Performative.CFP, query=query) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope(to="receiver", sender="sender", protocol_id=FIPAMessage.protocol_id, message=msg_bytes) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope == actual_envelope actual_msg = FIPASerializer().decode(actual_envelope.message) expected_msg = msg assert expected_msg == actual_msg deserialised_msg = FIPASerializer().decode(envelope.message) assert msg.get("performative") == deserialised_msg.get("performative")
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 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 test_performative_match_accept(): """Test the serialization - deserialization of the match_accept performative.""" msg = FIPAMessage(message_id=0, dialogue_reference=(str(0), ''), target=1, performative=FIPAMessage.Performative.MATCH_ACCEPT) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope(to="receiver", sender="sender", protocol_id=FIPAMessage.protocol_id, message=msg_bytes) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope == actual_envelope deserialised_msg = FIPASerializer().decode(envelope.message) assert msg.get("performative") == deserialised_msg.get("performative")
def test_performative_inform(): """Test the serialization-deserialization of the inform performative.""" msg = FIPAMessage(message_id=0, dialogue_reference=(str(0), ''), target=1, performative=FIPAMessage.Performative.INFORM, json_data={"foo": "bar"}) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope(to="receiver", sender="sender", protocol_id=FIPAMessage.protocol_id, message=msg_bytes) envelope_bytes = envelope.encode() actual_envelope = Envelope.decode(envelope_bytes) expected_envelope = envelope assert expected_envelope == actual_envelope deserialised_msg = FIPASerializer().decode(envelope.message) assert msg.get("performative") == deserialised_msg.get("performative")
def handle_dialogue_message(self, envelope: Envelope) -> None: """ Handle messages from the other agents. The agents expect a response. :param envelope: the envelope. :return: None """ message = FIPASerializer().decode(envelope.message) # type: Message logger.debug("Handling Dialogue message. type={}".format( type(message.get("performative")))) if self.dialogues.is_belonging_to_registered_dialogue( message, self.crypto.public_key, envelope.sender): self.on_existing_dialogue(message, envelope.sender) elif self.dialogues.is_permitted_for_new_dialogue( message, self.game_instance.game_configuration.agent_pbks, envelope.sender): self.on_new_dialogue(message, envelope.sender) else: self.on_unidentified_dialogue(message, envelope.sender)
def handle_envelope(self, envelope: Envelope) -> None: """ Dispatch envelope to relevant handler and respond. :param envelope: the envelope :return: None """ fipa_msg = FIPASerializer().decode(envelope.message) fipa_msg = cast(FIPAMessage, fipa_msg) fipa_msg_performative = fipa_msg.get("performative") # FIPAMessage.Performative(fipa_msg.get("performative")) logger.debug("[{}]: Identifying dialogue of FIPAMessage={}".format(self.context.agent_name, fipa_msg)) dialogues = cast(Dialogues, self.context.dialogues) if dialogues.is_belonging_to_registered_dialogue(fipa_msg, envelope.sender, self.context.agent_public_key): dialogue = dialogues.get_dialogue(fipa_msg, envelope.sender, self.context.agent_public_key) dialogue.incoming_extend(fipa_msg) elif dialogues.is_permitted_for_new_dialogue(fipa_msg, envelope.sender): dialogue = dialogues.create_opponent_initiated(fipa_msg, envelope.sender) dialogue.incoming_extend(fipa_msg) else: logger.debug("[{}]: Unidentified dialogue.".format(self.context.agent_name)) default_msg = DefaultMessage(type=DefaultMessage.Type.BYTES, content=b'This message belongs to an unidentified dialogue.') msg_bytes = DefaultSerializer().encode(default_msg) self.context.outbox.put_message(to=envelope.sender, sender=self.context.agent_public_key, protocol_id=DefaultMessage.protocol_id, message=msg_bytes) return logger.debug("[{}]: Handling FIPAMessage of performative={}".format(self.context.agent_name, fipa_msg_performative)) fipa_msg = cast(FIPAMessage, fipa_msg) if fipa_msg_performative == FIPAMessage.Performative.CFP: self._on_cfp(fipa_msg, dialogue) elif fipa_msg_performative == FIPAMessage.Performative.PROPOSE: self._on_propose(fipa_msg, dialogue) elif fipa_msg_performative == FIPAMessage.Performative.DECLINE: self._on_decline(fipa_msg, dialogue) elif fipa_msg_performative == FIPAMessage.Performative.ACCEPT: self._on_accept(fipa_msg, dialogue) elif fipa_msg_performative == FIPAMessage.Performative.MATCH_ACCEPT: self._on_match_accept(fipa_msg, dialogue)