[][src]Struct libp2p_kad::Kademlia

pub struct Kademlia<TStore> { /* fields omitted */ }

Network behaviour that handles Kademlia.


impl<TStore> Kademlia<TStore> where
    TStore: RecordStore<'a>, 

pub fn new(id: PeerId, store: TStore) -> Self[src]

Creates a new Kademlia network behaviour with the given configuration.

pub fn with_config(id: PeerId, store: TStore, config: KademliaConfig) -> Self[src]

Creates a new Kademlia network behaviour with the given configuration.

pub fn add_address(&mut self, peer: &PeerId, address: Multiaddr)[src]

Adds a known listen address of a peer participating in the DHT to the routing table.

Explicitly adding addresses of peers serves two purposes:

  1. In order for a node to join the DHT, it must know about at least one other node of the DHT.

  2. When a remote peer initiates a connection and that peer is not yet in the routing table, the Kademlia behaviour must be informed of an address on which that peer is listening for connections before it can be added to the routing table from where it can subsequently be discovered by all peers in the DHT.

If the routing table has been updated as a result of this operation, a KademliaEvent::RoutingUpdated event is emitted.

pub fn kbuckets_entries(&mut self) -> impl Iterator<Item = &PeerId>[src]

Returns an iterator over all peer IDs of nodes currently contained in a bucket of the Kademlia routing table.

pub fn get_closest_peers<K>(&mut self, key: K) where
    K: AsRef<[u8]> + Clone

Performs a lookup for the closest peers to the given key.

The result of this operation is delivered in KademliaEvent::GetClosestPeersResult.

pub fn get_record(&mut self, key: &Key, quorum: Quorum)[src]

Performs a lookup for a record in the DHT.

The result of this operation is delivered in KademliaEvent::GetRecordResult.

pub fn put_record(&mut self, record: Record, quorum: Quorum)[src]

Stores a record in the DHT.

The result of this operation is delivered in KademliaEvent::PutRecordResult.

The record is always stored locally with the given expiration. If the record's expiration is None, the common case, it does not expire in local storage but is still replicated with the configured record TTL. To remove the record locally and stop it from being re-published in the DHT, see Kademlia::remove_record.

After the initial publication of the record, it is subject to (re-)replication and (re-)publication as per the configured intervals. Periodic (re-)publication does not update the record's expiration in local storage, thus a given record with an explicit expiration will always expire at that instant and until then is subject to regular (re-)replication and (re-)publication.

pub fn remove_record(&mut self, key: &Key)[src]

Removes the record with the given key from local storage, if the local node is the publisher of the record.

Has no effect if a record for the given key is stored locally but the local node is not a publisher of the record.

This is a local operation. However, it also has the effect that the record will no longer be periodically re-published, allowing the record to eventually expire throughout the DHT.

pub fn store_mut(&mut self) -> &mut TStore[src]

Gets a mutable reference to the record store.

pub fn bootstrap(&mut self)[src]

Bootstraps the local node to join the DHT.

Bootstrapping is a multi-step operation that starts with a lookup of the local node's own ID in the DHT. This introduces the local node to the other nodes in the DHT and populates its routing table with the closest neighbours.

Subsequently, all buckets farther from the bucket of the closest neighbour are refreshed by initiating an additional bootstrapping query for each such bucket with random keys.

The result(s) of this operation are delivered in KademliaEvent::BootstrapResult, with one event per bootstrapping query.

Note: Bootstrapping requires at least one node of the DHT to be known. See Kademlia::add_address.

pub fn start_providing(&mut self, key: Key)[src]

Establishes the local node as a provider of a value for the given key.

This operation publishes a provider record with the given key and identity of the local node to the peers closest to the key, thus establishing the local node as a provider.

The publication of the provider records is periodically repeated as per the configured interval, to renew the expiry and account for changes to the DHT topology. A provider record may be removed from local storage and thus no longer re-published by calling Kademlia::stop_providing.

In contrast to the standard Kademlia push-based model for content distribution implemented by Kademlia::put_record, the provider API implements a pull-based model that may be used in addition or as an alternative. The means by which the actual value is obtained from a provider is out of scope of the libp2p Kademlia provider API.

The results of the (repeated) provider announcements sent by this node are delivered in AddProviderResult.

pub fn stop_providing(&mut self, key: &Key)[src]

Stops the local node from announcing that it is a provider for the given key.

This is a local operation. The local node will still be considered as a provider for the key by other nodes until these provider records expire.

pub fn get_providers(&mut self, key: Key)[src]

Performs a lookup for providers of a value to the given key.

The result of this operation is delivered in KademliaEvent::GetProvidersResult.

Trait Implementations

impl<TStore> NetworkBehaviour for Kademlia<TStore> where
    TStore: RecordStore<'a>,
    TStore: Send + 'static, 

type ProtocolsHandler = KademliaHandler<QueryId>

Handler for all the protocols the network behaviour supports.

type OutEvent = KademliaEvent

Event generated by the NetworkBehaviour and that the swarm will report back.

Auto Trait Implementations

impl<TStore> !RefUnwindSafe for Kademlia<TStore>

impl<TStore> Send for Kademlia<TStore> where
    TStore: Send

impl<TStore> Sync for Kademlia<TStore> where
    TStore: Sync

impl<TStore> Unpin for Kademlia<TStore> where
    TStore: Unpin

impl<TStore> !UnwindSafe for Kademlia<TStore>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized

impl<T> Borrow<T> for T where
    T: ?Sized

impl<T> BorrowMut<T> for T where
    T: ?Sized

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,