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):
48 sender['host'] = conn['host']
49 sender = Node().initWithDict(sender)
50 self.table.table.insertNode(sender)
51 if self.finished or self.answered.has_key(sender.id):
52 # a day late and a dollar short
54 self.outstanding = self.outstanding - 1
55 self.answered[sender.id] = 1
57 n = Node().initWithDict(node)
58 if not self.found.has_key(n.id):
64 send messages to new peers, if necessary
68 l = self.found.values()
72 if node.id == self.target:
74 return self.callback([node])
75 if not self.queried.has_key(node.id) and node.id != self.table.node.id:
76 #xxxx t.timeout = time.time() + FIND_NODE_TIMEOUT
77 df = node.findNode(self.target, self.table.node.senderDict())
78 df.addCallbacks(self.handleGotNodes, self.makeMsgFailed(node))
79 self.outstanding = self.outstanding + 1
80 self.queried[node.id] = 1
81 if self.outstanding >= const.CONCURRENT_REQS:
83 assert(self.outstanding) >=0
84 if self.outstanding == 0:
87 reactor.callFromThread(self.callback, l[:K])
89 def makeMsgFailed(self, node):
90 def defaultGotNodes(err, self=self, node=node):
91 self.table.table.nodeFailed(node)
94 self.outstanding = self.outstanding - 1
96 return defaultGotNodes
98 def goWithNodes(self, nodes):
100 this starts the process, our argument is a transaction with t.extras being our list of nodes
101 it's a transaction since we got called from the dispatcher
104 if node.id == self.table.node.id:
107 self.found[node.id] = node
112 GET_VALUE_TIMEOUT = 15
113 class GetValue(FindNode):
114 """ get value task """
115 def handleGotNodes(self, args):
118 sender['host'] = conn['host']
119 sender = Node().initWithDict(sender)
120 self.table.table.insertNode(sender)
121 if self.finished or self.answered.has_key(sender.id):
122 # a day late and a dollar short
124 self.outstanding = self.outstanding - 1
125 self.answered[sender.id] = 1
127 # if we have any closer than what we already got, query them
128 if l.has_key('nodes'):
129 for node in l['nodes']:
130 n = Node().initWithDict(node)
131 if not self.found.has_key(n.id):
133 elif l.has_key('values'):
134 def x(y, z=self.results):
141 v = filter(None, map(x, l['values']))
143 reactor.callFromThread(self.callback, v)
150 l = self.found.values()
154 if not self.queried.has_key(node.id) and node.id != self.table.node.id:
155 #xxx t.timeout = time.time() + GET_VALUE_TIMEOUT
156 df = node.findValue(self.target, self.table.node.senderDict())
157 df.addCallback(self.handleGotNodes)
158 df.addErrback(self.makeMsgFailed(node))
159 self.outstanding = self.outstanding + 1
160 self.queried[node.id] = 1
161 if self.outstanding >= const.CONCURRENT_REQS:
163 assert(self.outstanding) >=0
164 if self.outstanding == 0:
165 ## all done, didn't find it!!
167 reactor.callFromThread(self.callback,[])
170 def goWithNodes(self, nodes, found=None):
176 if node.id == self.table.node.id:
179 self.found[node.id] = node
186 def __init__(self, store, itime, kw):
190 reactor.callLater(const.KEINITIAL_DELAY, self.doExpire)
193 self.cut = `time() - const.KE_AGE`
197 ic = self.itime.cursor()
198 sc = self.store.cursor()
199 kc = self.kw.cursor()
202 irec = ic.set_range(self.cut)
203 except db.DBNotFoundError:
204 # everything is expired
213 k, v, lt = loads(self.store[h])
220 except db.DBNotFoundError:
221 print "Database inconsistency! No key->value entry when a store entry was found!"
231 reactor.callLater(const.KE_DELAY, self.doExpire)
233 print ">>>KE: done expiring %d" % i