[56] | 1 | # HG changeset patch |
---|
| 2 | # Parent bbb746f7971831cb09e1539bfd3fefe862c3cc9e |
---|
| 3 | |
---|
| 4 | diff -r bbb746f79718 doc/examples/pubsub_client_create_node.tac |
---|
| 5 | --- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
---|
| 6 | +++ b/doc/examples/pubsub_client_create_node.tac Mon Nov 21 23:14:09 2011 +0100 |
---|
| 7 | @@ -0,0 +1,56 @@ |
---|
| 8 | +""" |
---|
| 9 | +An XMPP Publish Subscribe publisher client as an XMPP client. |
---|
| 10 | + |
---|
| 11 | +This client logs in as C{publisher@example.org}, creates a node at |
---|
| 12 | +C{pubsub.example.org} and prints the node identifier. |
---|
| 13 | + |
---|
| 14 | +Note that some services may change the node identifier to adhere to local |
---|
| 15 | +policies. If you set L{NODE_IDENTIFIER} to C{None}, the service will create an |
---|
| 16 | +'instant node', with a unique node identifier. |
---|
| 17 | +""" |
---|
| 18 | + |
---|
| 19 | +from twisted.application import service |
---|
| 20 | +from twisted.internet import reactor, task |
---|
| 21 | +from twisted.python import log |
---|
| 22 | +from twisted.words.protocols.jabber.jid import JID |
---|
| 23 | +from wokkel.client import XMPPClient |
---|
| 24 | +from wokkel.pubsub import PubSubClient |
---|
| 25 | + |
---|
| 26 | +# Configuration parameters |
---|
| 27 | + |
---|
| 28 | +THIS_JID = JID('publisher@example.org') |
---|
| 29 | +SERVICE_JID = JID('pubsub.example.org') |
---|
| 30 | +NODE_IDENTIFIER = 'test_node' |
---|
| 31 | +SECRET = 'secret' |
---|
| 32 | +LOG_TRAFFIC = True |
---|
| 33 | + |
---|
| 34 | +class Publisher(PubSubClient): |
---|
| 35 | + |
---|
| 36 | + count = None |
---|
| 37 | + loopingCall = None |
---|
| 38 | + nodeIdentifier = None |
---|
| 39 | + |
---|
| 40 | + def connectionInitialized(self): |
---|
| 41 | + def cb(nodeIdentifier): |
---|
| 42 | + log.msg("The node %r was successfully created" % nodeIdentifier) |
---|
| 43 | + d = self.createNode(SERVICE_JID, NODE_IDENTIFIER) |
---|
| 44 | + d.addCallbacks(cb, log.err) |
---|
| 45 | + d.addCallback(lambda _: reactor.stop()) |
---|
| 46 | + |
---|
| 47 | + |
---|
| 48 | + def nodeCreated(self, nodeIdentifier): |
---|
| 49 | + self.nodeIdentifier = nodeIdentifier |
---|
| 50 | + self.count = 0 |
---|
| 51 | + self.loopingCall = task.LoopingCall(self.doPublish) |
---|
| 52 | + self.loopingCall.start(5) |
---|
| 53 | + |
---|
| 54 | +# Set up the Twisted application |
---|
| 55 | + |
---|
| 56 | +application = service.Application("Publish Subscribe publisher") |
---|
| 57 | + |
---|
| 58 | +client = XMPPClient(THIS_JID, SECRET) |
---|
| 59 | +client.logTraffic = LOG_TRAFFIC |
---|
| 60 | +client.setServiceParent(application) |
---|
| 61 | + |
---|
| 62 | +pubsubHandler = PubSubClient() |
---|
| 63 | +pubsubHandler.setHandlerParent(client) |
---|