Exemplo n.º 1
0
 def _split_bucket(self, old_bucket_index):
     """
     Splits the specified bucket into two new buckets which together
     cover the same range in the key/ID space.
     """
     # Resize the range of the current (old) bucket.
     old_bucket = self._buckets[old_bucket_index]
     split_point = old_bucket.range_max - (
         old_bucket.range_max - old_bucket.range_min) / 2
     # Create a new bucket to cover the range split off from the old
     # bucket.
     new_bucket = Bucket(split_point, old_bucket.range_max)
     old_bucket.range_max = split_point
     # Now, add the new bucket into the routing table.
     self._buckets.insert(old_bucket_index + 1, new_bucket)
     # Finally, copy all nodes that belong to the new bucket into it...
     for contact in old_bucket._contacts:
         if new_bucket.key_in_range(contact.network_id):
             new_bucket.add_contact(contact)
     # ...and remove them from the old bucket
     for contact in new_bucket._contacts:
         old_bucket.remove_contact(contact)