Mercurial > libervia-pubsub
comparison idavoll/memory_backend.py @ 43:9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Implemented Storage class for memory backend.
Implemented item storage for pgsql Storage.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Mon, 01 Nov 2004 12:37:40 +0000 |
parents | b9e7b3b6c687 |
children | 979e53b54267 |
comparison
equal
deleted
inserted
replaced
42:7d088c61e131 | 43:9685b7e291ef |
---|---|
18 self.configuration = NodeConfiguration() | 18 self.configuration = NodeConfiguration() |
19 self.subscriptions = {} | 19 self.subscriptions = {} |
20 self.affiliations = {} | 20 self.affiliations = {} |
21 self.items = {} | 21 self.items = {} |
22 | 22 |
23 class BackendService(backend.BackendService): | 23 class Storage: |
24 | |
25 def __init__(self): | 24 def __init__(self): |
26 backend.BackendService.__init__(self) | |
27 | |
28 self.nodes = {} | 25 self.nodes = {} |
29 | 26 |
30 node = Node("ralphm/mood/ralphm@ik.nu") | 27 node = Node("ralphm/mood/ralphm@ik.nu") |
31 node.subscriptions["ralphm@doe.ik.nu"] = Subscription("subscribed") | 28 node.subscriptions["ralphm@doe.ik.nu"] = Subscription("subscribed") |
32 node.subscriptions["notify@ik.nu/mood_monitor"] = Subscription("subscribed") | 29 node.subscriptions["notify@ik.nu/mood_monitor"] = Subscription("subscribed") |
33 node.affiliations["ralphm@ik.nu"] = "owner" | 30 node.affiliations["ralphm@ik.nu"] = "owner" |
34 node.affiliations["ralphm@doe.ik.nu"] = "publisher" | 31 node.affiliations["ralphm@doe.ik.nu"] = "publisher" |
35 node.configuration.persist_items = True | 32 node.configuration.persist_items = True |
36 node.configuration.deliver_payloads = True | 33 node.configuration.deliver_payloads = True |
37 self.nodes[node.id] = node | 34 self.nodes[node.id] = node |
38 | 35 |
36 def get_node_configuration(self, node_id): | |
37 try: | |
38 node = self.nodes[node_id] | |
39 except KeyError: | |
40 raise backend.NodeNotFound | |
41 else: | |
42 c = self.nodes[node_id].configuration | |
43 return defer.succeed({'persist_items': c.persist_items, | |
44 'deliver_payloads': c.deliver_payloads}) | |
45 | |
46 def get_affiliation(self, node_id, entity): | |
47 try: | |
48 node = self.nodes[node_id] | |
49 except KeyError: | |
50 raise backend.NodeNotFound | |
51 else: | |
52 return defer.succeed(node.affiliations.get(entity, None)) | |
53 | |
54 def get_subscribers(self, node_id): | |
55 try: | |
56 node = self.nodes[node_id] | |
57 except KeyError: | |
58 raise backend.NodeNotFound | |
59 else: | |
60 subscriptions = self.nodes[node_id].subscriptions | |
61 subscribers = [s for s in subscriptions | |
62 if subscriptions[s].state == 'subscribed'] | |
63 return defer.succeed(subscribers) | |
64 | |
65 def store_items(self, node_id, items, publisher): | |
66 for item in items: | |
67 self.nodes[node_id].items[item["id"]] = (item, publisher) | |
68 print self.nodes[node_id].items | |
69 return defer.succeed(None) | |
70 | |
71 class BackendService(backend.BackendService): | |
72 | |
39 def create_node(self, node_id, requestor): | 73 def create_node(self, node_id, requestor): |
40 if not node_id: | 74 if not node_id: |
41 raise backend.NoInstantNodes | 75 raise backend.NoInstantNodes |
42 | 76 |
43 if node_id in self.nodes: | 77 if node_id in self.nodes: |
46 node = Node(node_id) | 80 node = Node(node_id) |
47 node.affiliations[requestor.full()] = 'owner' | 81 node.affiliations[requestor.full()] = 'owner' |
48 self.nodes[node_id] = node | 82 self.nodes[node_id] = node |
49 | 83 |
50 return defer.succeed({'node_id': node.id}) | 84 return defer.succeed({'node_id': node.id}) |
51 | |
52 def publish(self, node_id, items, requestor): | |
53 try: | |
54 node = self.nodes[node_id] | |
55 persist_items = node.configuration.persist_items | |
56 deliver_payloads = node.configuration.deliver_payloads | |
57 except KeyError: | |
58 raise backend.NodeNotFound | |
59 | |
60 try: | |
61 if node.affiliations[requestor.full()] not in \ | |
62 ['owner', 'publisher']: | |
63 raise backend.NotAuthorized | |
64 except KeyError: | |
65 raise backend.NotAuthorized | |
66 | |
67 if items and not persist_items and not deliver_payloads: | |
68 raise backend.NoPayloadAllowed | |
69 elif not items and (persist_items or deliver_payloads): | |
70 raise backend.PayloadExpected | |
71 | |
72 print "publish by %s to %s" % (requestor.full(), node_id) | |
73 | |
74 if persist_items or deliver_payloads: | |
75 for item in items: | |
76 if item["id"] is None: | |
77 item["id"] = 'random' # FIXME | |
78 | |
79 if persist_items: | |
80 self.store_items(node_id, items, requestor.full()) | |
81 | |
82 if items and not deliver_payloads: | |
83 for item in items: | |
84 item.children = [] | |
85 | |
86 self.dispatch({ 'items': items, 'node_id': node_id }, | |
87 '//event/pubsub/notify') | |
88 return defer.succeed(None) | |
89 | |
90 def get_notification_list(self, node_id, items): | |
91 subscriptions = self.nodes[node_id].subscriptions | |
92 | |
93 try: | |
94 subscribers = [s for s in subscriptions | |
95 if subscriptions[s].state == 'subscribed'] | |
96 d = defer.succeed(subscribers) | |
97 except: | |
98 d = defer.fail() | |
99 | |
100 d.addCallback(self._magic_filter, node_id, items) | |
101 | |
102 return d | |
103 | |
104 def _magic_filter(self, subscribers, node_id, items): | |
105 list = {} | |
106 for subscriber in subscribers: | |
107 list[subscriber] = items | |
108 | |
109 return list | |
110 | 85 |
111 def subscribe(self, node_id, subscriber, requestor): | 86 def subscribe(self, node_id, subscriber, requestor): |
112 # expect subscriber and requestor to be a jid.JID | 87 # expect subscriber and requestor to be a jid.JID |
113 try: | 88 try: |
114 node = self.nodes[node_id] | 89 node = self.nodes[node_id] |
151 except KeyError: | 126 except KeyError: |
152 raise backend.NotSubscribed | 127 raise backend.NotSubscribed |
153 | 128 |
154 return defer.succeed(None) | 129 return defer.succeed(None) |
155 | 130 |
156 def store_items(self, node_id, items, publisher): | |
157 for item in items: | |
158 self.nodes[node_id].items[item["id"]] = item | |
159 print self.nodes[node_id].items | |
160 | |
161 class NodeCreationService(service.Service): | 131 class NodeCreationService(service.Service): |
162 | 132 |
163 __implements__ = backend.INodeCreationService, | 133 __implements__ = backend.INodeCreationService, |
164 | 134 |
165 def create_node(self, node_id, requestor): | 135 def create_node(self, node_id, requestor): |