Created
November 2, 2017 19:03
-
-
Save javipalanca/29d9474531ac8c7fd7262cdcd6472ddc to your computer and use it in GitHub Desktop.
presence test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import time | |
import getpass | |
from spade.agent import Agent | |
from spade.behaviour import OneShotBehaviour | |
import logging | |
logging.basicConfig(level=logging.DEBUG) | |
def on_available(jid, stanza): | |
print("Agent {} is available.".format(jid)) | |
class Agent1(Agent): | |
def setup(self): | |
self.add_behaviour(self.Behav1()) | |
class Behav1(OneShotBehaviour): | |
def on_subscribed(self, jid): | |
print("Agent {} has accepted the subscription.".format(jid)) | |
async def run(self): | |
self.presence.on_subscribed = self.on_subscribed | |
await asyncio.sleep(5) | |
self.presence.subscribe(jid2) | |
class Agent2(Agent): | |
def setup(self): | |
self.add_behaviour(self.Behav2()) | |
class Behav2(OneShotBehaviour): | |
def on_subscribe(self, jid): | |
print("Agent {} asked for subscription. Let's aprove it.".format(jid)) | |
self.presence.approve(jid) | |
def on_subscribed(self, jid): | |
print("Agent {} has accepted the subscription.".format(jid)) | |
async def run(self): | |
self.presence.on_subscribe = self.on_subscribe | |
await asyncio.sleep(20) | |
if __name__ == "__main__": | |
jid1 = input("Enter JID of agent1: ") | |
passwd1 = getpass.getpass(prompt='Password of agent1: ') | |
jid2 = input("Enter JID of agent2: ") | |
passwd2 = getpass.getpass(prompt='Password of agent2: ') | |
agent2 = Agent2(jid2, passwd2) | |
agent1 = Agent1(jid1, passwd1) | |
while True: | |
try: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
break | |
agent1.stop() | |
agent2.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment