diff --git a/Cargo.lock b/Cargo.lock index 5dbebeb..caaac27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -137,7 +137,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c" dependencies = [ "memchr", - "regex-automata", + "regex-automata 0.4.8", "serde", ] @@ -517,11 +517,14 @@ dependencies = [ name = "horust-commands-lib" version = "0.1.0" dependencies = [ - "log", + "anyhow", "prost", "prost-build", "serde", "serde_json", + "tokio", + "tracing", + "tracing-test", ] [[package]] @@ -685,6 +688,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.161" @@ -707,6 +716,16 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.22" @@ -719,6 +738,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "memchr" version = "2.7.4" @@ -776,6 +804,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -806,6 +844,35 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.52.6", +] + [[package]] name = "percent-encoding" version = "2.3.1" @@ -1021,6 +1088,15 @@ dependencies = [ "rand_core 0.3.1", ] +[[package]] +name = "redox_syscall" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +dependencies = [ + "bitflags", +] + [[package]] name = "redox_users" version = "0.4.6" @@ -1040,8 +1116,17 @@ checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" dependencies = [ "aho-corasick", "memchr", - "regex-automata", - "regex-syntax", + "regex-automata 0.4.8", + "regex-syntax 0.8.5", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] [[package]] @@ -1052,9 +1137,15 @@ checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.8.5", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.8.5" @@ -1131,6 +1222,12 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "serde" version = "1.0.214" @@ -1184,6 +1281,15 @@ dependencies = [ "serde", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shellexpand" version = "3.1.0" @@ -1199,6 +1305,15 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "slab" version = "0.4.9" @@ -1299,6 +1414,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tinyvec" version = "1.8.0" @@ -1321,13 +1446,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "145f3413504347a2be84393cc8a7d2fb4d863b375909ea59f2158261aa258bbb" dependencies = [ "backtrace", + "bytes", "libc", "mio", + "parking_lot", "pin-project-lite", + "signal-hook-registry", "socket2", + "tokio-macros", "windows-sys 0.52.0", ] +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "toml" version = "0.8.19" @@ -1375,9 +1515,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.32" @@ -1385,6 +1537,57 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "tracing-test" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "557b891436fe0d5e0e363427fc7f217abf9ccd510d5136549847bdcbcd011d68" +dependencies = [ + "tracing-core", + "tracing-subscriber", + "tracing-test-macro", +] + +[[package]] +name = "tracing-test-macro" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04659ddb06c87d233c566112c1c9c5b9e98256d9af50ec3bc9c8327f873a7568" +dependencies = [ + "quote", + "syn", ] [[package]] @@ -1431,6 +1634,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "wait-timeout" version = "0.2.0" diff --git a/commands/Cargo.toml b/commands/Cargo.toml index 1f396fd..eaa34b8 100644 --- a/commands/Cargo.toml +++ b/commands/Cargo.toml @@ -6,10 +6,15 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -log = "^0.4.19" -serde = { version = "^1.0", features = ["derive"] } +serde = { version = "~1.0", features = ["derive"] } serde_json = "~1.0" prost = "~0.13" +anyhow = "~1.0" +tokio = { version = "~1.41", features = ["full"] } +tracing = "0.1" + +[dev-dependencies] +tracing-test = { version = "0.2", features = ["no-env-filter"] } [build-dependencies] -prost-build = { version = "~0.13" } +prost-build = { version = "~0.13" } \ No newline at end of file diff --git a/commands/README.md b/commands/README.md index 9896f1e..d740a40 100644 --- a/commands/README.md +++ b/commands/README.md @@ -1,4 +1,5 @@ ## Compile + To compile this crate, you will need protobuf compiler. On debian-like you can run: -apt-get install protobuf-compiler \ No newline at end of file +apt-get install protobuf-compiler diff --git a/commands/src/lib.rs b/commands/src/lib.rs index 6241c43..21cff63 100644 --- a/commands/src/lib.rs +++ b/commands/src/lib.rs @@ -1,21 +1,108 @@ mod proto; + +use anyhow::{bail, Context, Result}; use proto::tutorial::*; +use std::path::{Path, PathBuf}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::{UnixListener, UnixStream}; +use tracing::info; -pub fn create_large_shirt(color: String) -> Person { - let mut person = Person::default(); - person +pub struct CommandsUdsConnectionHandler { + socket: UnixStream, } +impl CommandsUdsConnectionHandler { + fn get_path(socket_folder: &Path, socket_name: u32) -> PathBuf { + socket_folder.join(format!("hourst-{socket_name}.sock")) + } + fn new(socket: UnixStream) -> Self { + Self { socket } + } + pub async fn new_client(socket_path: PathBuf) -> Result { + Ok(Self { + socket: UnixStream::connect(socket_path) + .await + .context("Could not create stream")?, + }) + } -pub fn add() { - println!("{:?}", create_large_shirt("blue".to_string())); -} + pub async fn client(mut self) -> Result<()> { + info!("client: sending data"); + self.socket + .write_all(b"Hello?") + .await // we write bytes, &[u8] + .context("Failed at writing onto the unix stream")?; + info!("client: Completed."); + // server is waiting for EOF. + self.socket.shutdown().await?; + + let mut buf = String::new(); + info!("client: reading back:"); + //Reads all bytes until EOF in this source, appending them to buf. + self.socket + .read_to_string(&mut buf) + .await // we write bytes, &[u8] + .context("Failed at writing onto the unix stream")?; + info!("Client received: {}", buf); + Ok(()) + } -#[cfg(test)] -mod tests { - use super::*; + pub async fn server(mut self) -> Result<()> { + let mut message = String::new(); + info!("Server: receving data"); + // Reads all bytes until EOF in this source, appending them to buf. + self.socket + .read_to_string(&mut message) + .await + .context("Failed at reading the unix stream")?; + info!("Server: Received data: {message}"); + self.socket + .write_all(message.as_bytes()) + .await + .context("Failed at reading the unix stream")?; - #[test] - fn it_works() { - add(); + info!("Server: has written back {}", message); + Ok(()) } } +pub struct CommandsUdsServer { + unix_listener: UnixListener, +} +impl CommandsUdsServer { + pub async fn new(socket_path: &Path) -> Result { + Ok(Self { + unix_listener: UnixListener::bind(socket_path) + .context("Could not create the unix socket")?, + }) + } + pub async fn start(&mut self) -> Result<()> { + // put the server logic in a loop to accept several connections + loop { + self.accept().await?; + } + Ok(()) + } + pub async fn accept(&mut self) -> Result<()> { + match self.unix_listener.accept().await { + Ok((stream, _addr)) => { + tokio::spawn(async move { + CommandsUdsConnectionHandler::new(stream) + .server() + .await + .unwrap(); + }) + .await? + } + Err(e) => { + bail!("error accepting connction: {e}") + } + }; + Ok(()) + } +} + +fn create_uds() {} + +fn listen_uds() {} + +fn send_message() {} +fn receive_message() {} diff --git a/commands/tests/simple.rs b/commands/tests/simple.rs new file mode 100644 index 0000000..f228a5a --- /dev/null +++ b/commands/tests/simple.rs @@ -0,0 +1,31 @@ +use anyhow::Result; +use horust_commands_lib::{CommandsUdsConnectionHandler, CommandsUdsServer}; +use std::path::PathBuf; +use tracing::info; +use tracing_test::traced_test; + +#[tokio::test] +#[traced_test] +async fn test_simple() -> Result<()> { + info!("Starting"); + + let socket_path: PathBuf = "/tmp/simple.sock".into(); + if socket_path.exists() { + std::fs::remove_file(&socket_path)?; + } + let socket_path2 = socket_path.clone(); + let s_handle = tokio::spawn(async move { + let mut uds = CommandsUdsServer::new(&socket_path2).await.unwrap(); + info!("uds created"); + uds.accept().await.unwrap(); + }); + let c_handle = tokio::spawn(async { + let client = CommandsUdsConnectionHandler::new_client(socket_path) + .await + .unwrap(); + client.client().await.unwrap(); + }); + s_handle.await?; + c_handle.await?; + Ok(()) +}