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
83
84
85
86
87
88
89
90
91
92
93
use std::{collections::BTreeMap, sync::Mutex};

use twizzler_net::{
    addr::{NodeAddr, ServiceAddr},
    ConnectionId, ListenFlags,
};

use crate::HandleRef;

#[allow(dead_code)]
pub struct EndPoint {
    handle: HandleRef,
    conn_id: ConnectionId,
    info: EndPointKey,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct EndPointKey {
    source: NodeAddr,
    dest: NodeAddr,
    flags: ListenFlags,
    source_service: ServiceAddr,
    dest_service: ServiceAddr,
}

impl EndPointKey {
    pub fn source_address(&self) -> (NodeAddr, ServiceAddr) {
        (self.source, self.source_service)
    }

    pub fn dest_address(&self) -> (NodeAddr, ServiceAddr) {
        (self.dest, self.dest_service)
    }

    pub fn flags(&self) -> ListenFlags {
        self.flags
    }

    pub fn new(
        source: NodeAddr,
        dest: NodeAddr,
        flags: ListenFlags,
        source_service: ServiceAddr,
        dest_service: ServiceAddr,
    ) -> Self {
        Self {
            source,
            dest,
            flags,
            source_service,
            dest_service,
        }
    }
}

lazy_static::lazy_static! {
    static ref ENDPOINTS: Mutex<BTreeMap<EndPointKey, BTreeMap<(u64, ConnectionId), EndPoint>>> = Mutex::new(BTreeMap::new());
}

#[allow(dead_code)]
pub fn foreach_endpoint(info: &EndPointKey, f: impl Fn(&HandleRef, ConnectionId)) {
    let endpoints = ENDPOINTS.lock().unwrap();
    if let Some(map) = endpoints.get(&info) {
        for item in map {
            f(&item.1.handle, item.1.conn_id);
        }
    }
}

pub fn add_endpoint(info: EndPointKey, handle: HandleRef, conn_id: ConnectionId) {
    let mut endpoints = ENDPOINTS.lock().unwrap();
    if let Some(map) = endpoints.get_mut(&info) {
        map.insert(
            (handle.id(), conn_id),
            EndPoint {
                handle,
                conn_id,
                info,
            },
        );
    } else {
        let mut map = BTreeMap::new();
        map.insert(
            (handle.id(), conn_id),
            EndPoint {
                handle,
                conn_id,
                info,
            },
        );
        endpoints.insert(info, map);
    }
}