[Add] basic split
This commit is contained in:
parent
a0ff6b1082
commit
3cdf7c62d5
77 changed files with 506 additions and 648 deletions
13
omikron-connector/Cargo.toml
Normal file
13
omikron-connector/Cargo.toml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "omikron-connector"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
ttp-core = { git = "https://github.com/Tensamin/TTP.git", package = "ttp-core" }
|
||||
ttp-native = { git = "https://github.com/Tensamin/TTP.git", package = "ttp-native" }
|
||||
|
||||
dashmap = "6.1.0"
|
||||
json = "*"
|
||||
tokio = { version = "1.50.0", features = ["full"] }
|
||||
uuid = { version = "*", features = ["v4"] }
|
||||
2
omikron-connector/src/lib.rs
Normal file
2
omikron-connector/src/lib.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod omikron_connection;
|
||||
pub mod ping_pong_task;
|
||||
1172
omikron-connector/src/omikron_connection.rs
Executable file
1172
omikron-connector/src/omikron_connection.rs
Executable file
File diff suppressed because it is too large
Load diff
38
omikron-connector/src/ping_pong_task.rs
Normal file
38
omikron-connector/src/ping_pong_task.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use crate::omikron::omikron_connection::OmikronConnection;
|
||||
use crate::{APP_STATE, log};
|
||||
use dashmap::DashMap;
|
||||
use std::sync::LazyLock;
|
||||
use std::time::Instant;
|
||||
use tokio::time::Duration;
|
||||
use ttp_core::{CommunicationType, CommunicationValue, DataTypes, DataValue, rand_u32};
|
||||
|
||||
static PING_TIMES: LazyLock<DashMap<u32, Instant>> = LazyLock::new(|| DashMap::new());
|
||||
|
||||
impl OmikronConnection {
|
||||
pub async fn send_ping(&self) {
|
||||
let id = rand_u32();
|
||||
|
||||
PING_TIMES.insert(id, Instant::now());
|
||||
|
||||
PING_TIMES.retain(|_, v| v.elapsed() < Duration::from_secs(30));
|
||||
|
||||
let ping_message = CommunicationValue::new(CommunicationType::ping)
|
||||
.with_id(id)
|
||||
.add_data(
|
||||
DataTypes::last_ping,
|
||||
DataValue::Array(vec![DataValue::Number(*self.last_ping.lock().await)]),
|
||||
);
|
||||
|
||||
self.send_message(&ping_message).await;
|
||||
}
|
||||
|
||||
pub async fn handle_pong(&self, cv: &CommunicationValue) {
|
||||
let id = cv.get_id();
|
||||
|
||||
if let Some((_, send_time)) = PING_TIMES.remove(&id) {
|
||||
let ping_ms = Instant::now().duration_since(send_time).as_millis() as i64;
|
||||
*self.last_ping.lock().await = ping_ms;
|
||||
APP_STATE.lock().unwrap().push_ping_val(ping_ms as f64);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue