# HG changeset patch # User Ralph Meijer # Date 1088345357 0 # Node ID 05a5d412e1b164482db0c5f9f927ce7a98edc040 # Parent ea195dc1732d449bacefa42e756fa4c7aef73c73 Added PayloadExpected and NoPayloadAllowed exceptions. Implement configuration of persistence and sending payloads in the notifications or not. diff -r ea195dc1732d -r 05a5d412e1b1 idavoll/backend.py --- 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() +