""" get value task """
def handleGotNodes(self, args):
l, sender = args
- l = l[0]
if self.finished or self.answered.has_key(sender['id']):
# a day late and a dollar short
return
elif l.has_key('values'):
## done
self.finished = 1
- return self.callback(l['values'])
+ reactor.callFromThread(self.callback, l['values'])
self.schedule()
## get value
for node in l[:K]:
if not self.queried.has_key(node.id) and node.id != self.table.node.id:
#xxx t.timeout = time.time() + GET_VALUE_TIMEOUT
- df = node.getValue(node, self.target)
- df.addCallbacks(self.handleGotNodes, self.defaultGotNodes)
+ df = node.findValue(self.target, self.table.node.senderDict())
+ df.addCallback(self.handleGotNodes)
+ df.addErrback(self.defaultGotNodes)
self.outstanding = self.outstanding + 1
self.queried[node.id] = 1
if self.outstanding >= N:
continue
self.found[node.id] = node
#xxx t.timeout = time.time() + FIND_NODE_TIMEOUT
- df = node.findNode(self.target, self.table.node.senderDict())
+ df = node.findValue(self.target, self.table.node.senderDict())
df.addCallbacks(self.handleGotNodes, self.defaultGotNodes)
self.outstanding = self.outstanding + 1
self.queried[node.id] = 1
+from twisted.internet.default import SelectReactor ## twistedmatrix.com
-from twisted.internet.default import SelectReactor
reactor = SelectReactor(installSignalHandlers=0)
from twisted.internet import main
main.installReactor(reactor)
\ No newline at end of file
from twisted.web import xmlrpc
from twisted.internet.defer import Deferred
from twisted.python import threadable
+from twisted.internet.app import Application
+from twisted.web import server
threadable.init()
from bsddb3 import db ## find this at http://pybsddb.sf.net/
def __init__(self, host, port):
self.node = Node(newID(), host, port)
self.table = KTable(self.node)
- from twisted.internet.app import Application
- from twisted.web import server
self.app = Application("xmlrpc")
self.app.listenTCP(port, server.Site(self))
self.store = db.DB()
if self.store.has_key(key):
return {'values' : self.store[key]}, self.node.senderDict()
else:
- nodes = self.table.findNodes(msg['key'])
+ nodes = self.table.findNodes(key)
nodes = map(lambda node: node.senderDict(), nodes)
return {'nodes' : nodes}, self.node.senderDict()
pass
-#------
+
+
+
+
+#------ testing
def test_build_net(quiet=0):
from whrandom import randrange
if(len(values) == 0):
print "find FAILED"
else:
- if values[0]['value'] != val:
+ if values != val:
print "find FAILED"
else:
print "find FOUND"
f.set()
return callback
b.valueForKey(key, mc(fa))
- c.valueForKey(key, mc(fb))
- d.valueForKey(key, mc(fc))
-
fa.wait()
+ c.valueForKey(key, mc(fb))
fb.wait()
+ d.valueForKey(key, mc(fc))
fc.wait()
if __name__ == "__main__":
reactor.connectTCP(self.host, self.port, f)
return df
def findValue(self, key, sender):
+ df = Deferred()
f = factory('find_value', (key, sender), df.callback, df.errback)
reactor.connectTCP(self.host, self.port, f)
- df = Deferred()
return df