javipalanca / spade

Smart Python Agent Development Environment
MIT License
245 stars 98 forks source link

Unsubscribe #112

Open BrunoPGNatal opened 1 year ago

BrunoPGNatal commented 1 year ago

Description

I'm using the SPADE framework to automate the organization of mats. However, when I need to remove a transporter from the system, it's necessary to unsubscribe agents from the contact list. Unfortunately, I haven't been able to successfully unregister them using SPADE. I haven't found any method that addresses this particular requirement.

What I Did

I've developed a behavior that monitors incoming requests for subscription or unsubscription from the contact list. While the subscription part works smoothly, I'm facing issues with the unsubscription process.

class ConveyorBehCyclic(CyclicBehaviour):

def __int__(self):
    self._state = None

def on_subscribed(self, jid):
    print(f'The agent {self.agent.name} accepted the subscription from {jid.split("@")[0]}')

def on_unsubscribe(self, jid):
    print(f'The agent {jid.split("@")[0]} wasts to unsubscribe in the list of contacts')
    self.presence.approve(jid)

def on_subscribe(self, jid):
    print(f'The agent {jid.split("@")[0]} wants to subscribe in the list of contacts')
    self.presence.approve(jid)

async def on_start(self) -> None:
    print('register started')
    self._state = True  # Variable responsible to register once in contact list

async def on_end(self) -> None:
    print('register ended')

async def run(self) -> None:

    self.presence.on_subscribe = self.on_subscribe
    self.presence.on_subscribed = self.on_subscribed
    self.presence.on_unsubscribe = self.on_unsubscribe

    if self._state:
        with open("Jid's.txt", "r") as file:
            lines = file.readlines()

        for line in lines:
            jid = line.strip()
            if "JID" != jid:
                try:
                    self.presence.subscribe(jid)
                    print(f'Agent - [{self.agent.name}] - Sent a message register to [{jid}] ')
                except Exception as e:
                    print(f'Behaviour unexpected, cause {e}')

        self._state = False
    print('Register behaviour checked')

    await asyncio.sleep(10)
0x1F602 commented 1 year ago

I've been having an issue with the auto accept subscribe code, wonder if there was a breaking change. On top of that my agents are holding onto their presence as available even after killing it.