comparison idavoll/backend.py @ 20:eddea65d1032

Added two exceptions: NoInstantNodes and NodeExists. Renamed 'name' attribute of Node to 'id'. Added create_node() method.
author Ralph Meijer <ralphm@ik.nu>
date Wed, 06 Oct 2004 21:05:42 +0000
parents 7937d6fbbe2a
children 884268687229
comparison
equal deleted inserted replaced
19:53b1bea9289b 20:eddea65d1032
29 29
30 class NoPayloadAllowed(BackendException): 30 class NoPayloadAllowed(BackendException):
31 def __init__(self, msg = 'No payload allowed'): 31 def __init__(self, msg = 'No payload allowed'):
32 BackendException.__init__(self, msg) 32 BackendException.__init__(self, msg)
33 33
34 class NoInstantNodes(BackendException):
35 pass
36
37 class NodeExists(BackendException):
38 pass
39
34 class Subscription: 40 class Subscription:
35 def __init__(self, state): 41 def __init__(self, state):
36 self.state = state 42 self.state = state
37 43
38 class NodeConfiguration: 44 class NodeConfiguration:
39 def __init__(self): 45 def __init__(self):
40 self.persist_items = False 46 self.persist_items = False
41 self.deliver_payloads = False 47 self.deliver_payloads = False
42 48
43 class Node: 49 class Node:
44 def __init__(self, name): 50 def __init__(self, id):
45 self.name = name 51 self.id = id
46 self.configuration = NodeConfiguration() 52 self.configuration = NodeConfiguration()
47 self.subscriptions = {} 53 self.subscriptions = {}
48 self.affiliations = {} 54 self.affiliations = {}
49 self.items = {} 55 self.items = {}
50 56
60 node.subscriptions["notify@ik.nu/mood_monitor"] = Subscription("subscribed") 66 node.subscriptions["notify@ik.nu/mood_monitor"] = Subscription("subscribed")
61 node.affiliations["ralphm@ik.nu"] = "owner" 67 node.affiliations["ralphm@ik.nu"] = "owner"
62 node.affiliations["ralphm@doe.ik.nu"] = "publisher" 68 node.affiliations["ralphm@doe.ik.nu"] = "publisher"
63 node.configuration.persist_items = True 69 node.configuration.persist_items = True
64 node.configuration.deliver_payloads = True 70 node.configuration.deliver_payloads = True
65 self.nodes[node.name] = node 71 self.nodes[node.id] = node
66 72
67 def do_publish(self, node_id, publisher, items): 73 def do_publish(self, node_id, publisher, items):
68 try: 74 try:
69 try: 75 try:
70 node = self.nodes[node_id] 76 node = self.nodes[node_id]
87 print "publish by %s to %s" % (publisher, node_id) 93 print "publish by %s to %s" % (publisher, node_id)
88 94
89 if persist_items or deliver_payloads: 95 if persist_items or deliver_payloads:
90 for item in items: 96 for item in items:
91 if item["id"] is None: 97 if item["id"] is None:
92 item["id"] = 'random' 98 item["id"] = 'random' # FIXME
93 99
94 if persist_items: 100 if persist_items:
95 self.storeItems(node_id, publisher, items) 101 self.storeItems(node_id, publisher, items)
96 102
97 if items and not deliver_payloads: 103 if items and not deliver_payloads:
165 def storeItems(self, node_id, publisher, items): 171 def storeItems(self, node_id, publisher, items):
166 for item in items: 172 for item in items:
167 self.nodes[node_id].items[item["id"]] = item 173 self.nodes[node_id].items[item["id"]] = item
168 174
169 print self.nodes[node_id].items 175 print self.nodes[node_id].items
176
177 def create_node(node_id, owner):
178 result = {}
179
180 try:
181 if not node_id:
182 raise NoInstantNodes
183
184 if node_id in self.nodes:
185 raise NodeExists
186
187 self.nodes[node_id] = Node(node_id)
188 node.affiliations[owner.full()] = 'owner'
189 return defer.succeed({'node_id': node.id})
190 except:
191 return defer.fail(failure.Failure)
192