2 from pickle import loads, dumps
6 from const import reactor
9 from hash import intify
10 from knode import KNode as Node
11 from ktable import KTable, K
14 """ base class for some long running asynchronous proccesses like finding nodes or values """
15 def __init__(self, table, target, callback):
18 self.int = intify(target)
22 self.callback = callback
26 def sort(a, b, int=self.int):
27 """ this function is for sorting nodes relative to the ID we are looking for """
28 x, y = int ^ a.int, int ^ b.int
36 def goWithNodes(self, t):
41 FIND_NODE_TIMEOUT = 15
43 class FindNode(ActionBase):
44 """ find node action merits it's own class as it is a long running stateful process """
45 def handleGotNodes(self, args):
47 sender = Node().initWithDict(sender)
48 self.table.table.insertNode(sender)
49 if self.finished or self.answered.has_key(sender.id):
50 # a day late and a dollar short
52 self.outstanding = self.outstanding - 1
53 self.answered[sender.id] = 1
55 n = Node().initWithDict(node)
56 if not self.found.has_key(n.id):
62 send messages to new peers, if necessary
66 l = self.found.values()
70 if node.id == self.target:
72 return self.callback([node])
73 if (not self.queried.has_key(node.id)) and node.id != self.table.node.id:
74 #xxxx t.timeout = time.time() + FIND_NODE_TIMEOUT
75 df = node.findNode(self.target, self.table.node.senderDict())
76 df.addCallbacks(self.handleGotNodes, self.makeMsgFailed(node))
77 self.outstanding = self.outstanding + 1
78 self.queried[node.id] = 1
79 if self.outstanding >= const.CONCURRENT_REQS:
81 assert(self.outstanding) >=0
82 if self.outstanding == 0:
85 reactor.callFromThread(self.callback, l[:K])
87 def makeMsgFailed(self, node):
88 def defaultGotNodes(err, self=self, node=node):
89 self.table.table.nodeFailed(node)
90 self.outstanding = self.outstanding - 1
92 return defaultGotNodes
94 def goWithNodes(self, nodes):
96 this starts the process, our argument is a transaction with t.extras being our list of nodes
97 it's a transaction since we got called from the dispatcher
100 if node.id == self.table.node.id:
103 self.found[node.id] = node
108 GET_VALUE_TIMEOUT = 15
109 class GetValue(FindNode):
110 """ get value task """
111 def handleGotNodes(self, args):
113 sender = Node().initWithDict(sender)
114 self.table.table.insertNode(sender)
115 if self.finished or self.answered.has_key(sender.id):
116 # a day late and a dollar short
118 self.outstanding = self.outstanding - 1
119 self.answered[sender.id] = 1
121 # if we have any closer than what we already got, query them
122 if l.has_key('nodes'):
123 for node in l['nodes']:
124 n = Node().initWithDict(node)
125 if not self.found.has_key(n.id):
127 elif l.has_key('values'):
128 def x(y, z=self.results):
135 v = filter(None, map(x, l['values']))
137 reactor.callFromThread(self.callback, v)
144 l = self.found.values()
148 if (not self.queried.has_key(node.id)) and node.id != self.table.node.id:
149 #xxx t.timeout = time.time() + GET_VALUE_TIMEOUT
150 df = node.findValue(self.target, self.table.node.senderDict())
151 df.addCallback(self.handleGotNodes)
152 df.addErrback(self.makeMsgFailed(node))
153 self.outstanding = self.outstanding + 1
154 self.queried[node.id] = 1
155 if self.outstanding >= const.CONCURRENT_REQS:
157 assert(self.outstanding) >=0
158 if self.outstanding == 0:
159 ## all done, didn't find it!!
161 reactor.callFromThread(self.callback,[])
164 def goWithNodes(self, nodes, found=None):
170 if node.id == self.table.node.id:
173 self.found[node.id] = node
180 def __init__(self, store, itime, kw):
184 reactor.callLater(const.KEINITIAL_DELAY, self.doExpire)
187 self.cut = `time() - const.KE_AGE`
191 ic = self.itime.cursor()
192 sc = self.store.cursor()
193 kc = self.kw.cursor()
196 irec = ic.set_range(self.cut)
197 except db.DBNotFoundError:
198 # everything is expired
207 k, v, lt = loads(self.store[h])
214 except db.DBNotFoundError:
215 print "Database inconsistency! No key->value entry when a store entry was found!"
225 reactor.callLater(const.KE_DELAY, self.doExpire)
227 print ">>>KE: done expiring %d" % i