Mercurial > libervia-pubsub
diff idavoll/backend.py @ 5:05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Implement configuration of persistence and sending payloads in the
notifications or not.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Sun, 27 Jun 2004 14:09:17 +0000 |
parents | 9701df89c534 |
children | 46cd13c68ac0 |
line wrap: on
line diff
--- a/idavoll/backend.py Sun Jun 27 14:07:12 2004 +0000 +++ b/idavoll/backend.py Sun Jun 27 14:09:17 2004 +0000 @@ -16,26 +16,49 @@ return self.msg class NodeNotFound(BackendException): - #def __init__(self, msg = 'Node not found'): - # BackendException.__init__(self, msg) - pass + def __init__(self, msg = 'Node not found'): + BackendException.__init__(self, msg) class NotAuthorized(BackendException): pass +class PayloadExpected(BackendException): + def __init__(self, msg = 'Payload expected'): + BackendException.__init__(self, msg) + +class NoPayloadAllowed(BackendException): + def __init__(self, msg = 'No payload allowed'): + BackendException.__init__(self, msg) + class MemoryBackendService(service.Service): __implements__ = IBackendService, def __init__(self): - self.nodes = {"ralphm/test": 'test'} - self.subscribers = {"ralphm/test": ["ralphm@ik.nu", "ralphm@doe.ik.nu"] } - self.affiliations = {"ralphm/test": { "ralphm@ik.nu": "owner", "ralphm@se-135.se.wtb.tue.nl": 'publisher', 'ralphm@doe.ik.nu': 'publisher' } } + self.nodes = { + "ralphm/mood/ralphm@ik.nu": { + "persist_items": True, + "deliver_payloads": True, + } + } + self.subscribers = { + "ralphm/mood/ralphm@ik.nu": [ + "notify@ik.nu/mood_monitor" + ] + } + self.affiliations = { + "ralphm/mood/ralphm@ik.nu": { + "ralphm@ik.nu": "owner", + "ralphm@doe.ik.nu": "publisher" + } + } - def do_publish(self, node, publisher, item): + def do_publish(self, node, publisher, items): try: try: - result = self.nodes[node] + config = self.nodes[node] + persist_items = config["persist_items"] + deliver_payloads = config["deliver_payloads"] except KeyError: raise NodeNotFound @@ -46,19 +69,44 @@ except KeyError: raise NotAuthorized() + # the following is under the assumption that the publisher + # has to provide an item when the node is persistent, but + # an empty notification is to be sent. + + if items and not persist_items and not deliver_payloads: + raise NoPayloadAllowed + elif not items and (persist_items or deliver_payloads): + raise PayloadExpected + print "publish by %s to %s" % (publisher, node) + if persist_items or deliver_payloads: + for item in items: + if item["id"] is None: + item["id"] = 'random' + + if persist_items: + self.storeItems(node, publisher, items) + + if items and not deliver_payloads: + for item in items: + item.children = [] + recipients = self.get_subscribers(node) - recipients.addCallback(self.magic_filter, node, item) - recipients.addCallback(self.pubsub_service.do_notification, node, item) + recipients.addCallback(self.magic_filter, node, items) + recipients.addCallback(self.pubsub_service.do_notification, node) - return defer.succeed(result) + return defer.succeed(None) except: f = failure.Failure() return defer.fail(f) - def magic_filter(self, subscribers, node, item): - return subscribers + def magic_filter(self, subscribers, node, items): + list = {} + for subscriber in subscribers: + list[subscriber] = items + + return list def get_subscribers(self, node): d = defer.Deferred() @@ -72,3 +120,7 @@ return d + def storeItems(self, node, publisher, items): + for item in items: + print "Storing item %s" % item.toXml() +