From: Brian Warner Date: Fri, 1 Dec 2006 03:18:51 +0000 (-0700) Subject: add Client.permute_peers X-Git-Tag: tahoe_v0.1.0-0-UNSTABLE~515 X-Git-Url: https://git.rkrishnan.org/pf/content/en/?a=commitdiff_plain;h=5252d2d0df9e51ec10c3dc15be8cc1896b5372ff;p=tahoe-lafs%2Ftahoe-lafs.git add Client.permute_peers --- diff --git a/allmydata/client.py b/allmydata/client.py index 9838df41..4b055b30 100644 --- a/allmydata/client.py +++ b/allmydata/client.py @@ -1,5 +1,6 @@ import os.path +import sha from foolscap import Tub, Referenceable from twisted.application import service from twisted.python import log @@ -108,5 +109,20 @@ class Client(service.MultiService, Referenceable): def get_remote_service(self, nodeid, servicename): if nodeid not in self.connections: raise IndexError("no connection to that peer") - d = self.connections[nodeid].callRemote("get_service", name=servicename) + d = self.connections[nodeid].callRemote("get_service", + name=servicename) return d + + + def permute_peerids(self, key, max_count=None): + # TODO: eventually reduce memory consumption by doing an insertion + # sort of at most max_count elements + results = [] + for nodeid in self.all_peers: + permuted = sha.new(key + nodeid).digest() + results.append((permuted, nodeid)) + results.sort() + results = [r[1] for r in results] + if max_count is None: + return results + return results[:max_count] diff --git a/allmydata/test/test_client.py b/allmydata/test/test_client.py index d80eb308..c067c303 100644 --- a/allmydata/test/test_client.py +++ b/allmydata/test/test_client.py @@ -4,8 +4,16 @@ from twisted.trial import unittest from allmydata import client class Basic(unittest.TestCase): - def testLoadable(self): + def test_loadable(self): c = client.Client("") c.startService() return c.stopService() + def test_permute(self): + c = client.Client("") + c.all_peers = ["%d" % i for i in range(5)] + self.failUnlessEqual(c.permute_peerids("one"), ['3','1','0','4','2']) + self.failUnlessEqual(c.permute_peerids("one", 3), ['3','1','0']) + self.failUnlessEqual(c.permute_peerids("two"), ['0','4','2','1','3']) + c.all_peers = [] + self.failUnlessEqual(c.permute_peerids("one"), [])