]> git.mxchange.org Git - quix0rs-apt-p2p.git/commitdiff
DHT normalizes key lengths by chopping or adding 0.
authorCameron Dale <camrdale@gmail.com>
Mon, 7 Jan 2008 19:28:14 +0000 (11:28 -0800)
committerCameron Dale <camrdale@gmail.com>
Mon, 7 Jan 2008 19:28:14 +0000 (11:28 -0800)
apt_dht_Khashmir/DHT.py

index dd4d5c46b7bd1a239ca8d2e11ea56bfabd653d42..42e2e9afd4489f33a1ddc385f1938b18301f5ec4 100644 (file)
@@ -100,6 +100,15 @@ class DHT:
             self.joined = False
             self.khashmir.shutdown()
         
+    def normalizeKey(self, key):
+        """Normalize a key's length suitable for insertion in the DHT."""
+        key_bytes = (self.config['HASH_LENGTH'] - 1) // 8 + 1
+        if len(key) < key_bytes:
+            key = key + '\000'*(key_bytes - len(key))
+        elif len(key) > key_bytes:
+            key = key[:key_bytes]
+        return key
+    
     def getValue(self, key):
         """See L{apt_dht.interfaces.IDHT}."""
         if self.config is None:
@@ -108,6 +117,7 @@ class DHT:
             raise DHTError, "have not joined a network yet"
 
         d = defer.Deferred()
+        key = self.normalizeKey(key)
         if key not in self.retrieving:
             self.khashmir.valueForKey(key, self._getValue)
         self.retrieving.setdefault(key, []).append(d)
@@ -133,6 +143,7 @@ class DHT:
         if not self.joined:
             raise DHTError, "have not joined a network yet"
 
+        key = self.normalizeKey(key)
         if key in self.storing and value in self.storing[key]:
             raise DHTError, "already storing that key with the same value"
 
@@ -175,6 +186,13 @@ class TestSimpleDHT(unittest.TestCase):
         self.b.bootstrap = ["127.0.0.1:4044"]
         self.b.cache_dir = '/tmp'
         
+    def test_normalizeKey(self):
+        self.failUnless(self.a.normalizeKey('12345678901234567890') == '12345678901234567890')
+        self.failUnless(self.a.normalizeKey('12345678901234567') == '12345678901234567\000\000\000')
+        self.failUnless(self.a.normalizeKey('1234567890123456789012345') == '12345678901234567890')
+        self.failUnless(self.a.normalizeKey('1234567890123456789') == '1234567890123456789\000')
+        self.failUnless(self.a.normalizeKey('123456789012345678901') == '12345678901234567890')
+    
     def test_bootstrap_join(self):
         d = self.a.join()
         return d