1 ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
4 from twisted.trial import unittest
7 """given a list of khashmir instances, finds min, max, and average number of nodes in tables"""
12 for bucket in buckets:
16 c = count(node.table.buckets)
25 return {'min':min, 'max':max, 'avg':avg}
28 """Extract the contact info from a compact node representation.
31 @param s: the compact representation
33 @return: the node ID, IP address and port to contact the node on
34 @raise ValueError: if the compact representation doesn't exist
39 host = '.'.join([str(ord(i)) for i in s[20:24]])
40 port = (ord(s[24]) << 8) | ord(s[25])
41 return {'id': id, 'host': host, 'port': port}
43 def compact(id, host, port):
44 """Create a compact representation of node contact info.
47 @param id: the node ID
49 @param host: the IP address of the node
51 @param port: the port number to contact the node on
53 @return: the compact representation
54 @raise ValueError: if the compact representation doesn't exist
57 s = id + ''.join([chr(int(i)) for i in host.split('.')]) + \
58 chr((port & 0xFF00) >> 8) + chr(port & 0xFF)
63 class TestUtil(unittest.TestCase):
64 """Tests for the utilities."""
67 myid = '\xca\xec\xb8\x0c\x00\xe7\x07\xf8~])\x8f\x9d\xe5_B\xff\x1a\xc4!'
71 def test_compact(self):
72 d = uncompact(compact(self.myid, self.host, self.port))
73 self.failUnlessEqual(d['id'], self.myid)
74 self.failUnlessEqual(d['host'], self.host)
75 self.failUnlessEqual(d['port'], self.port)