turn_rs/router/interfaces.rs
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
use super::ports::capacity;
use std::net::SocketAddr;
use std::sync::{Arc, RwLock};
use ahash::AHashMap;
#[derive(Clone, Copy, Debug)]
pub struct Interface {
pub addr: SocketAddr,
pub external: SocketAddr,
}
pub struct Interfaces {
map: RwLock<AHashMap<SocketAddr, Arc<Interface>>>,
}
impl Default for Interfaces {
fn default() -> Self {
Self {
map: RwLock::new(AHashMap::with_capacity(capacity())),
}
}
}
impl Interfaces {
/// add interface from addr.
///
/// # Examples
///
/// ```
/// use std::net::SocketAddr;
/// use turn_rs::router::interfaces::*;
///
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
/// let interface = "127.0.0.1:8081".parse::<SocketAddr>().unwrap();
/// let interfaces = Interfaces::default();
///
/// interfaces.insert(addr, interface, interface);
/// let ret = interfaces.get(&addr).unwrap();
/// assert_eq!(ret.addr, interface);
/// assert_eq!(ret.external, interface);
/// ```
pub fn insert(&self, addr: SocketAddr, interface: SocketAddr, external: SocketAddr) {
self.map.write().unwrap().insert(
addr,
Arc::new(Interface {
addr: interface,
external,
}),
);
}
/// get interface from addr.
///
/// # Examples
///
/// ```
/// use std::net::SocketAddr;
/// use turn_rs::router::interfaces::*;
///
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
/// let interface = "127.0.0.1:8081".parse::<SocketAddr>().unwrap();
/// let interfaces = Interfaces::default();
///
/// interfaces.insert(addr, interface, interface);
/// let ret = interfaces.get(&addr).unwrap();
/// assert_eq!(ret.addr, interface);
/// assert_eq!(ret.external, interface);
/// ```
pub fn get(&self, addr: &SocketAddr) -> Option<Interface> {
self.map
.read()
.unwrap()
.get(addr)
.map(|item| *item.as_ref())
}
/// get interface ref from addr.
///
/// # Examples
///
/// ```
/// use std::net::SocketAddr;
/// use std::sync::Arc;
/// use turn_rs::router::interfaces::*;
///
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
/// let interface = "127.0.0.1:8081".parse::<SocketAddr>().unwrap();
/// let interfaces = Interfaces::default();
///
/// interfaces.insert(addr, interface, interface);
/// let ret = interfaces.get_ref(&addr).unwrap();
/// assert_eq!(ret.addr, interface);
/// assert_eq!(ret.external, interface);
/// ```
pub fn get_ref(&self, addr: &SocketAddr) -> Option<Arc<Interface>> {
self.map.read().unwrap().get(addr).cloned()
}
/// remove interface from addr.
///
/// # Examples
///
/// ```
/// use std::net::SocketAddr;
/// use std::sync::Arc;
/// use turn_rs::router::interfaces::*;
///
/// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
/// let interface = "127.0.0.1:8081".parse::<SocketAddr>().unwrap();
/// let interfaces = Interfaces::default();
///
/// interfaces.insert(addr, interface, interface);
/// let ret = interfaces.get(&addr).unwrap();
/// assert_eq!(ret.addr, interface);
/// assert_eq!(ret.external, interface);
///
/// interfaces.remove(&addr);
///
/// let ret = interfaces.get(&addr);
/// assert!(ret.is_none());
/// ```
pub fn remove(&self, addr: &SocketAddr) {
self.map.write().unwrap().remove(addr);
}
}