Calling Logging fixing

This commit is contained in:
Alex Emmet 2025-11-13 23:13:55 +00:00
commit 3cb2f0ec5e
12 changed files with 118 additions and 112 deletions

View file

@ -1,10 +1,7 @@
use async_tungstenite::{
WebSocketReceiver, WebSocketSender, WebSocketStream, tungstenite::Message,
};
use futures::SinkExt;
use async_tungstenite::{WebSocketReceiver, WebSocketSender, tungstenite::Message};
use std::sync::Arc;
use tokio::sync::{
Mutex, RwLock,
RwLock,
mpsc::{UnboundedSender, unbounded_channel},
};
use tokio_util::compat::Compat;
@ -101,6 +98,15 @@ impl CallConnection {
}
let group = call_manager::get_or_create_group(cid, secret_sha).await;
if let None = group {
self.send_message(&CommunicationValue::new(
CommunicationType::error_invalid_secret,
))
.await;
return;
}
let group = group.unwrap();
// Build broadcast
let broadcast = CommunicationValue::new(CommunicationType::client_connected)
.with_id(cv.get_id().clone())
@ -113,7 +119,6 @@ impl CallConnection {
.broadcast(&broadcast.to_json().to_string());
}
// Add member to group
{
group.lock().await.add_member(uid, self.tx.clone());
}
@ -244,6 +249,7 @@ impl CallConnection {
.to_json()
.to_string(),
);
group.lock().await.get_member(uid).await;
group.lock().await.remove_member(uid);
}
call_manager::remove_inactive().await;

View file

@ -1,24 +1,30 @@
use crate::calls::caller::Caller;
use std::collections::HashMap;
use std::{collections::HashMap, sync::Arc};
use tokio::sync::mpsc::UnboundedSender;
use tungstenite::Utf8Bytes;
use uuid::Uuid;
pub struct CallGroup {
pub call_id: Uuid,
pub callers: HashMap<Uuid, Caller>,
pub callers: HashMap<Uuid, Arc<Caller>>,
pub secret_hash: String,
}
impl CallGroup {
pub fn new(call_id: Uuid) -> Self {
pub fn new(secret_hash: String) -> Self {
Self {
call_id,
callers: HashMap::new(),
secret_hash: secret_hash,
}
}
pub fn add_member(&mut self, user_id: Uuid, tx: UnboundedSender<Utf8Bytes>) {
self.callers.insert(user_id, Caller { user_id, tx });
self.callers
.insert(user_id, Arc::new(Caller::new(user_id, tx)));
}
pub fn disconnect_member(&mut self, user_id: Uuid) {
if let Some(caller) = self.callers.get(&user_id) {
caller.disconnect();
}
}
pub fn remove_member(&mut self, user_id: Uuid) {
@ -26,7 +32,12 @@ impl CallGroup {
}
pub fn is_empty(&self) -> bool {
self.callers.is_empty()
for caller in self.callers.values() {
if !caller.is_connected() {
return false;
}
}
true
}
pub fn send_to(&self, user_id: &Uuid, message: &str) {
@ -41,7 +52,7 @@ impl CallGroup {
}
}
pub fn caller_state_mut(&mut self, user_id: &Uuid) -> Option<&mut Caller> {
self.callers.get_mut(user_id)
pub fn caller_state_mut(&mut self, user_id: &Uuid) -> Option<&Arc<Caller>> {
self.callers.get(user_id)
}
}

View file

@ -4,12 +4,8 @@ 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 static CALL_GROUPS: Lazy<RwLock<Mutex<HashMap<Uuid, Arc<Mutex<CallGroup>>>>>> =
Lazy::new(|| RwLock::new(Mutex::new(HashMap::new())));
@ -22,14 +18,18 @@ pub async fn get_group(call_id: Uuid) -> Option<Arc<Mutex<CallGroup>>> {
.get_mut(&call_id)
.cloned()
}
pub async fn get_or_create_group(call_id: Uuid, secret: &str) -> Arc<Mutex<CallGroup>> {
pub async fn get_or_create_group(call_id: Uuid, secret: &str) -> Option<Arc<Mutex<CallGroup>>> {
let g = CALL_GROUPS.write().await;
if let Some(group) = g.lock().await.get_mut(&call_id) {
group.clone()
if group.lock().await.secret_hash.eq(secret) {
Some(group.clone())
} else {
None
}
} else {
let cg = Arc::new(Mutex::new(CallGroup::new(call_id)));
let cg = Arc::new(Mutex::new(CallGroup::new(secret.to_string())));
g.lock().await.insert(call_id, cg.clone());
cg
Some(cg)
}
}
@ -45,7 +45,6 @@ pub async fn remove_inactive() {
.unwrap()
.lock()
.await
.callers
.is_empty()
{
rem.push(cg.clone());

View file

@ -1,14 +1,49 @@
use std::sync::Arc;
use futures::lock::Mutex;
use tokio::sync::mpsc::UnboundedSender;
use tungstenite::Utf8Bytes;
use uuid::Uuid;
#[derive(Debug, Clone)]
pub struct Caller {
pub user_id: Uuid,
pub tx: UnboundedSender<Utf8Bytes>,
pub user_id: Mutex<Uuid>,
pub tx: Mutex<Option<UnboundedSender<Utf8Bytes>>>,
pub user_state: Mutex<CallUserState>,
pub streaming: Mutex<bool>,
}
#[derive(Clone)]
pub enum CallUserState {
Active,
Muted,
Deafed,
Disconnected,
}
impl Caller {
pub fn new(user_id: Uuid, tx: UnboundedSender<Utf8Bytes>) -> Self {
Self {
user_id: Mutex::new(user_id),
tx: Mutex::new(Some(tx)),
user_state: Mutex::new(CallUserState::Active),
streaming: Mutex::new(false),
}
}
pub fn send(&self, msg: impl Into<Utf8Bytes>) {
let _ = self.tx.send(msg.into());
if let Some(tx) = &self.tx.lock().await {
let _ = tx.send(msg.into());
}
}
pub fn disconnect(self: Arc<Self>) {
self.user_state = CallUserState::Disconnected;
self.tx = None;
}
pub fn is_connected(&self) -> bool {
if let CallUserState::Disconnected = self.user_state {
false
} else {
true
}
}
}