annotate idavoll/backend.py @ 14:68e900b46d49

Send service-unavailable for unknown iq child namespaces
author Ralph Meijer <ralphm@ik.nu>
date Sat, 17 Jul 2004 22:03:37 +0000
parents 05a5d412e1b1
children 46cd13c68ac0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1 from twisted.application import service
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
2 from twisted.python import components, failure
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
3 from twisted.internet import defer, reactor
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
4
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5 class IBackendService(components.Interface):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6 """ Interface to a backend service of a pubsub service """
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
7
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
8 def do_publish(self, node, publisher, item):
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
9 """ Returns a deferred that returns """
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
10
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11 class BackendException(Exception):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
12 def __init__(self, msg = ''):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
13 self.msg = msg
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
14
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
15 def __str__(self):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
16 return self.msg
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
17
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
18 class NodeNotFound(BackendException):
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
19 def __init__(self, msg = 'Node not found'):
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
20 BackendException.__init__(self, msg)
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
21
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
22 class NotAuthorized(BackendException):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
23 pass
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
24
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
25 class PayloadExpected(BackendException):
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
26 def __init__(self, msg = 'Payload expected'):
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
27 BackendException.__init__(self, msg)
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
28
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
29 class NoPayloadAllowed(BackendException):
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
30 def __init__(self, msg = 'No payload allowed'):
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
31 BackendException.__init__(self, msg)
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
32
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
33 class MemoryBackendService(service.Service):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
34
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
35 __implements__ = IBackendService,
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
36
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
37 def __init__(self):
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
38 self.nodes = {
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
39 "ralphm/mood/ralphm@ik.nu": {
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
40 "persist_items": True,
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
41 "deliver_payloads": True,
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
42 }
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
43 }
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
44 self.subscribers = {
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
45 "ralphm/mood/ralphm@ik.nu": [
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
46 "notify@ik.nu/mood_monitor"
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
47 ]
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
48 }
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
49 self.affiliations = {
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
50 "ralphm/mood/ralphm@ik.nu": {
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
51 "ralphm@ik.nu": "owner",
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
52 "ralphm@doe.ik.nu": "publisher"
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
53 }
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
54 }
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
55
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
56 def do_publish(self, node, publisher, items):
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
57 try:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
58 try:
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
59 config = self.nodes[node]
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
60 persist_items = config["persist_items"]
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
61 deliver_payloads = config["deliver_payloads"]
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
62 except KeyError:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
63 raise NodeNotFound
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
64
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
65 try:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66 affiliation = self.affiliations[node][publisher]
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
67 if affiliation not in ['owner', 'publisher']:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
68 raise NotAuthorized
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
69 except KeyError:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
70 raise NotAuthorized()
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
71
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
72 # the following is under the assumption that the publisher
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
73 # has to provide an item when the node is persistent, but
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
74 # an empty notification is to be sent.
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
75
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
76 if items and not persist_items and not deliver_payloads:
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
77 raise NoPayloadAllowed
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
78 elif not items and (persist_items or deliver_payloads):
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
79 raise PayloadExpected
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
80
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
81 print "publish by %s to %s" % (publisher, node)
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
82
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
83 if persist_items or deliver_payloads:
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
84 for item in items:
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
85 if item["id"] is None:
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
86 item["id"] = 'random'
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
87
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
88 if persist_items:
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
89 self.storeItems(node, publisher, items)
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
90
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
91 if items and not deliver_payloads:
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
92 for item in items:
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
93 item.children = []
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
94
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
95 recipients = self.get_subscribers(node)
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
96 recipients.addCallback(self.magic_filter, node, items)
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
97 recipients.addCallback(self.pubsub_service.do_notification, node)
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
98
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
99 return defer.succeed(None)
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
100 except:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
101 f = failure.Failure()
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
102 return defer.fail(f)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
103
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
104 def magic_filter(self, subscribers, node, items):
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
105 list = {}
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
106 for subscriber in subscribers:
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
107 list[subscriber] = items
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
108
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
109 return list
2
9701df89c534 First take at notifications
Ralph Meijer <ralphm@ik.nu>
parents: 1
diff changeset
110
1
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
111 def get_subscribers(self, node):
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
112 d = defer.Deferred()
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
113 try:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
114 result = self.subscribers[node]
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
115 except:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
116 f = failure.Failure()
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
117 reactor.callLater(0, d.errback, f)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
118 else:
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
119 reactor.callLater(0, d.callback, result)
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
120
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
121 return d
4cc41776b7d7 Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
122
5
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
123 def storeItems(self, node, publisher, items):
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
124 for item in items:
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
125 print "Storing item %s" % item.toXml()
05a5d412e1b1 Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents: 2
diff changeset
126