1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 * rust-kad
 * Kademlia node type
 *
 * https://github.com/ryankurte/rust-kad
 * Copyright 2018 Ryan Kurte
 */


use std::time::{Instant};
use std::fmt::Debug;
use std::hash::Hash;

use crate::id::DatabaseId;

#[derive(Clone, Debug, Hash, Eq)]
pub struct Node<Id, Addr> {
    id: Id,
    address: Addr,
    seen: Option<Instant>,
    frozen: bool,
}

impl <Id, Addr> PartialEq for Node<Id, Addr> 
where
    Id: PartialEq,
    Addr: PartialEq,
{
    fn eq(&self, other: &Node<Id, Addr>) -> bool {
        self.id == other.id && self.address == other.address
    }
}

impl <Id, Addr>Node<Id, Addr> 
where 
    Id: DatabaseId + Clone + Debug + 'static,
    Addr: Clone + Debug + 'static,
{
    pub fn new(id: Id, address: Addr) -> Node<Id, Addr> {
        Node{id, address, seen: None, frozen: false}
    }

    pub fn id(&self) -> &Id {
        &self.id
    }

    pub fn address(&self) -> &Addr {
        &self.address
    }

    pub fn set_address(&mut self, address: &Addr) {
        self.address = address.clone();
    }

    pub fn seen(&self) -> Option<Instant> {
        self.seen
    }

    pub fn set_seen(&mut self, seen: Instant) {
        self.seen = Some(seen);
    }
}

impl <Id, Addr> From<(Id, Addr)> for Node<Id, Addr> 
where 
    Id: DatabaseId + Clone + Debug + 'static,
    Addr: Clone + Debug + 'static,
{
    fn from(d: (Id, Addr)) -> Node<Id, Addr> {
        Node::new(d.0, d.1)
    }
}

impl <Id, Addr> Into<(Id, Addr)> for Node<Id, Addr> 
where 
    Id: DatabaseId + Clone + Debug + 'static,
    Addr: Clone + Debug + 'static,
{
    fn into(self) -> (Id, Addr) {
        (self.id, self.address)
    }
}