]> git.mxchange.org Git - quix0rs-apt-p2p.git/blobdiff - apt_p2p_Khashmir/util.py
New TODO for better total number of nodes calculation.
[quix0rs-apt-p2p.git] / apt_p2p_Khashmir / util.py
index 55bd45ac1f102812706f1177b3838d153d2ec129..dba9d8b82bebdf4e233e50a7fc8ee03b723db2b3 100644 (file)
@@ -1,10 +1,10 @@
-## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
-# see LICENSE.txt for license information
 
 """Some utitlity functions for use in apt-p2p's khashmir DHT."""
 
 from twisted.trial import unittest
 
+from khash import HASH_LENGTH
+
 def bucket_stats(l):
     """Given a list of khashmir instances, finds min, max, and average number of nodes in tables."""
     max = avg = 0
@@ -12,7 +12,7 @@ def bucket_stats(l):
     def count(buckets):
         c = 0
         for bucket in buckets:
-            c = c + len(bucket.l)
+            c = c + bucket.len()
         return c
     for node in l:
         c = count(node.table.buckets)
@@ -35,11 +35,11 @@ def uncompact(s):
     @return: the node ID, IP address and port to contact the node on
     @raise ValueError: if the compact representation doesn't exist
     """
-    if (len(s) != 26):
+    if (len(s) != HASH_LENGTH+6):
         raise ValueError
-    id = s[:20]
-    host = '.'.join([str(ord(i)) for i in s[20:24]])
-    port = (ord(s[24]) << 8) | ord(s[25])
+    id = s[:HASH_LENGTH]
+    host = '.'.join([str(ord(i)) for i in s[HASH_LENGTH:(HASH_LENGTH+4)]])
+    port = (ord(s[HASH_LENGTH+4]) << 8) | ord(s[HASH_LENGTH+5])
     return {'id': id, 'host': host, 'port': port}
 
 def compact(id, host, port):