From 5252d2d0df9e51ec10c3dc15be8cc1896b5372ff Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Thu, 30 Nov 2006 20:18:51 -0700 Subject: [PATCH] add Client.permute_peers --- allmydata/client.py | 18 +++++++++++++++++- allmydata/test/test_client.py | 10 +++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) 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"), []) -- 2.45.2