Struct kademlia_routing_table::RoutingTable
[−]
[src]
pub struct RoutingTable<T: ContactInfo> { // some fields omitted }
A routing table to manage contacts for a node.
It maintains a list of XorName
s representing connected peer nodes, and provides algorithms for
routing messages.
See the crate documentation for details.
Methods
impl<T: ContactInfo> RoutingTable<T>
[src]
fn new(our_info: T) -> Self
Creates a new routing table for the node with the given name.
fn add(&mut self, info: T) -> Option<AddedNodeDetails<T>>
Adds a contact to the routing table, or updates it.
Returns None
if the contact already existed or was denied (see allow_connection
).
Otherwise it returns AddedNodeDetails
.
fn need_to_add(&self, name: &XorName) -> bool
Returns whether it is desirable to add the given contact to the routing table.
Returns false
if adding the contact in question would not bring the routing table closer
to satisfy the invariant. It returns true
if and only if the new contact would be among
the GROUP_SIZE
closest nodes in its bucket.
fn allow_connection(&self, name: &XorName) -> bool
Returns whether we can allow the given contact to connect to us.
The connection is allowed if:
- they already are one of our contacts,
- we need them in our routing table to satisfy the invariant or
- we are in the close group of one of their bucket addresses.
fn dynamic_quorum_size(&self) -> usize
Returns the current calculated quorum size.
If it is known that the network has at least GROUP_SIZE
nodes, this returns the constant
QUORUM_SIZE
. For networks smaller than that, the quorum might not be reachable, so a
smaller number is computed which represents a strict majority in the current network.
fn furthest_close_bucket(&self) -> usize
Returns the bucket index of the furthest close node, or 0
if the table is empty.
fn remove(&mut self, name: &XorName) -> Option<DroppedNodeDetails>
Removes the contact from the table.
If no entry with that name is found, None
is returned. Otherwise, the entry is removed
from the routing table and DroppedNodeDetails
are returned.
fn target_nodes(&self, dst: Destination, hop: &XorName, count: usize) -> Vec<T>
Returns a collection of nodes to which a message should be sent onwards.
If the message is addressed at a group we are a member of and the previous hop
is not,
this returns all other members of that group once, and an empty collection for all further
copies.
If the message is addressed at an individual node that is directly connected to us, this returns the destination node once, and an empty collection for all further copies.
If we are the individual recipient, it also returns an empty collection.
If none of the above is the case and we are the original sender, it returns the
PARALLELISM
closest nodes to the target.
Otherwise it returns the n
-th closest node to the target if this is the n
-th copy of
the message we are relaying.
Arguments
dst
- The destination of the message.hop
- The name of the node that relayed the message to us, or ourselves if we are the original sender.count
- The number of times we have seen this message before.
fn is_recipient(&self, dst: Destination) -> bool
Returns whether the message is addressed to this node.
If this returns true
, this node is either the single recipient of the message, or a
member of the group authority to which it is addressed. It therefore needs to handle the
message.
fn other_close_nodes(&self, name: &XorName) -> Option<Vec<T>>
Returns the other members of name
's close group, or None
if we are not a member of it.
fn close_nodes(&self, name: &XorName) -> Option<Vec<T>>
Returns the members of name
's close group, or None
if we are not a member of it.
fn is_close(&self, name: &XorName) -> bool
Returns true
if there are fewer than GROUP_SIZE
nodes in our routing table that are
closer to name
than we are.
In other words, it returns true
whenever we cannot rule out that we might be among the
GROUP_SIZE
closest nodes to name
.
If the routing table is filled in such a way that each bucket contains GROUP_SIZE
elements unless there aren't enough such nodes in the network, then this criterion is
actually sufficient! In that case, true
is returned if and only if we are among the
GROUP_SIZE
closest node to name
in the network.
fn len(&self) -> usize
Number of entries in the routing table.
fn is_empty(&self) -> bool
Returns true
if there are no entries in the routing table.
fn our_name(&self) -> &XorName
Returns the name of the node this routing table is for.
fn contains(&self, name: &XorName) -> bool
Returns whether the given node is in the routing table.
fn get(&self, name: &XorName) -> Option<&T>
Returns the contact associated with the given name.
fn find<F>(&self, predicate: F) -> Option<&T> where F: FnMut(&&T) -> bool
Returns an entry that satisfies the given predicate
.
fn closest_nodes_to(&self, target: &XorName, n: usize, ourselves: bool) -> Vec<T>
Returns the n
nodes in our routing table that are closest to target
.
Returns fewer than n
nodes if the routing table doesn't have enough entries. If
ourselves
is true
, this could potentially include ourselves. Otherwise, our own name is
skipped.