def _message_received(self, msg): """ Callback run when an XMPP Message is reveived. This callback delivers the message to every behaviour that is waiting for it using their templates match. the aioxmpp.Message is converted to spade.message.Message Args: msg (aioxmpp.Messagge): the message just received. Returns: list(asyncio.Future): a list of futures of the append of the message at each matched behaviour. """ logger.debug(f"Got message: {msg}") msg = Message.from_node(msg) futures = [] matched = False for behaviour in (x for x in self.behaviours if x.match(msg)): futures.append(self.submit(behaviour.enqueue(msg))) logger.debug(f"Message enqueued to behaviour: {behaviour}") self.traces.append(msg, category=str(behaviour)) matched = True if not matched: logger.warning(f"No behaviour matched for message: {msg}") self.traces.append(msg) return futures
def test_body_with_languages(): msg = aioxmpp.Message(type_=aioxmpp.MessageType.CHAT) msg.body["en"] = "Hello World" msg.body["es"] = "Hola Mundo" new_msg = Message.from_node(msg) assert new_msg.body == "Hello World"
def test_body_with_languages(): msg = aioxmpp.Message(type_=aioxmpp.MessageType.CHAT) msg.body["en"] = "Hello World" msg.body["es"] = "Hola Mundo" new_msg = Message.from_node(msg) assert new_msg.body == "Hello World"
def test_message_from_node(): aiomsg = aioxmpp.Message(type_=aioxmpp.MessageType.CHAT) data = forms_xso.Data(type_=forms_xso.DataType.FORM) data.fields.append( forms_xso.Field( var="performative", type_=forms_xso.FieldType.TEXT_SINGLE, values=["request"], ) ) data.fields.append( forms_xso.Field( var="_thread_node", type_=forms_xso.FieldType.TEXT_SINGLE, values=["thread-id"], ) ) data.title = SPADE_X_METADATA aiomsg.xep0004_data = [data] msg = Message.from_node(aiomsg) assert msg.thread == "thread-id" assert msg.get_metadata("performative") == "request" assert msg.metadata == {"performative": "request"}
def setup(self): self.web.start(templates_path="examples") template1 = Template(sender="agent0@fake_server") template2 = Template(sender="agent1@fake_server") template3 = Template(sender="agent2@fake_server") template4 = Template(sender="agent3@fake_server") # Create some dummy behaviours dummybehav = self.DummyBehav() self.add_behaviour(dummybehav, template=template1) periodbehav = self.DummyPeriodBehav(period=12.7) self.add_behaviour(periodbehav, template=template2) timeoutbehav = self.DummyTimeoutBehav(start_at=datetime.datetime.now()) self.add_behaviour(timeoutbehav, template=template3) fsm_behav = self.DummyFSMBehav() self.add_behaviour(fsm_behav, template=template4) behavs = [dummybehav, periodbehav, timeoutbehav, fsm_behav] # Create some fake contacts self.add_fake_contact("agent0@fake_server", PresenceType.AVAILABLE) self.add_fake_contact("agent1@fake_server", PresenceType.AVAILABLE, show=PresenceShow.AWAY) self.add_fake_contact( "agent2@fake_server", PresenceType.AVAILABLE, show=PresenceShow.DO_NOT_DISTURB, ) self.add_fake_contact("agent3@fake_server", PresenceType.UNAVAILABLE) self.add_fake_contact("agent4@fake_server", PresenceType.AVAILABLE, show=PresenceShow.CHAT) self.add_fake_contact("agent5@fake_server", PresenceType.UNAVAILABLE) # Send and Receive some fake messages self.traces.reset() for i in range(20): number = random.randint(0, 3) from_ = JID.fromstr("agent{}@fake_server".format(number)) msg = aioxmpp.Message(from_=from_, to=self.jid, type_=MessageType.CHAT) msg.body[None] = "Hello from {}! This is a long message.".format( from_.localpart) msg = Message.from_node(msg) msg.metadata = { "performative": "inform", "acl-representation": "xml" } msg = msg.prepare() self._message_received(msg=msg) msg = Message(sender=str(self.jid), to=str(from_), body="This is my answer.") msg.sent = True self.traces.append(msg, category=str(behavs[number]))
def _message_received(self, msg): """ Callback run when an XMPP Message is reveived. The aioxmpp.Message is converted to spade.message.Message Args: msg (aioxmpp.Messagge): the message just received. Returns: asyncio.Future: a future of the append of the message. """ msg = Message.from_node(msg) logger.debug(f"Got message: {msg}") return asyncio.run_coroutine_threadsafe(self.queue.put(msg), self.loop)
def test_receive_without_behaviours(): agent = make_connected_agent() aiomsg = aioxmpp.Message(type_=aioxmpp.MessageType.CHAT) msg = Message.from_node(aiomsg) assert agent.traces.len() == 0 agent.start(auto_register=False) with LogCapture() as log: agent._message_received(aiomsg) log.check_present(('spade.Agent', 'WARNING', f"No behaviour matched for message: {msg}")) assert agent.traces.len() == 1 assert msg in agent.traces.store[0] agent.stop()
def test_receive_without_behaviours(): agent = make_connected_agent() aiomsg = aioxmpp.Message(type_=aioxmpp.MessageType.CHAT) msg = Message.from_node(aiomsg) assert agent.traces.len() == 0 future = agent.start(auto_register=False) assert future.result() is None with LogCapture() as log: agent._message_received(aiomsg) log.check_present(('spade.Agent', 'WARNING', f"No behaviour matched for message: {msg}")) assert agent.traces.len() == 1 assert msg in agent.traces.store[0] agent.stop()
def _message_received(self, msg): """ Callback run when an XMPP Message is reveived. This callback delivers the message to every behaviour that is waiting for it. First, the aioxmpp.Message is converted to spade.message.Message Args: msg (aioxmpp.Messagge): the message just received. Returns: list(asyncio.Future): a list of futures of the append of the message at each matched behaviour. """ msg = Message.from_node(msg) return self.dispatch(msg)
def _message_received(self, msg): """ Callback run when an XMPP Message is reveived. This callback delivers the message to every behaviour that is waiting for it. First, the aioxmpp.Message is converted to spade.message.Message Args: msg (aioxmpp.Messagge): the message just received. Returns: list(asyncio.Future): a list of futures of the append of the message at each matched behaviour. """ msg = Message.from_node(msg) return self.dispatch(msg)
def test_receive_without_behaviours(): agent = MockedAgentFactory() aiomsg = aioxmpp.Message(type_=aioxmpp.MessageType.CHAT) msg = Message.from_node(aiomsg) assert agent.traces.len() == 0 future = agent.start(auto_register=False) assert future.result() is None with LogCapture() as log: agent._message_received(aiomsg) log.check_present(("spade.Agent", "WARNING", f"No behaviour matched for message: {msg}")) assert agent.traces.len() == 1 assert msg in agent.traces.store[0] agent.stop()
def setup(self): self.web.start(templates_path="examples") template1 = Template(sender="agent0@fake_server") template2 = Template(sender="agent1@fake_server") template3 = Template(sender="agent2@fake_server") template4 = Template(sender="agent3@fake_server") # Create some dummy behaviours dummybehav = self.DummyBehav() self.add_behaviour(dummybehav, template=template1) periodbehav = self.DummyPeriodBehav(period=12.7) self.add_behaviour(periodbehav, template=template2) timeoutbehav = self.DummyTimeoutBehav(start_at=datetime.datetime.now()) self.add_behaviour(timeoutbehav, template=template3) fsm_behav = self.DummyFSMBehav() self.add_behaviour(fsm_behav, template=template4) behavs = [dummybehav, periodbehav, timeoutbehav, fsm_behav] # Create some fake contacts self.add_fake_contact("agent0@fake_server", PresenceType.AVAILABLE) self.add_fake_contact("agent1@fake_server", PresenceType.AVAILABLE, show=PresenceShow.AWAY) self.add_fake_contact("agent2@fake_server", PresenceType.AVAILABLE, show=PresenceShow.DO_NOT_DISTURB) self.add_fake_contact("agent3@fake_server", PresenceType.UNAVAILABLE) self.add_fake_contact("agent4@fake_server", PresenceType.AVAILABLE, show=PresenceShow.CHAT) self.add_fake_contact("agent5@fake_server", PresenceType.UNAVAILABLE) # Send and Receive some fake messages self.traces.reset() for i in range(20): number = random.randint(0, 3) from_ = JID.fromstr("agent{}@fake_server".format(number)) msg = aioxmpp.Message(from_=from_, to=self.jid, type_=MessageType.CHAT) msg.body[None] = "Hello from {}! This is a long message.".format(from_.localpart) msg = Message.from_node(msg) msg.metadata = {"performative": "inform", "acl-representation": "xml"} msg = msg.prepare() self._message_received(msg=msg) msg = Message(sender=str(self.jid), to=str(from_), body="This is my answer.") msg.sent = True self.traces.append(msg, category=str(behavs[number]))
def test_message_from_node(): aiomsg = aioxmpp.Message(type_=aioxmpp.MessageType.CHAT) data = forms_xso.Data(type_=forms_xso.DataType.FORM) data.fields.append( forms_xso.Field( var="performative", type_=forms_xso.FieldType.TEXT_SINGLE, values=["request"], ) ) data.fields.append(forms_xso.Field(var="_thread_node", type_=forms_xso.FieldType.TEXT_SINGLE, values=["thread-id"])) data.title = SPADE_X_METADATA aiomsg.xep0004_data = [data] msg = Message.from_node(aiomsg) assert msg.thread == "thread-id" assert msg.get_metadata("performative") == "request" assert msg.metadata == {"performative": "request"}
def test_message_from_node_attribute_error(): with pytest.raises(AttributeError) as e: Message.from_node(Message())
def test_message_from_node_attribute_error(): with pytest.raises(AttributeError) as e: Message.from_node(Message())