2021-11-22 16:28:30 +00:00
|
|
|
use super::*;
|
|
|
|
use crate::dht::*;
|
|
|
|
use alloc::fmt;
|
|
|
|
|
|
|
|
pub struct NodeRef {
|
|
|
|
routing_table: RoutingTable,
|
|
|
|
node_id: DHTKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeRef {
|
|
|
|
pub fn new(routing_table: RoutingTable, key: DHTKey, entry: &mut BucketEntry) -> Self {
|
|
|
|
entry.ref_count += 1;
|
|
|
|
Self {
|
2021-11-26 15:39:43 +00:00
|
|
|
routing_table,
|
2021-11-22 16:28:30 +00:00
|
|
|
node_id: key,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn node_id(&self) -> DHTKey {
|
|
|
|
self.node_id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn operate<T, F>(&self, f: F) -> T
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut BucketEntry) -> T,
|
|
|
|
{
|
|
|
|
self.routing_table.operate_on_bucket_entry(self.node_id, f)
|
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
pub fn peer_info(&self) -> PeerInfo {
|
|
|
|
self.operate(|e| e.peer_info(self.node_id()))
|
|
|
|
}
|
2022-04-08 14:17:09 +00:00
|
|
|
pub fn node_info(&self) -> NodeInfo {
|
|
|
|
self.operate(|e| e.node_info().clone())
|
|
|
|
}
|
2022-04-16 15:18:54 +00:00
|
|
|
pub fn local_node_info(&self) -> LocalNodeInfo {
|
|
|
|
self.operate(|e| e.local_node_info().clone())
|
2022-04-08 14:17:09 +00:00
|
|
|
}
|
2022-04-16 15:18:54 +00:00
|
|
|
pub fn has_seen_our_node_info(&self) -> bool {
|
|
|
|
self.operate(|e| e.has_seen_our_node_info())
|
2022-03-25 02:07:55 +00:00
|
|
|
}
|
2022-04-16 15:18:54 +00:00
|
|
|
pub fn set_seen_our_node_info(&self) {
|
|
|
|
self.operate(|e| e.set_seen_our_node_info(true));
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
pub fn last_connection(&self) -> Option<ConnectionDescriptor> {
|
2022-04-16 15:18:54 +00:00
|
|
|
self.operate(|e| e.last_connection())
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for NodeRef {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
self.operate(move |e| {
|
|
|
|
e.ref_count += 1;
|
|
|
|
});
|
|
|
|
Self {
|
|
|
|
routing_table: self.routing_table.clone(),
|
2021-11-26 15:39:43 +00:00
|
|
|
node_id: self.node_id,
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for NodeRef {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-04-16 15:18:54 +00:00
|
|
|
write!(f, "{}", self.node_id.encode())
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for NodeRef {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.routing_table.drop_node_ref(self.node_id);
|
|
|
|
}
|
|
|
|
}
|