From 2eb80394907c2203e726c08c1b927e3b5a50ffe7 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 24 Jul 2026 17:36:53 +0200 Subject: [PATCH] Engine as command executor --- src/engine/engine.rs | 27 ++++++++++++++++++++++----- src/engine/terminal.rs | 29 +++++++++++++++++------------ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/engine/engine.rs b/src/engine/engine.rs index e98ba9f..7506360 100644 --- a/src/engine/engine.rs +++ b/src/engine/engine.rs @@ -11,11 +11,13 @@ pub struct Engine { } impl Engine { - pub async fn new() -> tokio::io::Result { - Ok(Self { - terminal_server: TerminalServer::new(), - config: Arc::new(Mutex::new(Config::new().await?)), - }) + pub async fn new() -> tokio::io::Result> { + let config = Arc::new(Mutex::new(Config::new().await?)); + + Ok(Arc::new_cyclic(|engine| Self { + terminal_server: TerminalServer::new(engine.clone()), + config, + })) } pub async fn spawn_terminal_server(&self) -> tokio::io::Result<()> { @@ -66,4 +68,19 @@ impl Engine { tokio::time::sleep(tokio::time::Duration::from_millis(5000)).await; } } + + pub async fn execute_command(&self, command: &str, _args: Vec<&str>) -> tokio::io::Result<()> { + match command { + _ => { + self.terminal_server + .error( + "Command executor", + &format!("Command '{}' not found", command), + ) + .await?; + } + } + + Ok(()) + } } diff --git a/src/engine/terminal.rs b/src/engine/terminal.rs index e4273fc..4e3a503 100644 --- a/src/engine/terminal.rs +++ b/src/engine/terminal.rs @@ -1,4 +1,7 @@ -use std::{collections::HashMap, sync::Arc}; +use std::{ + collections::HashMap, + sync::{Arc, Weak}, +}; use pulse_wire::{ PulseWire, @@ -13,17 +16,21 @@ use tokio::{ sync::Mutex, }; +use crate::engine::Engine; + #[derive(Debug)] pub struct TerminalServer { clients: Mutex>, logs: Mutex>, + engine: Weak, } impl TerminalServer { - pub fn new() -> Arc { + pub fn new(engine: Weak) -> Arc { Arc::new(Self { clients: Mutex::new(HashMap::new()), logs: Mutex::new(Vec::new()), + engine, }) } @@ -86,21 +93,13 @@ impl TerminalServer { TerminalClientMessage::ExecuteCommand(command) => { let command = command.as_str(); - let (command, _args) = if let Some((command, args)) = command.split_once(" ") { + let (command, args) = if let Some((command, args)) = command.split_once(" ") { (command, args.split(" ").collect()) } else { (command, Vec::new()) }; - match command { - _ => { - self.error( - "Command executor", - &format!("Command '{}' not found", command), - ) - .await?; - } - } + self.get_engine().execute_command(command, args).await?; } } } @@ -190,4 +189,10 @@ impl TerminalServer { pub async fn debug(self: &Arc, name: &str, message: &str) -> tokio::io::Result<()> { self.log(LogKind::Debug, name, message).await } + + pub fn get_engine(&self) -> Arc { + self.engine + .upgrade() + .expect("Failed to upgrade engine(Weak) to Arc") + } }