]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
add Client.permute_peers
authorBrian Warner <warner@lothar.com>
Fri, 1 Dec 2006 03:18:51 +0000 (20:18 -0700)
committerBrian Warner <warner@lothar.com>
Fri, 1 Dec 2006 03:18:51 +0000 (20:18 -0700)
allmydata/client.py
allmydata/test/test_client.py

index 9838df41cdb2f2c1be36ef79d593f9b827055e67..4b055b30a1f5c33f7de78d67bb173849db181e58 100644 (file)
@@ -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]
index d80eb30882771e06f21c4fc2454eff739a1c0697..c067c303ce96304392c898a20dd1849bf7f3bf0f 100644 (file)
@@ -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"), [])