From 36db2c1145e5e21b360d17854bb54248fbd12299 Mon Sep 17 00:00:00 2001 From: burris Date: Mon, 2 Sep 2002 17:32:28 +0000 Subject: [PATCH] finding values is now fixed --- actions.py | 10 +++++----- const.py | 2 +- khashmir.py | 19 +++++++++++-------- knode.py | 2 +- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/actions.py b/actions.py index 052f6a4..3112027 100644 --- a/actions.py +++ b/actions.py @@ -109,7 +109,6 @@ class GetValue(FindNode): """ 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 @@ -126,7 +125,7 @@ class GetValue(FindNode): elif l.has_key('values'): ## done self.finished = 1 - return self.callback(l['values']) + reactor.callFromThread(self.callback, l['values']) self.schedule() ## get value @@ -139,8 +138,9 @@ class GetValue(FindNode): 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: @@ -158,7 +158,7 @@ class GetValue(FindNode): 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 diff --git a/const.py b/const.py index 0aad77d..a36d343 100644 --- a/const.py +++ b/const.py @@ -1,5 +1,5 @@ +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 diff --git a/khashmir.py b/khashmir.py index 748ce21..aa1902f 100644 --- a/khashmir.py +++ b/khashmir.py @@ -12,6 +12,8 @@ from actions import FindNode, GetValue 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/ @@ -28,8 +30,6 @@ class Khashmir(xmlrpc.XMLRPC): 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() @@ -204,7 +204,7 @@ class Khashmir(xmlrpc.XMLRPC): 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() @@ -215,7 +215,11 @@ class Khashmir(xmlrpc.XMLRPC): pass -#------ + + + + +#------ testing def test_build_net(quiet=0): from whrandom import randrange @@ -309,7 +313,7 @@ def test_find_value(l, quiet=0): if(len(values) == 0): print "find FAILED" else: - if values[0]['value'] != val: + if values != val: print "find FAILED" else: print "find FOUND" @@ -317,11 +321,10 @@ def test_find_value(l, quiet=0): 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__": diff --git a/knode.py b/knode.py index 66b8b53..aecf14e 100644 --- a/knode.py +++ b/knode.py @@ -20,7 +20,7 @@ class KNode(Node): 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 -- 2.39.5