pub struct Dht<H: DhtHandler> {Show 15 fields
pub handler: RwLock<Weak<H>>,
pub bootstrapped: Arc<RwLock<bool>>,
pub buckets: Arc<RwLock<Vec<DhtBucket<H::Node>>>>,
pub hash_table: DhtHashTable<H::Value>,
pub n_buckets: usize,
pub channel_cache: Arc<RwLock<HashMap<u32, ChannelCacheItem<H::Node>>>>,
pub host_cache: Arc<RwLock<HashMap<Url, HostCacheItem>>>,
ping_locks: Arc<Mutex<HashMap<u32, Arc<Mutex<Option<Result<H::Node>>>>>>>,
pub add_node_tx: Sender<(H::Node, ChannelPtr)>,
pub add_node_rx: Receiver<(H::Node, ChannelPtr)>,
pub settings: DhtSettings,
pub event_publisher: PublisherPtr<DhtEvent<H::Node, H::Value>>,
pub p2p: P2pPtr,
pub connector: Connector,
pub executor: ExecutorPtr,
}Fields§
§handler: RwLock<Weak<H>>DhtHandler that implements application-specific behaviors over a Dht
bootstrapped: Arc<RwLock<bool>>Are we bootstrapped?
buckets: Arc<RwLock<Vec<DhtBucket<H::Node>>>>Vec of buckets
hash_table: DhtHashTable<H::Value>Our local hash table, storing a part of the full DHT keys/values
n_buckets: usizeNumber of buckets
channel_cache: Arc<RwLock<HashMap<u32, ChannelCacheItem<H::Node>>>>Channel ID -> ChannelCacheItem
host_cache: Arc<RwLock<HashMap<Url, HostCacheItem>>>Host address -> ChannelCacheItem
ping_locks: Arc<Mutex<HashMap<u32, Arc<Mutex<Option<Result<H::Node>>>>>>>Locks that prevent pinging the same channel multiple times at once.
add_node_tx: Sender<(H::Node, ChannelPtr)>Add node sender
add_node_rx: Receiver<(H::Node, ChannelPtr)>Add node receiver
settings: DhtSettingsDHT settings
event_publisher: PublisherPtr<DhtEvent<H::Node, H::Value>>DHT event publisher
p2p: P2pPtrP2P network pointer
connector: ConnectorConnector to create manual connections
executor: ExecutorPtrGlobal multithreaded executor reference
Implementations§
Source§impl<H: DhtHandler> Dht<H>
impl<H: DhtHandler> Dht<H>
pub async fn new(settings: &DhtSettings, p2p: P2pPtr, ex: ExecutorPtr) -> Self
pub async fn handler(&self) -> Arc<H>
pub async fn is_bootstrapped(&self) -> bool
pub async fn set_bootstrapped(&self, value: bool)
pub async fn subscribe(&self) -> Subscription<DhtEvent<H::Node, H::Value>>
Sourcepub fn distance(&self, key_1: &Hash, key_2: &Hash) -> [u8; 32]
pub fn distance(&self, key_1: &Hash, key_2: &Hash) -> [u8; 32]
Get the distance between key_1 and key_2
Sourcepub fn sort_by_distance(&self, nodes: &mut [H::Node], key: &Hash)
pub fn sort_by_distance(&self, nodes: &mut [H::Node], key: &Hash)
Sort nodes by distance from key
Sourcepub async fn get_bucket_index(&self, self_node_id: &Hash, key: &Hash) -> usize
pub async fn get_bucket_index(&self, self_node_id: &Hash, key: &Hash) -> usize
key -> bucket index
Sourcepub async fn find_neighbors(&self, key: &Hash, n: usize) -> Vec<H::Node>
pub async fn find_neighbors(&self, key: &Hash, n: usize) -> Vec<H::Node>
Get n closest known nodes to a key
TODO: Can be optimized
Sourcepub async fn get_node_from_channel(&self, channel_id: u32) -> Option<H::Node>
pub async fn get_node_from_channel(&self, channel_id: u32) -> Option<H::Node>
Channel ID -> DhtNode
Sourcepub async fn announce<M: Message>(
&self,
key: &Hash,
value: &H::Value,
message: &M,
) -> Result<()>
pub async fn announce<M: Message>( &self, key: &Hash, value: &H::Value, message: &M, ) -> Result<()>
Add value to our hash table and send message for a key to the closest nodes found
async fn on_new_node(&self, node: &H::Node, channel: ChannelPtr)
Sourcepub async fn update_node(&self, node: &H::Node, channel: ChannelPtr)
pub async fn update_node(&self, node: &H::Node, channel: ChannelPtr)
Move a node to the tail in its bucket,
to show that it is the most recently seen in the bucket.
If the node is not in a bucket it will be added using add_node.
Sourcepub async fn remove_node(&self, node_id: &Hash)
pub async fn remove_node(&self, node_id: &Hash)
Remove a node from the buckets.
Sourcepub async fn ping(&self, channel: ChannelPtr) -> Result<H::Node>
pub async fn ping(&self, channel: ChannelPtr) -> Result<H::Node>
Send a DHT ping to channel using the handler’s ping method.
Prevents sending multiple pings at once to the same channel.
Sourceasync fn lookup(
&self,
key: Hash,
lookup_type: DhtLookupType,
) -> (Vec<H::Node>, Vec<H::Value>)
async fn lookup( &self, key: Hash, lookup_type: DhtLookupType, ) -> (Vec<H::Node>, Vec<H::Value>)
Lookup algorithm for both nodes lookup and value lookup.
Sourcepub async fn lookup_nodes(&self, key: &Hash) -> Vec<H::Node>
pub async fn lookup_nodes(&self, key: &Hash) -> Vec<H::Node>
Find k nodes closest to a key
Sourcepub async fn lookup_value(&self, key: &Hash) -> (Vec<H::Node>, Vec<H::Value>)
pub async fn lookup_value(&self, key: &Hash) -> (Vec<H::Node>, Vec<H::Value>)
Find value for key
Sourcepub async fn update_channel(&self, channel_id: u32)
pub async fn update_channel(&self, channel_id: u32)
Update a channel’s last_used field in the channel cache.
Sourcepub async fn get_channel(&self, node: &H::Node) -> Result<(ChannelPtr, H::Node)>
pub async fn get_channel(&self, node: &H::Node) -> Result<(ChannelPtr, H::Node)>
Get a channel (existing or create a new one) to node.
Don’t forget to call cleanup_channel() once you are done with it.
Sourcepub async fn create_channel(&self, addr: &Url) -> Result<(ChannelPtr, H::Node)>
pub async fn create_channel(&self, addr: &Url) -> Result<(ChannelPtr, H::Node)>
Create a channel in the direct session, ping the peer, add the DHT node to our buckets and the channel to our channel cache.
pub async fn create_channel_to_node( &self, node: &H::Node, ) -> Result<(ChannelPtr, H::Node)>
Sourcepub async fn add_channel_to_cache(&self, channel_id: u32, node: &H::Node)
pub async fn add_channel_to_cache(&self, channel_id: u32, node: &H::Node)
Insert a channel to the DHT’s channel cache. If the channel is already
in the cache, last_used is updated.
Sourcepub async fn wait_fully_pinged(&self, channel_id: u32) -> Result<()>
pub async fn wait_fully_pinged(&self, channel_id: u32) -> Result<()>
Wait until we received a DHT ping and sent a DHT ping on a channel.
Sourcepub async fn cleanup_channel(&self, channel: ChannelPtr)
pub async fn cleanup_channel(&self, channel: ChannelPtr)
Call crate::net::session::DirectSession::cleanup_channel() and cleanup the DHT caches.
Auto Trait Implementations§
impl<H> !Freeze for Dht<H>
impl<H> !RefUnwindSafe for Dht<H>
impl<H> Send for Dht<H>
impl<H> Sync for Dht<H>
impl<H> !Unpin for Dht<H>
impl<H> !UnwindSafe for Dht<H>
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2where
T: SharedNiching<N1, N2>,
N1: Niching<T>,
N2: Niching<T>,
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2where
T: SharedNiching<N1, N2>,
N1: Niching<T>,
N2: Niching<T>,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> Pointee for T
impl<T> Pointee for T
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.