Changeset 27:d62d7ea14995


Ignore:
Timestamp:
Jul 7, 2008, 4:39:05 PM (13 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@57
Message:

Implement SHIM support.

Author: ralphm.
Fixes #14.

This changes the signature of PubSubClient's itemsReceived and friends, to
have an object to represent the event, for easier addition of new information
from the event's message stanza, like these SHIM headers.

Location:
wokkel
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • wokkel/iwokkel.py

    r25 r27  
    138138class IPubSubClient(Interface):
    139139
    140     def itemsReceived(recipient, service, nodeIdentifier, items):
     140    def itemsReceived(event):
    141141        """
    142142        Called when an items notification has been received for a node.
     
    146146        accompanied with an item identifier in the C{id} attribute.
    147147
    148         @param recipient: The entity to which the notification was sent.
    149         @type recipient: L{jid.JID}
    150         @param service: The entity from which the notification was received.
    151         @type service: L{jid.JID}
    152         @param nodeIdentifier: Identifier of the node the items belong to.
    153         @type nodeIdentifier: C{unicode}
    154         @param items: List of received items as domish elements.
    155         @type items: C{list} of L{domish.Element}
    156         """
    157 
    158     def deleteReceived(recipient, service, nodeIdentifier):
     148        @param event: The items event.
     149        @type event: L{ItemsEvent<wokkel.pubsub.ItemsEvent>}
     150        """
     151
     152
     153    def deleteReceived(event):
    159154        """
    160155        Called when a deletion notification has been received for a node.
    161156
    162         @param recipient: The entity to which the notification was sent.
    163         @type recipient: L{jid.JID}
    164         @param service: The entity from which the notification was received.
    165         @type service: L{jid.JID}
    166         @param nodeIdentifier: Identifier of the node that has been deleted.
    167         @type nodeIdentifier: C{unicode}
    168         """
    169 
    170     def purgeReceived(recipient, service, nodeIdentifier):
     157        @param event: The items event.
     158        @type event: L{ItemsEvent<wokkel.pubsub.DeleteEvent>}
     159        """
     160
     161
     162    def purgeReceived(event):
    171163        """
    172164        Called when a purge notification has been received for a node.
     
    175167        considered retracted.
    176168
    177         @param recipient: The entity to which the notification was sent.
    178         @type recipient: L{jid.JID}
    179         @param service: The entity from which the notification was received.
    180         @type service: L{jid.JID}
    181         @param nodeIdentifier: Identifier of the node that has been purged.
    182         @type nodeIdentifier: C{unicode}
     169        @param event: The items event.
     170        @type event: L{ItemsEvent<wokkel.pubsub.PurgeEvent>}
    183171        """
    184172
  • wokkel/pubsub.py

    r25 r27  
    1717from twisted.words.xish import domish
    1818
    19 from wokkel import disco, data_form
     19from wokkel import disco, data_form, shim
    2020from wokkel.subprotocols import IQHandlerMixin, XMPPHandler
    2121from wokkel.iwokkel import IPubSubClient, IPubSubService
     
    190190
    191191
     192class PubSubEvent(object):
     193    """
     194    A publish subscribe event.
     195
     196    @param sender: The entity from which the notification was received.
     197    @type sender: L{jid.JID}
     198    @param recipient: The entity to which the notification was sent.
     199    @type recipient: L{wokkel.pubsub.ItemsEvent}
     200    @param nodeIdentifier: Identifier of the node the event pertains to.
     201    @type nodeIdentifier: C{unicode}
     202    @param headers: SHIM headers, see L{wokkel.shim.extractHeaders}.
     203    @type headers: L{dict}
     204    """
     205
     206    def __init__(self, sender, recipient, nodeIdentifier, headers):
     207        self.sender = sender
     208        self.recipient = recipient
     209        self.nodeIdentifier = nodeIdentifier
     210        self.headers = headers
     211
     212
     213
     214class ItemsEvent(PubSubEvent):
     215    """
     216    A publish-subscribe event that signifies new, updated and retracted items.
     217
     218    @param items: List of received items as domish elements.
     219    @type items: C{list} of L{domish.Element}
     220    """
     221
     222    def __init__(self, sender, recipient, nodeIdentifier, items, headers):
     223        PubSubEvent.__init__(self, sender, recipient, nodeIdentifier, headers)
     224        self.items = items
     225
     226
     227
     228class DeleteEvent(PubSubEvent):
     229    """
     230    A publish-subscribe event that signifies the deletion of a node.
     231    """
     232
     233
     234
     235class PurgeEvent(PubSubEvent):
     236    """
     237    A publish-subscribe event that signifies the purging of a node.
     238    """
     239
     240
     241
    192242class PubSubClient(XMPPHandler):
    193243    """
     
    204254    def _onEvent(self, message):
    205255        try:
    206             service = jid.JID(message["from"])
     256            sender = jid.JID(message["from"])
    207257            recipient = jid.JID(message["to"])
    208258        except KeyError:
     
    220270
    221271        if eventHandler:
    222             eventHandler(service, recipient, actionElement)
     272            headers = shim.extractHeaders(message)
     273            eventHandler(sender, recipient, actionElement, headers)
    223274            message.handled = True
    224275
    225     def _onEvent_items(self, service, recipient, action):
     276    def _onEvent_items(self, sender, recipient, action, headers):
    226277        nodeIdentifier = action["node"]
    227278
     
    229280                         if element.name in ('item', 'retract')]
    230281
    231         self.itemsReceived(recipient, service, nodeIdentifier, items)
    232 
    233     def _onEvent_delete(self, service, recipient, action):
     282        event = ItemsEvent(sender, recipient, nodeIdentifier, items, headers)
     283        self.itemsReceived(event)
     284
     285    def _onEvent_delete(self, sender, recipient, action, headers):
    234286        nodeIdentifier = action["node"]
    235         self.deleteReceived(recipient, service, nodeIdentifier)
    236 
    237     def _onEvent_purge(self, service, recipient, action):
     287        event = DeleteEvent(sender, recipient, nodeIdentifier, headers)
     288        self.deleteReceived(event)
     289
     290    def _onEvent_purge(self, sender, recipient, action, headers):
    238291        nodeIdentifier = action["node"]
    239         self.purgeReceived(recipient, service, nodeIdentifier)
    240 
    241     def itemsReceived(self, recipient, service, nodeIdentifier, items):
     292        event = PurgeEvent(sender, recipient, nodeIdentifier, headers)
     293        self.purgeReceived(event)
     294
     295    def itemsReceived(self, event):
    242296        pass
    243297
    244     def deleteReceived(self, recipient, service, nodeIdentifier):
     298    def deleteReceived(self, event):
    245299        pass
    246300
    247     def purgeReceived(self, recipient, service, nodeIdentifier):
     301    def purgeReceived(self, event):
    248302        pass
    249303
  • wokkel/test/test_pubsub.py

    r25 r27  
    8080        item3['id'] = 'item3'
    8181
    82         def itemsReceived(recipient, service, nodeIdentifier, items):
    83             self.assertEquals(JID('user@example.org/home'), recipient)
    84             self.assertEquals(JID('pubsub.example.org'), service)
    85             self.assertEquals('test', nodeIdentifier)
    86             self.assertEquals([item1, item2, item3], items)
     82        def itemsReceived(event):
     83            self.assertEquals(JID('user@example.org/home'), event.recipient)
     84            self.assertEquals(JID('pubsub.example.org'), event.sender)
     85            self.assertEquals('test', event.nodeIdentifier)
     86            self.assertEquals([item1, item2, item3], event.items)
    8787
    8888        d, self.protocol.itemsReceived = calledAsync(itemsReceived)
     
    102102        items['node'] = 'test'
    103103
    104         def deleteReceived(recipient, service, nodeIdentifier):
    105             self.assertEquals(JID('user@example.org/home'), recipient)
    106             self.assertEquals(JID('pubsub.example.org'), service)
    107             self.assertEquals('test', nodeIdentifier)
     104        def deleteReceived(event):
     105            self.assertEquals(JID('user@example.org/home'), event.recipient)
     106            self.assertEquals(JID('pubsub.example.org'), event.sender)
     107            self.assertEquals('test', event.nodeIdentifier)
    108108
    109109        d, self.protocol.deleteReceived = calledAsync(deleteReceived)
     
    123123        items['node'] = 'test'
    124124
    125         def purgeReceived(recipient, service, nodeIdentifier):
    126             self.assertEquals(JID('user@example.org/home'), recipient)
    127             self.assertEquals(JID('pubsub.example.org'), service)
    128             self.assertEquals('test', nodeIdentifier)
     125        def purgeReceived(event):
     126            self.assertEquals(JID('user@example.org/home'), event.recipient)
     127            self.assertEquals(JID('pubsub.example.org'), event.sender)
     128            self.assertEquals('test', event.nodeIdentifier)
    129129
    130130        d, self.protocol.purgeReceived = calledAsync(purgeReceived)
Note: See TracChangeset for help on using the changeset viewer.