Mercurial > libervia-pubsub
annotate idavoll/backend.py @ 15:46cd13c68ac0
Redone memory storage of nodes.
Created a few classes for memory storage of nodes.
Implemented basic subscription.
Implemented item storage.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Sat, 17 Jul 2004 22:05:50 +0000 |
parents | 05a5d412e1b1 |
children | 7937d6fbbe2a |
rev | line source |
---|---|
1 | 1 from twisted.application import service |
2 from twisted.python import components, failure | |
3 from twisted.internet import defer, reactor | |
15 | 4 from twisted.protocols.jabber import jid |
1 | 5 |
6 class IBackendService(components.Interface): | |
7 """ Interface to a backend service of a pubsub service """ | |
8 | |
2 | 9 def do_publish(self, node, publisher, item): |
10 """ Returns a deferred that returns """ | |
11 | |
1 | 12 class BackendException(Exception): |
13 def __init__(self, msg = ''): | |
14 self.msg = msg | |
15 | |
16 def __str__(self): | |
17 return self.msg | |
18 | |
19 class NodeNotFound(BackendException): | |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
20 def __init__(self, msg = 'Node not found'): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
21 BackendException.__init__(self, msg) |
1 | 22 |
23 class NotAuthorized(BackendException): | |
24 pass | |
25 | |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
26 class PayloadExpected(BackendException): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
27 def __init__(self, msg = 'Payload expected'): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
28 BackendException.__init__(self, msg) |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
29 |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
30 class NoPayloadAllowed(BackendException): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
31 def __init__(self, msg = 'No payload allowed'): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
32 BackendException.__init__(self, msg) |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
33 |
15 | 34 class Subscription: |
35 def __init__(self, state): | |
36 self.state = state | |
37 | |
38 class NodeConfiguration: | |
39 def __init__(self): | |
40 self.persist_items = False | |
41 self.deliver_payloads = False | |
42 | |
43 class Node: | |
44 def __init__(self, name): | |
45 self.name = name | |
46 self.configuration = NodeConfiguration() | |
47 self.subscriptions = {} | |
48 self.affiliations = {} | |
49 self.items = {} | |
50 | |
1 | 51 class MemoryBackendService(service.Service): |
52 | |
53 __implements__ = IBackendService, | |
54 | |
55 def __init__(self): | |
15 | 56 self.nodes = {} |
1 | 57 |
15 | 58 node = Node("ralphm/mood/ralphm@ik.nu") |
59 node.subscriptions["ralphm@doe.ik.nu"] = Subscription("subscribed") | |
60 node.affiliations["ralphm@ik.nu"] = "owner" | |
61 node.affiliations["ralphm@doe.ik.nu"] = "publisher" | |
62 node.configuration.persist_items = True | |
63 node.configuration.deliver_payloads = True | |
64 self.nodes[node.name] = node | |
65 | |
66 def do_publish(self, node_id, publisher, items): | |
1 | 67 try: |
68 try: | |
15 | 69 node = self.nodes[node_id] |
70 persist_items = node.configuration.persist_items | |
71 deliver_payloads = node.configuration.deliver_payloads | |
1 | 72 except KeyError: |
73 raise NodeNotFound | |
74 | |
75 try: | |
15 | 76 if node.affiliations[publisher] not in ['owner', 'publisher']: |
1 | 77 raise NotAuthorized |
78 except KeyError: | |
79 raise NotAuthorized() | |
2 | 80 |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
81 # the following is under the assumption that the publisher |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
82 # 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
|
83 # an empty notification is to be sent. |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
84 |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
85 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
|
86 raise NoPayloadAllowed |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
87 elif not items and (persist_items or deliver_payloads): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
88 raise PayloadExpected |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
89 |
15 | 90 print "publish by %s to %s" % (publisher, node_id) |
2 | 91 |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
92 if persist_items or deliver_payloads: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
93 for item in items: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
94 if item["id"] is None: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
95 item["id"] = 'random' |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
96 |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
97 if persist_items: |
15 | 98 self.storeItems(node_id, publisher, items) |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
99 |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
100 if items and not deliver_payloads: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
101 for item in items: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
102 item.children = [] |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
103 |
15 | 104 recipients = self.get_subscribers(node_id) |
105 recipients.addCallback(self.magic_filter, node_id, items) | |
106 recipients.addCallback(self.pubsub_service.do_notification, node_id) | |
2 | 107 |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
108 return defer.succeed(None) |
1 | 109 except: |
110 f = failure.Failure() | |
111 return defer.fail(f) | |
112 | |
15 | 113 def do_subscribe(self, node_id, subscriber, requestor): |
114 # expect subscriber and requestor to be a jid.JID | |
115 try: | |
116 try: | |
117 node = self.nodes[node_id] | |
118 except KeyError: | |
119 raise NodeNotFound | |
120 | |
121 affiliation = node.affiliations.get(requestor.full(), 'none') | |
122 | |
123 if affiliation == 'banned': | |
124 raise NotAuthorized | |
125 | |
126 print subscriber.full() | |
127 print subscriber.userhostJID().full() | |
128 print requestor.full() | |
129 | |
130 if subscriber.userhostJID() != requestor: | |
131 raise NotAuthorized | |
132 | |
133 try: | |
134 subscription = node.subscriptions[subscriber.full()] | |
135 except KeyError: | |
136 subscription = Subscription('subscribed') | |
137 node.subscriptions[subscriber.full()] = subscription | |
138 | |
139 print node.subscriptions | |
140 | |
141 return defer.succeed({ | |
142 'affiliation': affiliation, | |
143 'node': node_id, | |
144 'jid': subscriber, | |
145 'subscription': subscription.state}) | |
146 except: | |
147 f = failure.Failure() | |
148 return defer.fail(f) | |
149 | |
150 | |
151 def magic_filter(self, subscribers, node_id, items): | |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
152 list = {} |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
153 for subscriber in subscribers: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
154 list[subscriber] = items |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
155 |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
156 return list |
2 | 157 |
15 | 158 def get_subscribers(self, node_id): |
1 | 159 d = defer.Deferred() |
160 try: | |
15 | 161 result = self.nodes[node_id].subscriptions.keys() |
1 | 162 except: |
163 f = failure.Failure() | |
164 reactor.callLater(0, d.errback, f) | |
165 else: | |
166 reactor.callLater(0, d.callback, result) | |
167 | |
168 return d | |
169 | |
15 | 170 def storeItems(self, node_id, publisher, items): |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
171 for item in items: |
15 | 172 self.nodes[node_id].items[item["id"]] = item |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
173 |
15 | 174 print self.nodes[node_id].items |