Mercurial > libervia-pubsub
comparison 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 |
comparison
equal
deleted
inserted
replaced
4:ea195dc1732d | 5:05a5d412e1b1 |
---|---|
14 | 14 |
15 def __str__(self): | 15 def __str__(self): |
16 return self.msg | 16 return self.msg |
17 | 17 |
18 class NodeNotFound(BackendException): | 18 class NodeNotFound(BackendException): |
19 #def __init__(self, msg = 'Node not found'): | 19 def __init__(self, msg = 'Node not found'): |
20 # BackendException.__init__(self, msg) | 20 BackendException.__init__(self, msg) |
21 pass | |
22 | 21 |
23 class NotAuthorized(BackendException): | 22 class NotAuthorized(BackendException): |
24 pass | 23 pass |
24 | |
25 class PayloadExpected(BackendException): | |
26 def __init__(self, msg = 'Payload expected'): | |
27 BackendException.__init__(self, msg) | |
28 | |
29 class NoPayloadAllowed(BackendException): | |
30 def __init__(self, msg = 'No payload allowed'): | |
31 BackendException.__init__(self, msg) | |
25 | 32 |
26 class MemoryBackendService(service.Service): | 33 class MemoryBackendService(service.Service): |
27 | 34 |
28 __implements__ = IBackendService, | 35 __implements__ = IBackendService, |
29 | 36 |
30 def __init__(self): | 37 def __init__(self): |
31 self.nodes = {"ralphm/test": 'test'} | 38 self.nodes = { |
32 self.subscribers = {"ralphm/test": ["ralphm@ik.nu", "ralphm@doe.ik.nu"] } | 39 "ralphm/mood/ralphm@ik.nu": { |
33 self.affiliations = {"ralphm/test": { "ralphm@ik.nu": "owner", "ralphm@se-135.se.wtb.tue.nl": 'publisher', 'ralphm@doe.ik.nu': 'publisher' } } | 40 "persist_items": True, |
41 "deliver_payloads": True, | |
42 } | |
43 } | |
44 self.subscribers = { | |
45 "ralphm/mood/ralphm@ik.nu": [ | |
46 "notify@ik.nu/mood_monitor" | |
47 ] | |
48 } | |
49 self.affiliations = { | |
50 "ralphm/mood/ralphm@ik.nu": { | |
51 "ralphm@ik.nu": "owner", | |
52 "ralphm@doe.ik.nu": "publisher" | |
53 } | |
54 } | |
34 | 55 |
35 def do_publish(self, node, publisher, item): | 56 def do_publish(self, node, publisher, items): |
36 try: | 57 try: |
37 try: | 58 try: |
38 result = self.nodes[node] | 59 config = self.nodes[node] |
60 persist_items = config["persist_items"] | |
61 deliver_payloads = config["deliver_payloads"] | |
39 except KeyError: | 62 except KeyError: |
40 raise NodeNotFound | 63 raise NodeNotFound |
41 | 64 |
42 try: | 65 try: |
43 affiliation = self.affiliations[node][publisher] | 66 affiliation = self.affiliations[node][publisher] |
44 if affiliation not in ['owner', 'publisher']: | 67 if affiliation not in ['owner', 'publisher']: |
45 raise NotAuthorized | 68 raise NotAuthorized |
46 except KeyError: | 69 except KeyError: |
47 raise NotAuthorized() | 70 raise NotAuthorized() |
48 | 71 |
72 # the following is under the assumption that the publisher | |
73 # has to provide an item when the node is persistent, but | |
74 # an empty notification is to be sent. | |
75 | |
76 if items and not persist_items and not deliver_payloads: | |
77 raise NoPayloadAllowed | |
78 elif not items and (persist_items or deliver_payloads): | |
79 raise PayloadExpected | |
80 | |
49 print "publish by %s to %s" % (publisher, node) | 81 print "publish by %s to %s" % (publisher, node) |
50 | 82 |
83 if persist_items or deliver_payloads: | |
84 for item in items: | |
85 if item["id"] is None: | |
86 item["id"] = 'random' | |
87 | |
88 if persist_items: | |
89 self.storeItems(node, publisher, items) | |
90 | |
91 if items and not deliver_payloads: | |
92 for item in items: | |
93 item.children = [] | |
94 | |
51 recipients = self.get_subscribers(node) | 95 recipients = self.get_subscribers(node) |
52 recipients.addCallback(self.magic_filter, node, item) | 96 recipients.addCallback(self.magic_filter, node, items) |
53 recipients.addCallback(self.pubsub_service.do_notification, node, item) | 97 recipients.addCallback(self.pubsub_service.do_notification, node) |
54 | 98 |
55 return defer.succeed(result) | 99 return defer.succeed(None) |
56 except: | 100 except: |
57 f = failure.Failure() | 101 f = failure.Failure() |
58 return defer.fail(f) | 102 return defer.fail(f) |
59 | 103 |
60 def magic_filter(self, subscribers, node, item): | 104 def magic_filter(self, subscribers, node, items): |
61 return subscribers | 105 list = {} |
106 for subscriber in subscribers: | |
107 list[subscriber] = items | |
108 | |
109 return list | |
62 | 110 |
63 def get_subscribers(self, node): | 111 def get_subscribers(self, node): |
64 d = defer.Deferred() | 112 d = defer.Deferred() |
65 try: | 113 try: |
66 result = self.subscribers[node] | 114 result = self.subscribers[node] |
70 else: | 118 else: |
71 reactor.callLater(0, d.callback, result) | 119 reactor.callLater(0, d.callback, result) |
72 | 120 |
73 return d | 121 return d |
74 | 122 |
123 def storeItems(self, node, publisher, items): | |
124 for item in items: | |
125 print "Storing item %s" % item.toXml() | |
126 |