Spade 3.0 example with ejabberd in docker(windows), does not work in separate files

49 Views Asked by At

I have created a docker container to run ejabberd on my windows 11 machine using this guide https://www.process-one.net/blog/install-ejabberd-on-windows-10-using-docker-desktop/ I have two agents, in SPADE, python, a Receiver and a Sender -->Receiver:

import spade
from spade.agent import Agent
from spade.behaviour import OneShotBehaviour
from spade.message import Message
from spade.template import Template



class ReceiverAgent(Agent):
    class RecvBehav(OneShotBehaviour):
        async def run(self):
            print("RecvBehav running")

            msg = await self.receive(timeout=60) # wait for a message for 10 seconds
            if msg:
                print("Message received with content: {}".format(msg.body))
            else:
                print("Did not received any message after 10 seconds")

            # stop agent from behaviour
            await self.agent.stop()

    async def setup(self):
        print("ReceiverAgent started")
        b = self.RecvBehav()
        template = Template()
        template.set_metadata("performative", "inform")
        self.add_behaviour(b, template)

async def main():

    receiveragent = ReceiverAgent("admin@localhost", "admin")
    await receiveragent.start(auto_register=True)
    print("Receiver started")

    await spade.wait_until_finished(receiveragent)
    print("Agents finished")


if __name__ == "__main__":
    spade.run(main())

-->Sender:

import spade
from spade.agent import Agent
from spade.behaviour import OneShotBehaviour
from spade.message import Message
from spade.template import Template


class SenderAgent(Agent):
    class InformBehav(OneShotBehaviour):
        async def run(self):
            print("InformBehav running")
            msg = Message(to="admin@localhost")     # Instantiate the message
            msg.set_metadata("performative", "inform")  # Set the "inform" FIPA performative
            msg.body = "Hello World"                    # Set the message content

            await self.send(msg)
            print("Message sent!")

            # stop agent from behaviour
            await self.agent.stop()

    async def setup(self):
        print("SenderAgent started")
        b = self.InformBehav()
        self.add_behaviour(b)


async def main():

    senderagent = SenderAgent("admin2@localhost", "admin")
    await senderagent.start(auto_register=True)
    print("Sender started")


if __name__ == "__main__":
    spade.run(main())

And a third file, Manaager, that has the following code in it:

async def main():

    receiveragent = ReceiverAgent("admin@localhost", "admin")
    await receiveragent.start(auto_register=True)
    print("Receiver started")

    senderagent = SenderAgent("admin2@localhost", "admin")
    await senderagent.start(auto_register=True)
    print("Sender started")


if __name__ == "__main__":
    spade.run(main())

If i run this Manager, it will work, send data and receive it. But what I wanted was to make the agents independetly, and have in their main function the start part, for example in receiver.py, I will add this at the end

    receiveragent = ReceiverAgent("admin@localhost", "admin")
    await receiveragent.start(auto_register=True)
    print("Receiver started")

By doing so, I don't need a manager to "manually" start it anymore, I can just run the python file. The problem is that when I try to do this, nothing is received(or sent?), just nothing happens. I tried for a few days, but nothing seems to work.

Does anyone have any idea how to do this? Thanks!

My ideas were that maybe there's a problem with ejabberd, but if it works with the exact same code when using the manager, I'm not sure. Another thing was the syncronization of the agents, but I do start receiver first, and later sender(changed timout, tried even the other way around). Debugging doesnt really helped, as there is no error, just nothing happens...

0

There are 0 best solutions below