source: ralphm-patches/pubsub_resource_example.patch @ 83:255aae0cf8c5

Last change on this file since 83:255aae0cf8c5 was 49:537d1413b661, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Save work after moving stuff to keep from wokkel.xmppim to wokkel.im.

File size: 2.3 KB
  • new file doc/examples/pubsub_resource.tac

    # HG changeset patch
    # Parent 2c8dc93fbef4f2d5b7115c7afdd1b7fb799d47c3
    
    diff -r 2c8dc93fbef4 doc/examples/pubsub_resource.tac
    - +  
     1from twisted.application import service
     2from twisted.words.protocols.jabber.error import StanzaError
     3from wokkel import pubsub
     4
     5class CounterService(service.Service):
     6
     7    pubsubService = None
     8
     9    def __init__(self):
     10        self.counters = {}
     11        self.lc = None
     12
     13
     14    def startService(self):
     15        self.lc = task.LoopingCall(self.updateCounters)
     16        self.lc.start(1)
     17
     18
     19    def updateCounters(self):
     20        for key in self.counters:
     21            self.counters[key] += 1
     22            self.pubsubService.publishCounters(self.counters)
     23
     24
     25class Backend(pubsub.PubSubResource):
     26    """
     27    Business logic for XMPP Publish-Subscribe nodes.
     28    """
     29
     30    features = [
     31            "subscribe",
     32            ]
     33
     34    discoIdentity = disco.DiscoIdentity('pubsub', 'service',
     35                                        'Example publish-subscribe service')
     36
     37    def __init__(self):
     38       self.nodes = {}
     39       self.subscriptions = {}
     40
     41    def subscribe(self, request):
     42        if request.nodeIdentifier == "":
     43            raise StanzaError('forbidden')
     44
     45        # TODO: check subscriber against sender
     46
     47        # Get or create subscription
     48        try:
     49            nodeSubscriptions = self.subscriptions[request.nodeIdentifier]
     50        except KeyError:
     51            nodeSubscriptions = {}
     52            self.subscriptions[request.nodeIdentifier] = nodeSubscriptions
     53
     54        try:
     55            subscription = nodeSubscriptions[request.subscriber]
     56        except:
     57            subscription = pubsub.Subscription(request.nodeIdentifier,
     58                                               request.subscriber,
     59                                               'subscribed')
     60            nodeSubscriptions[request.subscriber] = subscription
     61
     62        # Initialize the node if it does not exist yet.
     63        if request.nodeIdentifier not in self.nodes:
     64            self.nodes[request.nodeIdentifier] = 0
     65
     66        return subscription
     67
     68
     69    def publishCounters(self, counters):
     70        for key in counters:
     71            if key in self.subscriptions(
     72
Note: See TracBrowser for help on using the repository browser.