http://code.google.com/p/spade2/source/detail?r=2232
Modified:
/trunk/examples/unittests/pubsubTestCase.py
/trunk/spade/Agent.py
/trunk/spade/Behaviour.py
=======================================
--- /trunk/examples/unittests/pubsubTestCase.py Fri Jul 16 10:41:53 2010
+++ /trunk/examples/unittests/pubsubTestCase.py Tue Jul 20 08:35:54 2010
@@ -10,6 +10,12 @@
host = "127.0.0.1"
+class SubscribeBehaviour(spade.Behaviour.EventBehaviour):
+
+ def _process(self):
+ msg = self._receive(True)
+ self.myAgent.eventmsg = msg
+
class PubSubTestCase(unittest.TestCase):
def setUp(self):
@@ -74,13 +80,25 @@
result = self.a.createEvent("ExistsNode")
self.assertEqual(result, ('ok', ['ExistsNode']))
- result = self.b.subscribeToEvent("ExistsNode")
+ result = self.b.subscribeToEvent("ExistsNode",
SubscribeBehaviour())
self.assertEqual( result, ('ok', []) )
+ #TODO: Check that the last published item is sent after
subscription.
+
+ self.b.eventmsg = None
self.a.publishEvent('ExistsNode', Node(tag='foo'))
- #TODO: Check that the last published item is sent after
subscription.
-
- #TODO: Check that the new item published by Romeo is received too.
+
+ import time
+ time.sleep(3) #wait for the event
+ #Check that the event is received in the callback
+ self.assertNotEqual(self.b.eventmsg,None)
+
+ n = self.b.eventmsg.T.event.T.items.T.item
+ self.assertNotEqual(n.getTag("foo"),None)
+
+ if not "a@"+host in n.getAttr("publisher"): self.fail("Wrong
publisher")
+
+ #TODO: Check that the new item published by 'a' is received too.
self.b.unsubscribeFromEvent("ExistsNode")
self.a.deleteEvent("ExistsNode")
@@ -120,11 +138,29 @@
self.assertEqual( result, ('ok', [])) # OK
#TODO: Check that the last published item is sent after
subscription.
+ def testNotEventBehaviour(self):
+
+ class Behav(spade.Behaviour.Behaviour): pass
+ self.a.deleteEvent("ExistsNode")
+ self.b.deleteEvent("ExistsNode")
+ self.a.createEvent("ExistsNode")
+ result = self.b.subscribeToEvent("ExistsNode",Behav())
+ self.assertEqual(result, ("error",["not-event-behaviour"]))
if __name__ == "__main__":
unittest.main()
+ sys.exit()
+
+ suite = unittest.TestSuite()
+ suite.addTest(PubSubTestCase('testPublishEvent'))
+ result = unittest.TestResult()
+
+ suite.run(result)
+ for f in result.failures:
+ print f[0]
+ print f[1]
=======================================
--- /trunk/spade/Agent.py Fri Jul 16 10:41:53 2010
+++ /trunk/spade/Agent.py Tue Jul 20 08:35:54 2010
@@ -120,6 +120,7 @@
self._unsubscribeHandler = lambda frm,typ,stat,show: False
self._pubsub = pubsub.PubSub(self)
+ self._events = {}
self._waitingForRoster = False # Indicates that a request for the
roster is in progress
@@ -1522,10 +1523,24 @@
####################
def publishEvent(self, name, event):
return self._pubsub.publish(name,event)
- def subscribeToEvent(self, name, server=None,jid=None):
- return self._pubsub.subscribe(name,server,jid)
+ def subscribeToEvent(self, name, behaviour=None, server=None,jid=None):
+ r = self._pubsub.subscribe(name,server,jid)
+ if r[0]=='ok' and behaviour!=None:
+ if not issubclass(behaviour.__class__,
Behaviour.EventBehaviour):
+ self.DEBUG("Behaviour MUST be an EventBehaviour to
subscribe to events.","error","pubsub")
+ return ("error",["not-event-behaviour"])
+ self._events[name]=behaviour
+ n = xmpp.Node(node='<message xmlns="jabber:client"><event
xmlns="http://jabber.org/protocol/pubsub#events"><items node="'+name+'"
/></event></message>')
+ template = xmpp.Message(node=n)
+ mt = Behaviour.MessageTemplate(template)
+ self.addBehaviour(behaviour,mt)
+ return r
def unsubscribeFromEvent(self, name,server=None,jid=None):
- return self._pubsub.unsubscribe(name,server,jid)
+ r = self._pubsub.unsubscribe(name,server,jid)
+ if name in self._events.keys():
+ self.removeBehaviour(self._events[name])
+ del self._events[name]
+ return r
def createEvent(self, name, server=None, type='leaf', parent=None,
access=None):
return self._pubsub.createNode(name, server=None, type='leaf',
parent=None, access=None)
def deleteEvent(self, name, server=None):
=======================================
--- /trunk/spade/Behaviour.py Wed Jul 14 06:10:54 2010
+++ /trunk/spade/Behaviour.py Tue Jul 20 08:35:54 2010
@@ -208,18 +208,9 @@
if type(Template) == types.InstanceType:
if Template.__class__ == ACLTemplate:
self.match = self.acl_match
- elif Template.__class__ == Message:
- self.match = self.node_match
- elif Template.__class__ == Presence:
- self.match = self.node_match
- elif Template.__class__ == Iq:
- self.match = self.node_match
- elif Template.__class__ == Node:
- self.match = self.node_match
else:
# Default template option
self.match = self.node_match
- print "MATCH: DEFAULT OPTION SHOULD NOT BE CALLED"
self.template = copy.copy(Template)