async-tungstenite

This commit is contained in:
Alex Emmet 2025-11-09 22:01:57 +00:00
commit 1baaad2f76
10 changed files with 423 additions and 272 deletions

View file

@ -1,45 +1,57 @@
use crate::calls::call_group::CallGroup;
use futures::lock::Mutex;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::sync::mpsc::UnboundedSender;
use tungstenite::Utf8Bytes;
use uuid::Uuid;
pub type Tx = UnboundedSender<Utf8Bytes>;
pub struct CallManagerState {
pub call_groups: HashMap<Uuid, Arc<Mutex<CallGroup>>>,
pub static CALL_GROUPS: Lazy<RwLock<Mutex<HashMap<Uuid, Arc<Mutex<CallGroup>>>>>> =
Lazy::new(|| RwLock::new(Mutex::new(HashMap::new())));
pub async fn get_group(call_id: Uuid) -> Option<Arc<Mutex<CallGroup>>> {
CALL_GROUPS
.read()
.await
.lock()
.await
.get_mut(&call_id)
.cloned()
}
pub async fn get_or_create_group(call_id: Uuid, secret: &str) -> Arc<Mutex<CallGroup>> {
let g = CALL_GROUPS.write().await;
if let Some(group) = g.lock().await.get_mut(&call_id) {
group.clone()
} else {
let cg = Arc::new(Mutex::new(CallGroup::new(call_id)));
g.lock().await.insert(call_id, cg.clone());
cg
}
}
impl CallManagerState {
pub fn new() -> Self {
CallManagerState {
call_groups: HashMap::new(),
pub async fn remove_inactive() {
let mut rem = Vec::new();
for cg in CALL_GROUPS.read().await.lock().await.keys() {
if CALL_GROUPS
.write()
.await
.lock()
.await
.get(cg)
.unwrap()
.lock()
.await
.callers
.is_empty()
{
rem.push(cg.clone());
}
}
pub fn get_or_create_group(&mut self, call_id: Uuid, secret: &str) -> Arc<Mutex<CallGroup>> {
let g = self.call_groups.get_mut(&call_id);
if let Some(group) = g {
group.clone()
} else {
let cg = Arc::new(Mutex::new(CallGroup::new(call_id)));
self.call_groups.insert(call_id, cg);
self.call_groups.get(&call_id).unwrap().clone()
}
}
pub async fn remove_inactive(&mut self) {
let mut rem = Vec::new();
for cg in self.call_groups.keys() {
let group = self.call_groups.get(cg).unwrap().lock().await;
if group.callers.is_empty() {
rem.push(cg.clone());
}
}
for cg in rem {
self.call_groups.remove(&cg);
}
for cg in rem {
CALL_GROUPS.write().await.lock().await.remove(&cg);
}
}