use std::{ collections::HashMap };
use std::net::SocketAddr;
use crate::protocols::client_type::{ ClientType, close_client_type };
#[derive(Clone)]
pub struct ConnectedClients {
pub inner: HashMap<String, Vec<(ClientType, Vec<SocketAddr>)>>,
pub external: HashMap<String, Vec<(ClientType, Vec<SocketAddr>)>>,
}
impl ConnectedClients {
pub fn new() -> Self {
Self {
inner: HashMap::new(),
external: HashMap::new(),
}
}
pub fn add_inner(&mut self, address: &str, tcp: ClientType, sockets: Vec<SocketAddr>) {
self.inner.entry(address.to_string()).or_insert_with(Vec::new).push((tcp, sockets));
}
pub fn add_external(&mut self, address: &str, tcp: ClientType, sockets: Vec<SocketAddr>) {
self.external.entry(address.to_string()).or_insert_with(Vec::new).push((tcp, sockets));
}
pub async fn remove_client(&mut self, address: &str) {
let mut streams = Vec::new();
if let Some(v) = self.inner.remove(address) {
for (tcp, _) in v {
streams.push(tcp);
}
}
if let Some(v) = self.external.remove(address) {
for (tcp, _) in v {
streams.push(tcp);
}
}
for tcp in streams {
close_client_type(&tcp).await;
}
}
pub async fn close_all(&mut self) {
for (_, list) in self.inner.drain() {
for (tcp, _) in list {
close_client_type(&tcp).await;
}
}
for (_, list) in self.external.drain() {
for (tcp, _) in list {
close_client_type(&tcp).await;
}
}
}
pub fn get_connections(&self, address: &String, include_external: bool) -> Vec<ClientType> {
let mut result = Vec::new();
if let Some(list) = self.inner.get(address) {
for (tcp, _) in list {
result.push(tcp.clone());
}
}
if include_external {
if let Some(list) = self.external.get(address) {
for (tcp, _) in list {
result.push(tcp.clone());
}
}
}
println!("Found connection(s): {}", result.len());
result
}
}