rustydht_lib/storage/node_wrapper.rs
1use super::buckets::Bucketable;
2use crate::common::{Id, Node};
3use std::time::Instant;
4
5/// Wraps a Node with information about when the DHT first saw, last saw it, and last verified it.
6/// This is used by [NodeStorage](crate::storage::node_bucket_storage::NodeStorage) implementations
7#[derive(Debug, Clone)]
8pub struct NodeWrapper {
9 pub node: Node,
10
11 /// The Instant when this Node was first seen by us on the network
12 pub first_seen: std::time::Instant,
13
14 /// The Instant when this Node was last marked seen by us on the network.
15 pub last_seen: std::time::Instant,
16
17 /// The Instant when this Node was most recently marked as being alive on the network.
18 /// In general, being "verified" requires that the Node has responded to a request
19 /// from us recently.
20 pub last_verified: Option<std::time::Instant>,
21}
22
23impl NodeWrapper {
24 /// Creates a new NodeWrapper.
25 ///
26 /// Initializes `first_seen` and `last_seen` to the current time. Initializes `last_verified` to `None`.
27 pub fn new(node: Node) -> NodeWrapper {
28 let now = std::time::Instant::now();
29 NodeWrapper {
30 node,
31 first_seen: now,
32 last_seen: now,
33 last_verified: None,
34 }
35 }
36}
37
38impl Bucketable for NodeWrapper {
39 fn get_id(&self) -> Id {
40 self.node.id
41 }
42
43 fn get_first_seen(&self) -> Instant {
44 self.first_seen
45 }
46}