comparison idavoll/backend.py @ 23:884268687229

Simplify call chain by mapping incoming requests directly to method calls in pubsub.py, and providing a exception handler for all backend calls.
author Ralph Meijer <ralphm@ik.nu>
date Thu, 07 Oct 2004 15:57:05 +0000
parents eddea65d1032
children 4f3bbefc6fad
comparison
equal deleted inserted replaced
22:9b610962d045 23:884268687229
70 node.configuration.deliver_payloads = True 70 node.configuration.deliver_payloads = True
71 self.nodes[node.id] = node 71 self.nodes[node.id] = node
72 72
73 def do_publish(self, node_id, publisher, items): 73 def do_publish(self, node_id, publisher, items):
74 try: 74 try:
75 try: 75 node = self.nodes[node_id]
76 node = self.nodes[node_id] 76 persist_items = node.configuration.persist_items
77 persist_items = node.configuration.persist_items 77 deliver_payloads = node.configuration.deliver_payloads
78 deliver_payloads = node.configuration.deliver_payloads 78 except KeyError:
79 except KeyError: 79 raise NodeNotFound
80 raise NodeNotFound
81 80
82 try: 81 try:
83 if node.affiliations[publisher] not in ['owner', 'publisher']: 82 if node.affiliations[publisher] not in ['owner', 'publisher']:
84 raise NotAuthorized 83 raise NotAuthorized
85 except KeyError: 84 except KeyError:
86 raise NotAuthorized() 85 raise NotAuthorized()
87 86
88 if items and not persist_items and not deliver_payloads: 87 if items and not persist_items and not deliver_payloads:
89 raise NoPayloadAllowed 88 raise NoPayloadAllowed
90 elif not items and (persist_items or deliver_payloads): 89 elif not items and (persist_items or deliver_payloads):
91 raise PayloadExpected 90 raise PayloadExpected
92 91
93 print "publish by %s to %s" % (publisher, node_id) 92 print "publish by %s to %s" % (publisher, node_id)
94 93
95 if persist_items or deliver_payloads: 94 if persist_items or deliver_payloads:
96 for item in items: 95 for item in items:
97 if item["id"] is None: 96 if item["id"] is None:
98 item["id"] = 'random' # FIXME 97 item["id"] = 'random' # FIXME
99 98
100 if persist_items: 99 if persist_items:
101 self.storeItems(node_id, publisher, items) 100 self.storeItems(node_id, publisher, items)
102 101
103 if items and not deliver_payloads: 102 if items and not deliver_payloads:
104 for item in items: 103 for item in items:
105 item.children = [] 104 item.children = []
106 105
107 recipients = self.get_subscribers(node_id) 106 recipients = self.get_subscribers(node_id)
108 recipients.addCallback(self.magic_filter, node_id, items) 107 recipients.addCallback(self.magic_filter, node_id, items)
109 recipients.addCallback(self.pubsub_service.do_notification, node_id) 108 recipients.addCallback(self.pubsub_service.do_notification, node_id)
110 109
111 return defer.succeed(None) 110 return defer.succeed(None)
112 except:
113 return defer.fail(failure.Failure())
114 111
115 def do_subscribe(self, node_id, subscriber, requestor): 112 def do_subscribe(self, node_id, subscriber, requestor):
116 # expect subscriber and requestor to be a jid.JID 113 # expect subscriber and requestor to be a jid.JID
117 try: 114 try:
118 try: 115 node = self.nodes[node_id]
119 node = self.nodes[node_id] 116 except KeyError:
120 except KeyError: 117 raise NodeNotFound
121 raise NodeNotFound
122 118
123 affiliation = node.affiliations.get(requestor.full(), 'none') 119 affiliation = node.affiliations.get(requestor.full(), 'none')
124 120
125 if affiliation == 'banned': 121 if affiliation == 'banned':
126 raise NotAuthorized 122 raise NotAuthorized
127 123
128 print subscriber.full() 124 print subscriber.full()
129 print subscriber.userhostJID().full() 125 print subscriber.userhostJID().full()
130 print requestor.full() 126 print requestor.full()
131 127
132 if subscriber.userhostJID() != requestor: 128 if subscriber.userhostJID() != requestor:
133 raise NotAuthorized 129 raise NotAuthorized
134 130
135 try: 131 try:
136 subscription = node.subscriptions[subscriber.full()] 132 subscription = node.subscriptions[subscriber.full()]
137 except KeyError: 133 except KeyError:
138 subscription = Subscription('subscribed') 134 subscription = Subscription('subscribed')
139 node.subscriptions[subscriber.full()] = subscription 135 node.subscriptions[subscriber.full()] = subscription
140 136
141 print node.subscriptions 137 print node.subscriptions
142 138
143 return defer.succeed({ 139 return defer.succeed({
144 'affiliation': affiliation, 140 'affiliation': affiliation,
145 'node': node_id, 141 'node': node_id,
146 'jid': subscriber, 142 'jid': subscriber,
147 'subscription': subscription.state}) 143 'subscription': subscription.state})
148 except:
149 return defer.fail(failure.Failure)
150
151 144
152 def magic_filter(self, subscribers, node_id, items): 145 def magic_filter(self, subscribers, node_id, items):
153 list = {} 146 list = {}
154 for subscriber in subscribers: 147 for subscriber in subscribers:
155 list[subscriber] = items 148 list[subscriber] = items
157 return list 150 return list
158 151
159 def get_subscribers(self, node_id): 152 def get_subscribers(self, node_id):
160 d = defer.Deferred() 153 d = defer.Deferred()
161 try: 154 try:
162 result = self.nodes[node_id].subscriptions.keys() 155 return defer.succeed(self.nodes[node_id].subscriptions.keys())
163 except: 156 except:
164 f = failure.Failure() 157 return defer.fail()
165 reactor.callLater(0, d.errback, f)
166 else:
167 reactor.callLater(0, d.callback, result)
168
169 return d
170 158
171 def storeItems(self, node_id, publisher, items): 159 def storeItems(self, node_id, publisher, items):
172 for item in items: 160 for item in items:
173 self.nodes[node_id].items[item["id"]] = item 161 self.nodes[node_id].items[item["id"]] = item
174 162
175 print self.nodes[node_id].items 163 print self.nodes[node_id].items
176 164
177 def create_node(node_id, owner): 165 def create_node(self, node_id, owner):
178 result = {} 166 result = {}
179 167
180 try: 168 if not node_id:
181 if not node_id: 169 raise NoInstantNodes
182 raise NoInstantNodes
183 170
184 if node_id in self.nodes: 171 if node_id in self.nodes:
185 raise NodeExists 172 raise NodeExists
186 173
187 self.nodes[node_id] = Node(node_id) 174 node = Node(node_id)
188 node.affiliations[owner.full()] = 'owner' 175 node.affiliations[owner.full()] = 'owner'
189 return defer.succeed({'node_id': node.id}) 176 self.nodes[node_id] = node
190 except:
191 return defer.fail(failure.Failure)
192 177
178 return defer.succeed({'node_id': node.id})