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
use std::{
    sync::{RwLock}, 
    collections::{BTreeSet, BTreeMap}
};
use cyfs_base::*;
use crate::{
    types::*, 
    dht::*
};

pub struct SnCache {
    known_list: RwLock<BTreeSet<DeviceId>>, 
    active_endpoints: RwLock<BTreeMap<DeviceId, EndpointPair>> 
}

impl SnCache {
    pub fn new() -> Self {
        Self {
            known_list: RwLock::new(BTreeSet::new()),
            active_endpoints: RwLock::new(BTreeMap::new())
        }
    }

    pub fn add_known_sn(&self, sn_list: &Vec<DeviceId>) {
        let mut known_list = self.known_list.write().unwrap();
        for sn in sn_list {
            known_list.insert(sn.clone());
        }
       
    }
    pub fn nearest_sn_of(remote: &DeviceId, sn_list: &[DeviceId]) -> Option<DeviceId> {
        sn_list.iter().min_by(|l, r| l.object_id().distance(remote.object_id()).cmp(&r.object_id().distance(remote.object_id()))).cloned()
    }

    pub fn known_list(&self) -> Vec<DeviceId> {
        self.known_list.read().unwrap().iter().cloned().collect()
    }

    pub fn add_active(&self, sn: &DeviceId, active: EndpointPair) {
        self.active_endpoints.write().unwrap().insert(sn.clone(), active);
    }

    pub fn get_active(&self, sn: &DeviceId) -> Option<EndpointPair> {
        self.active_endpoints.read().unwrap().get(sn).cloned()
    }

    pub fn remove_active(&self, sn: &DeviceId) {
        self.active_endpoints.write().unwrap().remove(sn);
    }
}