1
|
1 from twisted.application import service |
|
2 from twisted.python import components, failure |
|
3 from twisted.internet import defer, reactor |
|
4 |
|
5 class IBackendService(components.Interface): |
|
6 """ Interface to a backend service of a pubsub service """ |
|
7 |
|
8 class BackendException(Exception): |
|
9 def __init__(self, msg = ''): |
|
10 self.msg = msg |
|
11 |
|
12 def __str__(self): |
|
13 return self.msg |
|
14 |
|
15 class NodeNotFound(BackendException): |
|
16 #def __init__(self, msg = 'Node not found'): |
|
17 # BackendException.__init__(self, msg) |
|
18 pass |
|
19 |
|
20 class NotAuthorized(BackendException): |
|
21 pass |
|
22 |
|
23 class MemoryBackendService(service.Service): |
|
24 |
|
25 __implements__ = IBackendService, |
|
26 |
|
27 def __init__(self): |
|
28 self.nodes = {"ralphm/test": 'test'} |
|
29 self.subscribers = {"ralphm/test": ["ralphm@ik.nu", "intosi@ik.nu"] } |
|
30 self.affiliations = {"ralphm/test": { "ralphm@ik.nu": "owner", "ralphm@se-135.se.wtb.tue.nl": 'publisher' } } |
|
31 |
|
32 def do_publish(self, node, publisher, item): |
|
33 try: |
|
34 try: |
|
35 result = self.nodes[node] |
|
36 except KeyError: |
|
37 raise NodeNotFound |
|
38 |
|
39 try: |
|
40 affiliation = self.affiliations[node][publisher] |
|
41 if affiliation not in ['owner', 'publisher']: |
|
42 raise NotAuthorized |
|
43 except KeyError: |
|
44 raise NotAuthorized() |
|
45 print "publish by %s to %s" % (publisher, node) |
|
46 return defer.succeed(result) |
|
47 except: |
|
48 f = failure.Failure() |
|
49 return defer.fail(f) |
|
50 |
|
51 def get_subscribers(self, node): |
|
52 d = defer.Deferred() |
|
53 try: |
|
54 result = self.subscribers[node] |
|
55 except: |
|
56 f = failure.Failure() |
|
57 reactor.callLater(0, d.errback, f) |
|
58 else: |
|
59 reactor.callLater(0, d.callback, result) |
|
60 |
|
61 return d |
|
62 |