From 790082674eba5588f2947fff11d2be5d58d9d219 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 22 Jul 2026 19:23:51 +0200 Subject: [PATCH] Preparing terminal communication --- Cargo.lock | 21 ++++++++++++ Cargo.toml | 2 +- pulse-wire/src/lib.rs | 7 ++++ src/engine/main.rs | 2 ++ src/engine/terminal.rs | 72 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/engine/terminal.rs diff --git a/Cargo.lock b/Cargo.lock index 9019c07..9caa2a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,12 @@ version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + [[package]] name = "cc" version = "1.2.67" @@ -446,6 +452,16 @@ version = "1.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" +[[package]] +name = "socket2" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52d1cfed4120b4d927bf7c0f86d2087a4a7d6027c906d9f9d525a80573b9be51" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "syn" version = "2.0.118" @@ -463,8 +479,13 @@ version = "1.52.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe" dependencies = [ + "bytes", + "libc", + "mio", "pin-project-lite", + "socket2", "tokio-macros", + "windows-sys", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ec9ed90..ac45c21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ pulse-ui = { workspace = true } pulse-wire = { workspace = true } chrono = "0.4.45" -tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } +tokio = { workspace = true, features = ["rt-multi-thread", "macros", "net", "fs", "io-util"] } crossterm = { workspace = true } [workspace] diff --git a/pulse-wire/src/lib.rs b/pulse-wire/src/lib.rs index 794082e..5a53401 100644 --- a/pulse-wire/src/lib.rs +++ b/pulse-wire/src/lib.rs @@ -1,5 +1,12 @@ +#[cfg(target_os = "macos")] +use std::path::PathBuf; + pub mod terminal; +pub fn server_path() -> PathBuf { + PathBuf::from("/tmp/pulse-engine.sock") +} + pub trait PulseWire { fn to_com(&self) -> Vec; fn from_com(_com: &mut Vec) -> Self; diff --git a/src/engine/main.rs b/src/engine/main.rs index 4e7f550..70a47b8 100644 --- a/src/engine/main.rs +++ b/src/engine/main.rs @@ -1,3 +1,5 @@ +pub mod terminal; + use std::time::Instant; use pulse_wire::PulseWire; diff --git a/src/engine/terminal.rs b/src/engine/terminal.rs new file mode 100644 index 0000000..ece2b82 --- /dev/null +++ b/src/engine/terminal.rs @@ -0,0 +1,72 @@ +use tokio::{ + io::AsyncReadExt, + net::{ + UnixListener, + unix::{OwnedReadHalf, OwnedWriteHalf}, + }, +}; + +pub struct TerminalServer { + clients: Vec, +} + +impl TerminalServer { + pub fn new() -> Self { + Self { + clients: Vec::new(), + } + } + + pub async fn run(&mut self) -> tokio::io::Result<()> { + let path = pulse_wire::server_path(); + + if path.exists() { + tokio::fs::remove_file(&path).await?; + } + + let listener = UnixListener::bind(&path)?; + + println!("Terminal server listening on {:?}", path); + + loop { + let (stream, _) = listener.accept().await?; + + let (reader, writer) = stream.into_split(); + + self.clients.push(writer); + + tokio::spawn(async move { + if let Err(err) = Self::handle_client(reader).await { + eprintln!("Terminal connection error: {err}"); + } + }); + } + } + + async fn handle_client(mut reader: OwnedReadHalf) -> tokio::io::Result<()> { + let input = tokio::spawn(async move { + let mut buffer = [0u8; 2048]; + + loop { + let size = reader.read(&mut buffer).await?; + + if size == 0 { + break; + } + + println!("Received {} bytes", size); + } + + Ok::<(), tokio::io::Error>(()) + }); + + let _ = tokio::try_join!(input)?; + + Ok(()) + } + + pub async fn broadcast(&mut self) -> tokio::io::Result<()> { + // for client in &mut self.clients {} + Ok(()) + } +}