use std::time::{Duration, Instant};
use super::key::BinaryID;
use super::BucketHeight;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Node<TValue> {
id: BinaryID,
value: TValue,
pub(super) eviction_status: NodeEvictionStatus,
pub(super) seen_at: Instant,
pub(crate) network_id: u8,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum NodeEvictionStatus {
None,
Requested(Instant),
}
impl<TValue> Node<TValue> {
pub fn new(id: BinaryID, value: TValue, network_id: u8) -> Self {
Node {
id,
value,
seen_at: Instant::now(),
eviction_status: NodeEvictionStatus::None,
network_id,
}
}
pub fn calculate_distance(
&self,
other: &Node<TValue>,
) -> Option<BucketHeight> {
self.id.calculate_distance(other.id.as_binary())
}
pub fn id(&self) -> &BinaryID {
&self.id
}
pub fn value(&self) -> &TValue {
&self.value
}
pub(super) fn refresh(&mut self) {
self.eviction_status = NodeEvictionStatus::None;
self.seen_at = Instant::now();
}
pub(super) fn flag_for_check(&mut self) {
self.eviction_status = NodeEvictionStatus::Requested(Instant::now());
}
pub(super) fn is_alive(&self, duration: Duration) -> bool {
self.seen_at.elapsed() < duration
}
pub fn seen_at(&self) -> &Instant {
&self.seen_at
}
}