本文整理汇总了Python中drogulus.dht.routingtable.RoutingTable类的典型用法代码示例。如果您正苦于以下问题:Python RoutingTable类的具体用法?Python RoutingTable怎么用?Python RoutingTable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RoutingTable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add_contact_with_existing_contact_in_replacement_cache
def test_add_contact_with_existing_contact_in_replacement_cache(self):
"""
Ensures that if the contact to be put in the replacement cache already
exists in the replacement cache then it is bumped to the most recent
position.
"""
parent_node_id = hex((2 ** 512) + 1)[2:]
r = RoutingTable(parent_node_id)
# Fill up the bucket and replacement cache
for i in range(40):
uri = 'netstring://192.168.0.%d:9999/' % i
contact = PeerNode(PUBLIC_KEY, self.version, uri, 0)
contact.network_id = hex(i)
r.add_contact(contact)
# Sanity check of the replacement cache.
cache_key = (r._buckets[0].range_min, r._buckets[0].range_max)
self.assertEqual(len(r._replacement_cache[cache_key]), 20)
self.assertEqual(hex(20),
r._replacement_cache[cache_key][0].network_id)
# Create a new contact that will be added to the replacement cache.
new_contact = PeerNode(PUBLIC_KEY, self.version,
'netstring://192.168.0.41:9999/', 0)
new_contact.network_id = hex(20)
r.add_contact(new_contact)
self.assertEqual(len(r._replacement_cache[cache_key]), 20)
self.assertEqual(new_contact, r._replacement_cache[cache_key][19])
self.assertEqual(hex(21),
r._replacement_cache[cache_key][0].network_id)
示例2: test_dump
def test_dump(self):
"""
Ensures that the expected dictionary is returned from a call to the
dump method (used to back up the routing table).
"""
contacts = []
blacklist = [BAD_PUBLIC_KEY, ]
uri = 'netstring://192.168.0.1:9999/'
contacts.append({
'public_key': PUBLIC_KEY,
'version': self.version,
'uri': uri,
})
data_dump = {
'contacts': contacts,
'blacklist': blacklist,
}
parent_node_id = 'deadbeef'
rt = RoutingTable(parent_node_id)
rt.restore(data_dump)
result = rt.dump()
self.assertIn('blacklist', result)
self.assertIn('contacts', result)
self.assertEqual(1, len(result['blacklist']))
self.assertIn(BAD_PUBLIC_KEY, result['blacklist'])
self.assertEqual(1, len(result['contacts']))
self.assertEqual(PUBLIC_KEY, result['contacts'][0]['public_key'])
self.assertEqual(self.version, result['contacts'][0]['version'])
self.assertEqual(uri, result['contacts'][0]['uri'])
# check it can be serialised into JSON
dump = json.dumps(result)
self.assertIsInstance(dump, str)
示例3: test_add_contact_with_full_replacement_cache
def test_add_contact_with_full_replacement_cache(self):
"""
Ensures that if the replacement cache is full (length = k) then the
oldest contact within the cache is replaced with the new contact that
was just seen.
"""
parent_node_id = hex((2 ** 512) + 1)[2:]
r = RoutingTable(parent_node_id)
# Fill up the bucket and replacement cache
for i in range(40):
uri = 'netstring://192.168.0.%d:9999/' % i
contact = PeerNode(PUBLIC_KEY, self.version, uri, 0)
contact.network_id = hex(i)
r.add_contact(contact)
# Sanity check of the replacement cache.
cache_key = (r._buckets[0].range_min, r._buckets[0].range_max)
self.assertEqual(len(r._replacement_cache[cache_key]), 20)
self.assertEqual(hex(20),
r._replacement_cache[cache_key][0].network_id)
# Create a new contact that will be added to the replacement cache.
new_contact = PeerNode(PUBLIC_KEY, self.version,
'netstring://192.168.0.20:9999/', 0)
new_contact.network_id = hex(40)
r.add_contact(new_contact)
self.assertEqual(len(r._replacement_cache[cache_key]), 20)
self.assertEqual(new_contact, r._replacement_cache[cache_key][19])
self.assertEqual(hex(21),
r._replacement_cache[cache_key][0].network_id)
示例4: test_restore_with_contacts_and_blacklist
def test_restore_with_contacts_and_blacklist(self):
"""
Ensures that any contacts also in the blacklist are actually
blacklisted and removed from the routing table.
"""
blacklist = [BAD_PUBLIC_KEY, ]
contacts = []
uri = 'netstring://192.168.0.1:9999/'
contacts.append({
'public_key': PUBLIC_KEY,
'version': self.version,
'uri': uri,
})
contacts.append({
'public_key': BAD_PUBLIC_KEY,
'version': self.version,
'uri': uri,
})
data_dump = {
'contacts': contacts,
'blacklist': blacklist,
}
parent_node_id = 'deadbeef'
rt = RoutingTable(parent_node_id)
rt.restore(data_dump)
self.assertEqual(len(rt._buckets[0]), 1)
self.assertEqual(len(rt._blacklist), 1)
示例5: test_find_close_nodes_in_correct_order
def test_find_close_nodes_in_correct_order(self):
"""
Ensures that the nearest nodes are returned in the correct order: from
the node closest to the target key to the node furthest away.
"""
parent_node_id = 'deadbeef'
r = RoutingTable(parent_node_id)
# Fill up the bucket and replacement cache
for i in range(512):
uri = 'netstring://192.168.0.%d:9999/' % i
contact = PeerNode(PUBLIC_KEY, self.version, uri, 0)
contact.network_id = hex(2 ** i)
r.add_contact(contact)
target_key = hex(2 ** 256)
result = r.find_close_nodes(target_key)
self.assertEqual(constants.K, len(result))
# Ensure results are in the correct order.
def key(node):
return distance(node.network_id, target_key)
sorted_nodes = sorted(result, key=key)
self.assertEqual(sorted_nodes, result)
# Ensure the order is from lowest to highest in terms of distance
distances = [distance(x.network_id, target_key) for x in result]
self.assertEqual(sorted(distances), distances)
示例6: test_get_contact_does_not_exist
def test_get_contact_does_not_exist(self):
"""
Ensures that a ValueError is returned if the referenced contact does
not exist in the routing table.
"""
parent_node_id = 'abc'
r = RoutingTable(parent_node_id)
contact1 = Contact('a', '192.168.0.1', 9999, self.version, 0)
r.add_contact(contact1)
self.assertRaises(ValueError, r.get_contact, 'b')
示例7: test_get_contact
def test_get_contact(self):
"""
Ensures that the correct contact is returned.
"""
parent_node_id = 'abc'
r = RoutingTable(parent_node_id)
contact1 = Contact('a', '192.168.0.1', 9999, self.version, 0)
r.add_contact(contact1)
result = r.get_contact('a')
self.assertEqual(contact1, result)
示例8: test_add_contact_with_parent_node_id
def test_add_contact_with_parent_node_id(self):
"""
If the newly discovered contact is, in fact, this node then it's not
added to the routing table.
"""
parent_node_id = 'abc'
r = RoutingTable(parent_node_id)
contact = Contact('abc', '192.168.0.1', 9999, 0)
r.add_contact(contact)
self.assertEqual(len(r._buckets[0]), 0)
示例9: test_random_key_in_bucket_range
def test_random_key_in_bucket_range(self):
"""
Ensures the returned key is within the expected bucket range.
"""
parent_node_id = 'deadbeef'
r = RoutingTable(parent_node_id)
bucket = Bucket(1, 2)
r._buckets[0] = bucket
expected = 1
actual = int(r._random_key_in_bucket_range(0), 0)
self.assertEqual(expected, actual)
示例10: test_add_contact_with_parent_node_id
def test_add_contact_with_parent_node_id(self):
"""
If the newly discovered contact is, in fact, this node then it's not
added to the routing table.
"""
parent_node_id = 'deadbeef'
r = RoutingTable(parent_node_id)
contact = PeerNode(PUBLIC_KEY, '192.168.0.1', 9999, 0)
contact.network_id = parent_node_id
r.add_contact(contact)
self.assertEqual(len(r._buckets[0]), 0)
示例11: test_distance
def test_distance(self):
"""
Sanity check to ensure the XOR'd values return the correct distance.
"""
parent_node_id = 'abc'
r = RoutingTable(parent_node_id)
key1 = 'abc'
key2 = 'xyz'
expected = 1645337L
actual = r.distance(key1, key2)
self.assertEqual(expected, actual)
示例12: test_get_contact_does_not_exist
def test_get_contact_does_not_exist(self):
"""
Ensures that a ValueError is returned if the referenced contact does
not exist in the routing table.
"""
parent_node_id = 'deadbeef'
r = RoutingTable(parent_node_id)
contact1 = PeerNode(PUBLIC_KEY, self.version,
'netstring://192.168.0.1:9999/', 0)
r.add_contact(contact1)
self.assertRaises(ValueError, r.get_contact, 'b')
示例13: test_get_contact
def test_get_contact(self):
"""
Ensures that the correct contact is returned.
"""
parent_node_id = 'deadbeef'
r = RoutingTable(parent_node_id)
contact1 = PeerNode(PUBLIC_KEY, self.version,
'netstring://192.168.0.1:9999/', 0)
contact1.network_id = 'a'
r.add_contact(contact1)
result = r.get_contact('a')
self.assertEqual(contact1, result)
示例14: test_find_close_nodes_fewer_than_K
def test_find_close_nodes_fewer_than_K(self):
"""
Ensures that all close nodes are returned if their number is < K.
"""
parent_node_id = 'abc'
r = RoutingTable(parent_node_id)
# Fill up the bucket and replacement cache
for i in range(10):
contact = Contact(i, "192.168.0.%d" % i, self.version, 0)
r.add_contact(contact)
result = r.find_close_nodes(1)
self.assertEqual(10, len(result))
示例15: test_find_close_nodes_single_kbucket
def test_find_close_nodes_single_kbucket(self):
"""
Ensures K number of closest nodes get returned.
"""
parent_node_id = 'abc'
r = RoutingTable(parent_node_id)
# Fill up the bucket and replacement cache
for i in range(40):
contact = Contact(i, "192.168.0.%d" % i, self.version, 0)
r.add_contact(contact)
result = r.find_close_nodes(1)
self.assertEqual(20, len(result))