Better Logging

This commit is contained in:
Alex Emmet 2025-10-26 11:20:00 +01:00
commit ab707c1907
13 changed files with 176 additions and 53 deletions

View file

@ -7,6 +7,9 @@ use tungstenite::Utf8Bytes;
use uuid::Uuid;
use super::{rho_connection::RhoConnection, rho_manager};
use crate::util::print::PrintType;
use crate::util::print::line;
use crate::util::print::line_err;
use crate::{
auth::auth_connector,
// calls::call_manager::CallManager,
@ -84,12 +87,18 @@ impl ClientConnection {
.send(Message::Text(Utf8Bytes::from(message.to_string())))
.await
{
eprintln!("Failed to send message to client: {}", e);
line_err(
PrintType::ClientOut,
&format!("Failed to send message to client: {}", e),
);
}
}
/// Send a CommunicationValue to the client
pub async fn send_message(&self, cv: &CommunicationValue) {
if !cv.is_type(CommunicationType::pong) {
line(PrintType::ClientOut, &cv.to_json().to_string());
}
self.send_message_str(&cv.to_json().to_string()).await;
}
@ -112,7 +121,7 @@ impl ClientConnection {
self.handle_ping(cv).await;
return;
}
line(PrintType::ClientIn, &cv.to_json().to_string());
// Handle client status changes
if cv.is_type(CommunicationType::client_changed) {
self.handle_client_changed(cv).await;
@ -132,7 +141,9 @@ impl ClientConnection {
}
// Forward other messages to Iota
self.forward_to_iota(cv).await;
tokio::spawn(async move {
self.forward_to_iota(cv).await;
});
}
/// Handle identification message
@ -173,7 +184,7 @@ impl ClientConnection {
return;
}
} else {
println!("Missing private key");
line(PrintType::ClientIn, "Missing private key");
self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_private_key)
.await;
return;

View file

@ -1,3 +1,8 @@
use crate::util::print::PrintType;
use crate::util::print::line;
use crate::util::print::line_err;
use ansi_term::Color;
use futures::FutureExt;
use futures::SinkExt;
use json::JsonValue;
use std::{
@ -83,14 +88,17 @@ impl IotaConnection {
.send(Message::Text(Utf8Bytes::from(message.to_string())))
.await
{
eprintln!("Failed to send WebSocket message: {:?}", e);
line_err(
PrintType::IotaOut,
&format!("Failed to send WebSocket message: {:?}", e),
);
}
}
/// Send a CommunicationValue to the Iota
pub async fn send_message(&self, cv: CommunicationValue) {
if !cv.is_type(CommunicationType::pong) {
println!("{}", cv.to_json().to_string());
line(PrintType::IotaOut, &cv.to_json().to_string());
}
self.send_message_str(&cv.to_json().to_string()).await;
}
@ -114,7 +122,7 @@ impl IotaConnection {
self.handle_ping(cv).await;
return;
}
line(PrintType::IotaIn, &cv.to_json().to_string());
// Handle forwarding to other Iotas or clients
let receiver_id = cv.get_receiver();
if !self.get_user_ids().await.contains(&receiver_id)
@ -164,6 +172,8 @@ impl IotaConnection {
if auth_iota_id == iota_id {
validated_user_ids.push(user_id);
}
} else {
line(PrintType::IotaIn, "User ID not found");
}
}
}

View file

@ -1,14 +1,16 @@
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;
use super::{client_connection::ClientConnection, iota_connection::IotaConnection, rho_manager};
use crate::data::{
communication::{CommunicationType, CommunicationValue, DataTypes},
user::UserStatus,
};
use crate::omega::omega_connection::OmegaConnection;
use crate::util::print::PrintType;
use crate::util::print::line;
use crate::util::print::line_err;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;
pub struct RhoConnection {
iota_connection: Arc<IotaConnection>,
@ -130,20 +132,8 @@ impl RhoConnection {
OmegaConnection::close_iota(self.get_iota_id().await).await;
}
/// Send message from Iota to specific client by user ID
pub async fn message_iota_to_client_by_user(&self, user_id: Uuid, message: &str) {
let connections = self.client_connections.read().await;
for connection in connections.iter() {
if let Some(conn_user_id) = connection.get_user_id().await {
if conn_user_id == user_id {
connection.send_message_str(message).await;
}
}
}
}
/// Send message from Iota to specific client
pub async fn message_iota_to_client(&self, cv: CommunicationValue) {
pub async fn message_to_client(&self, cv: CommunicationValue) {
if let Some(receiver_id) = Some(cv.get_receiver()) {
let connections = self.client_connections.read().await;
for connection in connections.iter() {

View file

@ -1,3 +1,7 @@
use super::rho_connection::RhoConnection;
use crate::util::print::PrintType;
use crate::util::print::line;
use crate::util::print::line_err;
use std::{
collections::HashMap,
sync::{Arc, LazyLock},
@ -5,18 +9,22 @@ use std::{
use tokio::sync::RwLock;
use uuid::Uuid;
use super::rho_connection::RhoConnection;
pub static RHO_CONNECTIONS: LazyLock<Arc<RwLock<HashMap<Uuid, Arc<RhoConnection>>>>> =
LazyLock::new(|| Arc::new(RwLock::new(HashMap::new())));
pub async fn get_rho_con_for_user(user_id: Uuid) -> Option<Arc<RhoConnection>> {
let connections = RHO_CONNECTIONS.read().await;
println!("Checking user ID: {:?}", user_id);
line(
PrintType::ClientIn,
&format!("Checking user ID: {:?}", user_id),
);
for rho_connection in connections.values() {
println!(
"Comparing user IDs: {:?}",
rho_connection.get_user_ids().to_vec()
line(
PrintType::ClientIn,
&format!(
"Comparing user IDs: {:?}",
rho_connection.get_user_ids().to_vec()
),
);
if rho_connection.get_user_ids().contains(&user_id) {
return Some(Arc::clone(rho_connection));