UI Utility

This commit is contained in:
Alex Emmet 2025-09-28 18:58:42 +00:00
commit cbd357ce14
14 changed files with 962 additions and 622 deletions

View file

@ -1,7 +1,10 @@
use crate::APP_STATE;
use crate::data::communication::{CommunicationType, CommunicationValue, DataTypes};
use crate::gui::log_panel::AppState;
use crate::omikron::omikron_connection::OmikronConnection;
use color_eyre::owo_colors::OwoColorize;
use futures_util::{SinkExt, StreamExt};
use std::arch::x86_64::_SIDD_MASKED_NEGATIVE_POLARITY;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
@ -10,12 +13,22 @@ use uuid::Uuid;
#[derive(Clone)]
pub struct PingPongTask {
pub parent: Arc<OmikronConnection>, // assuming OmikronConnection is your connection type
pub parent: Arc<OmikronConnection>,
pub message_send_times: Arc<Mutex<HashMap<Uuid, Instant>>>,
pub no_ping_in: Arc<Mutex<i32>>,
pub last_ping: Arc<Mutex<Option<u64>>>,
}
fn assert_send_sync<T: Send + Sync>() {}
#[test]
fn check_omikron_connection_send_sync() {
assert_send_sync::<OmikronConnection>();
}
#[test]
fn check_pingpong_task_send_sync() {
assert_send_sync::<PingPongTask>();
}
impl PingPongTask {
pub fn new(parent: Arc<OmikronConnection>) -> Self {
let message_send_times = Arc::new(Mutex::new(HashMap::new()));
@ -29,50 +42,32 @@ impl PingPongTask {
last_ping: last_ping.clone(),
};
// Spawn the periodic ping task
tokio::spawn({
let task = task.clone(); // Clone the task (Arc) so that it lives long enough for the async task
async move {
task.run_ping_loop().await;
}
});
task
}
pub async fn run_ping_loop(&self) {
loop {
sleep(Duration::from_secs(5)).await;
self.send_ping().await;
}
}
pub async fn send_ping(&self) {
let uuid = Uuid::new_v4();
let send_time = Instant::now();
{
let mut message_send_times = self.message_send_times.lock().await;
pub fn send_ping(&self) {
let sel = self.clone();
tokio::spawn(async move {
let uuid = Uuid::new_v4();
let send_time = Instant::now();
let mut message_send_times = sel.message_send_times.lock().await;
message_send_times.insert(uuid, send_time);
}
let no_ping_in = {
let no_ping_in = self.no_ping_in.lock().await;
*no_ping_in
};
let no_ping_in_val = {
let no_ping_in = sel.no_ping_in.lock().await;
*no_ping_in
};
if no_ping_in != -1 {
// Connection slow or disconnected
self.handle_slow_connection(no_ping_in).await;
} else {
// Connection is fine
self.parent.send_ping_message(uuid).await;
}
if no_ping_in_val != -1 {
sel.handle_slow_connection(no_ping_in_val).await;
} else {
sel.parent.send_ping_message(uuid).await;
}
});
}
pub async fn handle_slow_connection(&self, no_ping_in: i32) {
if no_ping_in > 8 {
// Attempt reconnection if ping times out
self.parent.reconnect().await;
self.reconnect().await;
}
@ -80,37 +75,39 @@ impl PingPongTask {
pub async fn reconnect(&self) {
let mut no_ping_in = self.no_ping_in.lock().await;
*no_ping_in = -1; // Reset slow count
*no_ping_in = -1;
}
pub async fn handle_pong(&self, cv: &CommunicationValue) {
let send_time = {
let message_send_times = self.message_send_times.lock().await;
message_send_times.get(&cv.get_id()).cloned()
};
pub fn handle_pong(&self, cv: &CommunicationValue, log: bool) {
let sel = self.clone();
let cv = cv.clone();
tokio::spawn(async move {
let send_time = {
let message_send_times = sel.message_send_times.lock().await;
message_send_times.get(&cv.get_id()).cloned()
};
if let Some(send_time) = send_time {
let receive_time = Instant::now();
let ping = receive_time.duration_since(send_time).as_millis() as u64;
if let Some(send_time) = send_time {
let receive_time = Instant::now();
let ping = receive_time.duration_since(send_time).as_millis() as u64;
{
let mut last_ping = self.last_ping.lock().await;
let mut last_ping = sel.last_ping.lock().await;
*last_ping = Some(ping);
}
{
let mut no_ping_in = self.no_ping_in.lock().await;
let mut no_ping_in = sel.no_ping_in.lock().await;
*no_ping_in = -1;
let mut message_send_times = sel.message_send_times.lock().await;
message_send_times.remove(&cv.get_id());
if log {
APP_STATE.lock().unwrap().push_log("12.0".to_string());
APP_STATE.lock().unwrap().push_ping_val(12.0);
}
}
}
});
}
pub async fn cancel(&self) {
// Cancel or stop the task
let mut no_ping_in = self.no_ping_in.lock().await;
*no_ping_in = -1; // Reset the counter
// Example of stopping the ping task gracefully
// If using a task join handle or similar
*no_ping_in = -1;
}
}