def test_match_ior(): t1 = Template() t1.sender = "sender1@host" t2 = Template() t2.to = "recv1@host" t2.metadata = {"performative": "query"} m1 = Message() m1.sender = "sender1@host" t1 |= t2 assert t1.match(m1) m2 = Message() m2.to = "recv1@host" m2.metadata = {"performative": "query"} assert t1.match(m2) m3 = Message() m3.sender = "sender2@host" m3.to = "recv1@host" m3.metadata = {"performative": "inform"} assert not t1.match(m3)
def test_match_false_metadata(): template = Template() template.metadata = {"performative": "query"} message = Message() assert not template.match(message) message.set_metadata("performative", "inform") assert not template.match(message)
def test_match_false_thread(): template = Template() template.thread = "thread-id" message = Message() assert not template.match(message) message.thread = "thread-id-false" assert not template.match(message)
def test_match_false_body(): template = Template() template.body = "Hello World" message = Message() assert not template.match(message) message.body = "Bye Bye Love" assert not template.match(message)
def test_match_false_to(): template = Template() template.to = "recv1@host" message = Message() assert not template.match(message) message.to = "recv2@host" assert not template.match(message)
def test_match_false_sender(): template = Template() template.sender = "sender2@host" message = Message() assert not template.match(message) message.sender = "sender1@host" assert not template.match(message)
def test_match_false_metadata_with_different_key(): template = Template() template.metadata = {"performative": "query"} message = Message() message.set_metadata("language", "query") assert not template.match(message)
def test_match(): template = Template() template.sender = "sender1@host" template.to = "recv1@host" template.body = "Hello World" template.thread = "thread-id" template.metadata = {"performative": "query"} message = Message() message.sender = "sender1@host" message.to = "recv1@host" message.body = "Hello World" message.thread = "thread-id" message.set_metadata("performative", "query") assert template.match(message)
async def __resolution_handshake(self): logger.info("Waiting for instructions") instructions_message = await self.receive(100) to_reveive = Template() to_reveive.set_metadata("performative", "inform") if to_reveive.match(instructions_message): # Receiving what the other agent will do foreign_action_choose = instructions_message.body self.actions_remaining.remove(foreign_action_choose) message = instructions_message.make_reply() message.set_metadata("performative", "inform") action_choose_key = self.__pick_my_actions() message.body = action_choose_key self.my_actions = self.coord_action.actions[action_choose_key] self.actions_remaining.remove(action_choose_key) await self.send(message)
async def first_handshake(self, message: Message): """ Send the first message for the handshake, with the action I choose to do. Then the agent send back his action. :param message: """ to_send = message.make_reply() my_action_key = self.__pick_my_actions() to_send.set_metadata("performative", "inform") to_send.body = my_action_key logger.info("Sending my chose action to the other agent") await self.send(to_send) reply = await self.receive(100) reply_verif = Template() reply_verif.set_metadata("performative", "inform") if reply_verif.match(reply): logger.info("Received the action of the other agent") self.my_actions = self.coord_action.actions[my_action_key] self.actions_remaining.remove(my_action_key) self.actions_remaining.remove(reply.body)
async def __handle_handshake(self, message): """ Method when the agent receives an handshake. It can accept it or reject it based on the goal of the coordinate action. :param message: the message to handle """ logger.info("Traiter message {}".format(message.body)) template = Template() template.set_metadata("performative", "proposal") if template.match(message): if message.body == self.coord_action.goal: logger.info("Accept proposal, the action it's the same goal ") accept_pop = message.make_reply() accept_pop.set_metadata("performative", "accept-proposal") await self.send(accept_pop) logger.info("Send accept-proposal to {}".format(message.sender)) await self.__resolution_handshake() else: logger.info("Not the same goal") accept_pop = message.make_reply() accept_pop.set_metadata("performative", "reject-proposal") await self.send(accept_pop)
async def __begin_handshake(self, agent_jid): """ Begin the negociation with the agent, send the goal and wait for a reponse. if the agent accepts then launches the handshake. :param agent_jid: the agent to communicate """ message_to_send = Message(to=agent_jid) message_to_send.set_metadata("performative", "proposal") message_to_send.body = self.coord_action.goal logger.info("Send message to {}".format(agent_jid)) await self.send(message_to_send) logger.info("Waiting for the reponse") await empty_queue(self) message = await self.receive(25) if message is None: logger.info("The dest didn't respond") else: t = Template() t.set_metadata("performative", "accept-proposal") if t.match(message): logger.info("{} Accepted the proposal".format(agent_jid)) await self.first_handshake(message) else: logger.warning("The message doesn't match the waiting")
# template.to = "recv1@host" template.to = "admin@Temp3rr0r-pc" template.body = "Hello World" template.thread = "thread-id" template.metadata = {"performative": "query"} message = Message() # message.sender = "sender1@host" message.sender = "madks@Temp3rr0r-pc" # message.to = "recv1@host" message.to = "admin@Temp3rr0r-pc" message.body = "Hello World" message.thread = "thread-id" message.set_metadata("performative", "query") assert template.match(message) t1 = Template() # t1.sender = "sender1@host" t1.sender = "madks@Temp3rr0r-pc" t2 = Template() # t2.to = "recv1@host" t2.to = "admin@Temp3rr0r-pc" t2.metadata = {"performative": "query"} m = Message() # m.sender = "sender1@host" m.sender = "madks@Temp3rr0r-pc" # m.to = "recv1@host" m.to = "admin@Temp3rr0r-pc" m.metadata = {"performative": "query"}