Engine as command executor

This commit is contained in:
2026-07-24 17:36:53 +02:00
parent f036d04d7b
commit 2eb8039490
2 changed files with 39 additions and 17 deletions
+22 -5
View File
@@ -11,11 +11,13 @@ pub struct Engine {
} }
impl Engine { impl Engine {
pub async fn new() -> tokio::io::Result<Self> { pub async fn new() -> tokio::io::Result<Arc<Self>> {
Ok(Self { let config = Arc::new(Mutex::new(Config::new().await?));
terminal_server: TerminalServer::new(),
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<()> { 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; 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(())
}
} }
+17 -12
View File
@@ -1,4 +1,7 @@
use std::{collections::HashMap, sync::Arc}; use std::{
collections::HashMap,
sync::{Arc, Weak},
};
use pulse_wire::{ use pulse_wire::{
PulseWire, PulseWire,
@@ -13,17 +16,21 @@ use tokio::{
sync::Mutex, sync::Mutex,
}; };
use crate::engine::Engine;
#[derive(Debug)] #[derive(Debug)]
pub struct TerminalServer { pub struct TerminalServer {
clients: Mutex<HashMap<usize, OwnedWriteHalf>>, clients: Mutex<HashMap<usize, OwnedWriteHalf>>,
logs: Mutex<Vec<EventLog>>, logs: Mutex<Vec<EventLog>>,
engine: Weak<Engine>,
} }
impl TerminalServer { impl TerminalServer {
pub fn new() -> Arc<Self> { pub fn new(engine: Weak<Engine>) -> Arc<Self> {
Arc::new(Self { Arc::new(Self {
clients: Mutex::new(HashMap::new()), clients: Mutex::new(HashMap::new()),
logs: Mutex::new(Vec::new()), logs: Mutex::new(Vec::new()),
engine,
}) })
} }
@@ -86,21 +93,13 @@ impl TerminalServer {
TerminalClientMessage::ExecuteCommand(command) => { TerminalClientMessage::ExecuteCommand(command) => {
let command = command.as_str(); 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()) (command, args.split(" ").collect())
} else { } else {
(command, Vec::new()) (command, Vec::new())
}; };
match command { self.get_engine().execute_command(command, args).await?;
_ => {
self.error(
"Command executor",
&format!("Command '{}' not found", command),
)
.await?;
}
}
} }
} }
} }
@@ -190,4 +189,10 @@ impl TerminalServer {
pub async fn debug(self: &Arc<Self>, name: &str, message: &str) -> tokio::io::Result<()> { pub async fn debug(self: &Arc<Self>, name: &str, message: &str) -> tokio::io::Result<()> {
self.log(LogKind::Debug, name, message).await self.log(LogKind::Debug, name, message).await
} }
pub fn get_engine(&self) -> Arc<Engine> {
self.engine
.upgrade()
.expect("Failed to upgrade engine(Weak) to Arc")
}
} }