(fix): connections

This commit is contained in:
Alois 2026-07-28 12:44:48 +02:00
commit c999f68aa4
Signed by: alois
SSH key fingerprint: SHA256:GBzT2DXvAuGV9XIV5W3WrzVpjU54FThmxHXdbz95J24
8 changed files with 31 additions and 363 deletions

View file

@ -6,7 +6,7 @@ use crate::util::logger::PrintType;
use crate::{log_cv_in, log_cv_out, log_err, log_in, log_out};
use mtp::codec::{CommunicationType, CommunicationValue, DataType, DataValue};
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::Duration;
use tokio::sync::RwLock;
use uuid::Uuid;
@ -19,7 +19,6 @@ pub struct AppConnection {
pub sender: Arc<MtpSender>,
pub receiver: Arc<MtpReceiver>,
pub ping: Arc<RwLock<i64>>,
pub_key: Arc<RwLock<Option<Vec<u8>>>>,
pub rho_connection: Arc<RwLock<Option<Arc<RhoConnection>>>>,
is_open: Arc<RwLock<bool>>,
@ -29,7 +28,6 @@ impl AppConnection {
pub async fn from_general(general: Arc<GeneralConnection>, user_id: u64) -> Arc<Self> {
Arc::new(Self {
state: general.state.clone(),
ping: Arc::new(RwLock::new(0)),
pub_key: Arc::new(RwLock::new(None)),
rho_connection: general.rho_connection.clone(),
is_open: Arc::new(RwLock::new(true)),
@ -66,12 +64,6 @@ impl AppConnection {
self.user_id
}
/// Get current ping
#[allow(unused)]
pub async fn get_ping(&self) -> i64 {
*self.ping.read().await
}
/// Get RhoConnection if available
pub async fn get_rho_connection(&self) -> Option<Arc<RhoConnection>> {
self.rho_connection.read().await.clone()
@ -87,19 +79,13 @@ impl AppConnection {
);
return;
}
if !cv.is_type(CommunicationType::Pong) && !cv.is_type(CommunicationType::Ping) {
log_cv_out!(PrintType::App, &cv);
}
log_cv_out!(PrintType::App, &cv);
let _ = self.sender.send(&cv).await;
}
/// Handle incoming message from app
pub async fn handle_message(self: Arc<Self>, cv: CommunicationValue) {
tokio::spawn(async move {
if cv.is_type(CommunicationType::Ping) {
self.handle_ping(cv).await;
return;
}
log_cv_in!(PrintType::App, cv);
if cv.is_type(CommunicationType::GetUserData) {
@ -168,36 +154,6 @@ impl AppConnection {
});
}
/// Handle ping message
async fn handle_ping(self: Arc<Self>, cv: CommunicationValue) {
// Update our ping if provided
if let DataValue::SignedNumber(last_ping) = cv.get_data(DataType::LastPing) {
let current = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let mut ping_guard = self.ping.write().await;
*ping_guard = (current as i128 - *last_ping) as i64;
}
// Get Iota ping from RhoConnection
let iota_ping = if let Some(rho_conn) = self.get_rho_connection().await {
rho_conn.get_iota_connection().get_ping().await
} else {
-1
};
// Send pong response
let response = CommunicationValue::new(CommunicationType::Pong)
.with_id(cv.get_id())
.add_typed_default(
DataType::PingIota,
DataValue::SignedNumber(iota_ping.into()),
);
self.send_message(&response).await;
}
/// Forward message to Iota
async fn forward_to_iota(self: Arc<Self>, cv: CommunicationValue) {
let sender_user_id = self.get_user_id().await;
@ -353,7 +309,6 @@ impl Clone for AppConnection {
app_identifier: self.app_identifier.clone(),
app_session: self.app_session,
client_version: self.client_version.clone(),
ping: Arc::clone(&self.ping),
pub_key: Arc::clone(&self.pub_key),
rho_connection: Arc::clone(&self.rho_connection),
is_open: Arc::clone(&self.is_open),

View file

@ -9,7 +9,7 @@ use crate::{log_cv_in, log_cv_out, log_err, log_in, log_out};
use mtp::codec::{CommunicationType, CommunicationValue, DataType, DataValue};
use std::str::FromStr;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::Duration;
use tokio::sync::RwLock;
use trust_dns_resolver::TokioAsyncResolver;
use uuid::Uuid;
@ -22,7 +22,6 @@ pub struct ClientConnection {
pub sender: Arc<MtpSender>,
pub receiver: Arc<MtpReceiver>,
pub ping: Arc<RwLock<i64>>,
pub_key: Arc<RwLock<Option<Vec<u8>>>>,
pub rho_connection: Arc<RwLock<Option<Arc<RhoConnection>>>>,
pub interested_users: Arc<RwLock<Vec<i64>>>,
@ -34,7 +33,6 @@ impl ClientConnection {
pub async fn from_general(general: Arc<GeneralConnection>, user_id: u64) -> Arc<Self> {
Arc::new(Self {
state: general.state.clone(),
ping: Arc::new(RwLock::new(0)),
pub_key: Arc::new(RwLock::new(None)),
rho_connection: general.rho_connection.clone(),
interested_users: Arc::new(RwLock::new(Vec::new())),
@ -72,11 +70,6 @@ impl ClientConnection {
self.user_id
}
/// Get current ping
pub async fn get_ping(&self) -> i64 {
*self.ping.read().await
}
/// Get RhoConnection if available
pub async fn get_rho_connection(&self) -> Option<Arc<RhoConnection>> {
self.rho_connection.read().await.clone()
@ -92,9 +85,7 @@ impl ClientConnection {
);
return;
}
if !cv.is_type(CommunicationType::Pong) && !cv.is_type(CommunicationType::Ping) {
log_cv_out!(PrintType::Client, &cv);
}
log_cv_out!(PrintType::Client, &cv);
let _ = self.sender.send(&cv).await;
}
@ -105,10 +96,6 @@ impl ClientConnection {
};
tokio::spawn(async move {
let _permit = permit;
if cv.is_type(CommunicationType::ClientPing) {
self.handle_ping(cv).await;
return;
}
log_cv_in!(PrintType::Client, cv);
let mut cv = cv;
@ -311,36 +298,6 @@ impl ClientConnection {
}
}
/// Handle ping message
async fn handle_ping(self: Arc<Self>, cv: CommunicationValue) {
// Update our ping if provided
if let DataValue::SignedNumber(last_ping) = cv.get_data(DataType::LastPing) {
let current = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let mut ping_guard = self.ping.write().await;
*ping_guard = (current as i128 - *last_ping) as i64;
}
// Get Iota ping from RhoConnection
let iota_ping = if let Some(rho_conn) = self.get_rho_connection().await {
rho_conn.get_iota_connection().get_ping().await
} else {
-1
};
// Send pong response
let response = CommunicationValue::new(CommunicationType::ClientPing)
.with_id(cv.get_id())
.add_typed_default(
DataType::PingIota,
DataValue::SignedNumber(iota_ping.into()),
);
self.send_message(&response).await;
}
/// Handle client status change
async fn handle_client_changed(self: Arc<Self>, cv: CommunicationValue) {
let user_id = self.get_user_id().await;
@ -917,7 +874,6 @@ impl Clone for ClientConnection {
user_id: self.user_id,
session_id: self.session_id,
client_version: self.client_version.clone(),
ping: Arc::clone(&self.ping),
pub_key: Arc::clone(&self.pub_key),
rho_connection: Arc::clone(&self.rho_connection),
interested_users: Arc::clone(&self.interested_users),

View file

@ -31,7 +31,6 @@ pub struct IotaConnection {
pub sender: Arc<MtpSender>,
pub receiver: Arc<MtpReceiver>,
pub user_ids: Arc<RwLock<Vec<u64>>>,
pub ping: Arc<RwLock<i64>>,
pub_key: Arc<RwLock<Option<Vec<u8>>>>,
pub waiting_tasks:
DashMap<u32, Box<dyn Fn(Arc<IotaConnection>, CommunicationValue) -> bool + Send + Sync>>,
@ -44,7 +43,6 @@ impl IotaConnection {
pub async fn from_general(general: Arc<GeneralConnection>, iota_id: u64) -> Arc<Self> {
Arc::new(Self {
state: general.state.clone(),
ping: Arc::new(RwLock::new(0)),
pub_key: Arc::new(RwLock::new(None)),
rho_connection: general.rho_connection.clone(),
user_ids: Arc::new(RwLock::new(Vec::new())),
@ -151,11 +149,6 @@ impl IotaConnection {
.push(cv);
}
/// Get current ping
pub async fn get_ping(&self) -> i64 {
*self.ping.read().await
}
/// Set the RhoConnection reference
pub async fn set_rho_connection(&self, rho_connection: Arc<RhoConnection>) {
let mut rho_ref = self.rho_connection.write().await;
@ -174,9 +167,7 @@ impl IotaConnection {
/// Send a CommunicationValue to the Iota
pub async fn send_message(&self, cv: &CommunicationValue) {
if !cv.is_type(CommunicationType::Pong) {
log_cv_out!(PrintType::Iota, cv);
}
log_cv_out!(PrintType::Iota, cv);
if let Err(e) = self.sender.send(&cv).await {
log_err!(
self.iota_id as i64,
@ -200,12 +191,6 @@ impl IotaConnection {
}
}
// Handle ping
if cv.is_type(CommunicationType::Ping) || cv.is_type(CommunicationType::Pong) {
self.handle_ping(cv).await;
return;
}
log_cv_in!(PrintType::Iota, cv);
let cv = if cv.is_type(CommunicationType::ClientStateSync) {
@ -452,46 +437,6 @@ impl IotaConnection {
}
}
}
/// Handle ping message
async fn handle_ping(&self, cv: CommunicationValue) {
if let DataValue::SignedNumber(last_ping) = cv.get_data(DataType::LastPing) {
if let Ok(ping_val) = last_ping.to_string().parse::<i64>() {
let mut ping_guard = self.ping.write().await;
*ping_guard = ping_val;
}
}
let client_pings = if let Some(rho_conn) = self.get_rho_connection().await {
rho_conn.get_client_pings().await
} else {
HashMap::new()
};
let tm = TypeMap::latest();
let pings: Vec<DataValue> = client_pings
.into_iter()
.map(|(k, v)| {
let mut map = BTreeMap::new();
if let Ok(uid) = k.parse::<i128>() {
map.insert(
data_type_id(DataType::UserId, &tm),
DataValue::SignedNumber(uid),
);
}
map.insert(
data_type_id(DataType::LastPing, &tm),
DataValue::SignedNumber(v.into()),
);
DataValue::container_from_map(&map)
})
.collect();
let response = CommunicationValue::new(CommunicationType::Pong)
.with_id(cv.get_id())
.add_typed_default(DataType::PingClients, DataValue::Array(pings));
self.send_message(&response).await;
}
/// Handle message forwarding to other Iotas
async fn handle_forward_message(&self, cv: CommunicationValue) {
let receiver_id = cv.get_receiver();

View file

@ -3,7 +3,6 @@ use super::{client_connection::ClientConnection, iota_connection::IotaConnection
use crate::{data::user::UserStatus, rho::app_connection::AppConnection};
use dashmap::DashMap;
use mtp::codec::{CommunicationValue, DataType};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;
@ -263,19 +262,6 @@ impl RhoConnection {
}
}
/// Get ping information for all clients
pub async fn get_client_pings(&self) -> HashMap<String, i64> {
let connections = self.get_client_connections().await;
let mut pings = HashMap::new();
for connection in connections.iter() {
let user_id = connection.get_user_id().await;
pings.insert(user_id.to_string(), connection.get_ping().await);
}
pings
}
/// Check if this RhoConnection contains a specific user ID
#[allow(dead_code)]
pub async fn contains_user(&self, user_id: &i64) -> bool {