This commit is contained in:
2026-07-24 15:36:56 +02:00
parent 9926a318a9
commit 7e595a7d0f
2 changed files with 42 additions and 11 deletions
+3 -1
View File
@@ -1,9 +1,11 @@
use std::sync::Arc;
use crate::terminal::TerminalServer; use crate::terminal::TerminalServer;
const WATCH_LIST_SYMBOLS: &[&str] = &["BTC", "ETH", "SOL", "XRP"]; const WATCH_LIST_SYMBOLS: &[&str] = &["BTC", "ETH", "SOL", "XRP"];
pub struct Engine { pub struct Engine {
pub terminal_server: TerminalServer, pub terminal_server: Arc<TerminalServer>,
} }
impl Engine { impl Engine {
+39 -10
View File
@@ -1,6 +1,9 @@
use std::sync::Arc; use std::sync::Arc;
use pulse_wire::{PulseWire, terminal::TerminalClientMessage}; use pulse_wire::{
PulseWire,
terminal::{EventLog, LogKind, TerminalClientMessage},
};
use tokio::{ use tokio::{
io::{AsyncReadExt, AsyncWriteExt}, io::{AsyncReadExt, AsyncWriteExt},
net::{ net::{
@@ -10,19 +13,21 @@ use tokio::{
sync::Mutex, sync::Mutex,
}; };
#[derive(Debug, Clone)] #[derive(Debug)]
pub struct TerminalServer { pub struct TerminalServer {
clients: Arc<Mutex<Vec<OwnedWriteHalf>>>, clients: Mutex<Vec<OwnedWriteHalf>>,
logs: Mutex<Vec<EventLog>>,
} }
impl TerminalServer { impl TerminalServer {
pub fn new() -> Self { pub fn new() -> Arc<Self> {
Self { Arc::new(Self {
clients: Arc::new(Mutex::new(Vec::new())), clients: Mutex::new(Vec::new()),
} logs: Mutex::new(Vec::new()),
})
} }
pub async fn run(&self) -> tokio::io::Result<()> { pub async fn run(self: &Arc<Self>) -> tokio::io::Result<()> {
let path = pulse_wire::server_path(); let path = pulse_wire::server_path();
if path.exists() { if path.exists() {
@@ -50,7 +55,7 @@ impl TerminalServer {
} }
} }
async fn handle_client(&self, mut reader: OwnedReadHalf) -> tokio::io::Result<()> { async fn handle_client(self: &Arc<Self>, mut reader: OwnedReadHalf) -> tokio::io::Result<()> {
loop { loop {
let mut len_buf = [0u8; size_of::<usize>()]; let mut len_buf = [0u8; size_of::<usize>()];
let size = reader.read_exact(&mut len_buf).await?; let size = reader.read_exact(&mut len_buf).await?;
@@ -95,7 +100,7 @@ impl TerminalServer {
} }
pub async fn broadcast( pub async fn broadcast(
&self, self: &Arc<Self>,
message: pulse_wire::terminal::TerminalServerMessage, message: pulse_wire::terminal::TerminalServerMessage,
) -> tokio::io::Result<()> { ) -> tokio::io::Result<()> {
let msg = message.to_com(); let msg = message.to_com();
@@ -123,4 +128,28 @@ impl TerminalServer {
Ok(()) Ok(())
} }
pub async fn log(self: &Arc<Self>, kind: LogKind, name: &str, message: &str) {
self.logs.lock().await.push(EventLog {
kind,
name: name.to_string(),
message: message.to_string(),
});
}
pub async fn info(self: &Arc<Self>, name: &str, message: &str) {
self.log(LogKind::Info, name, message).await
}
pub async fn warn(self: &Arc<Self>, name: &str, message: &str) {
self.log(LogKind::Warn, name, message).await
}
pub async fn error(self: &Arc<Self>, name: &str, message: &str) {
self.log(LogKind::Err, name, message).await
}
pub async fn debug(self: &Arc<Self>, name: &str, message: &str) {
self.log(LogKind::Debug, name, message).await
}
} }