From 6d1582792cc65340c604734e759a5b61ae01b013 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Tue, 24 Jan 2023 14:17:32 +0100 Subject: [PATCH] Add sample server + client unix socket --- Cargo.lock | 72 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 +++-- src/client/bin/main.rs | 22 ----------- src/client/main.rs | 41 ++++++++++++++++++++ src/common/internal_rpc.rs | 0 src/common/mod.rs | 1 + src/daemon/{bin => }/main.rs | 7 +++- src/lib.rs | 1 + 8 files changed, 127 insertions(+), 27 deletions(-) delete mode 100644 src/client/bin/main.rs create mode 100644 src/client/main.rs create mode 100644 src/common/internal_rpc.rs create mode 100644 src/common/mod.rs rename src/daemon/{bin => }/main.rs (85%) create mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index b301a60..03ac331 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,4 +13,76 @@ name = "command_gateway" version = "0.1.0" dependencies = [ "anyhow", + "jsonrpc-derive", ] + +[[package]] +name = "jsonrpc-derive" +version = "18.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b939a78fa820cdfcb7ee7484466746a7377760970f6f9c6fe19f9edcc8a38d2" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml", +] + +[[package]] +name = "proc-macro2" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" diff --git a/Cargo.toml b/Cargo.toml index f1a5ebb..c747ed1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,13 +5,17 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +crate-type = ["cdylib"] + [[bin]] name = "daemon" -path = "src/daemon/bin/main.rs" +path = "src/daemon/main.rs" [[bin]] name = "client" -path = "src/client/bin/main.rs" +path = "src/client/main.rs" [dependencies] -anyhow = "^1.0.42" \ No newline at end of file +anyhow = "^1.0" +jsonrpc-derive = "18.0" \ No newline at end of file diff --git a/src/client/bin/main.rs b/src/client/bin/main.rs deleted file mode 100644 index 16a3b07..0000000 --- a/src/client/bin/main.rs +++ /dev/null @@ -1,22 +0,0 @@ -use std::os::unix::net::{UnixListener, UnixStream}; -use std::io::{Read, Write}; - -use anyhow::Context; - -fn main() -> anyhow::Result<()> { - let socket_path = "mysocket"; - - let mut unix_stream = - UnixStream::connect(socket_path).context("Could not create stream")?; - - unix_stream - .write(b"Hello?") // we write bytes, &[u8] - .context("Failed at writing onto the unix stream")?; - - Ok(()) -} - -fn handle_stream(mut stream: UnixStream) -> anyhow::Result<()> { - // to be filled - Ok(()) -} \ No newline at end of file diff --git a/src/client/main.rs b/src/client/main.rs new file mode 100644 index 0000000..a693e1c --- /dev/null +++ b/src/client/main.rs @@ -0,0 +1,41 @@ +use std::os::unix::net::{UnixListener, UnixStream}; +use std::io::{Read, Write}; + +use anyhow::Context; + +fn main() -> anyhow::Result<()> { + let socket_path = "mysocket"; + + let mut unix_stream = + UnixStream::connect(socket_path).context("Could not create stream")?; + + write_request_and_shutdown(&mut unix_stream)?; + read_from_stream(&mut unix_stream)?; + + Ok(()) +} + +fn write_request_and_shutdown(unix_stream: &mut UnixStream) -> anyhow::Result<()> { + unix_stream + .write(b"Hello?") + .context("Failed at writing onto the unix stream")?; + + println!("We sent a request"); + println!("Shutting down writing on the stream, waiting for response..."); + + unix_stream + .shutdown(std::net::Shutdown::Write) + .context("Could not shutdown writing on the stream")?; + + Ok(()) +} + +fn read_from_stream(unix_stream: &mut UnixStream) -> anyhow::Result<()> { + let mut response = String::new(); + unix_stream + .read_to_string(&mut response) + .context("Failed at reading the unix stream")?; + + println!("We received this response: {}", response); + Ok(()) +} \ No newline at end of file diff --git a/src/common/internal_rpc.rs b/src/common/internal_rpc.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/common/mod.rs b/src/common/mod.rs new file mode 100644 index 0000000..219c060 --- /dev/null +++ b/src/common/mod.rs @@ -0,0 +1 @@ +pub mod internal_rpc; \ No newline at end of file diff --git a/src/daemon/bin/main.rs b/src/daemon/main.rs similarity index 85% rename from src/daemon/bin/main.rs rename to src/daemon/main.rs index 5d2d672..a2b7f29 100644 --- a/src/daemon/bin/main.rs +++ b/src/daemon/main.rs @@ -22,7 +22,6 @@ fn main() -> anyhow::Result<()> { .context("Failed at accepting a connection on the unix listener")?; handle_stream(unix_stream)?; } - Ok(()) } fn handle_stream(mut unix_stream: UnixStream) -> anyhow::Result<()> { @@ -31,6 +30,10 @@ fn handle_stream(mut unix_stream: UnixStream) -> anyhow::Result<()> { .read_to_string(&mut message) .context("Failed at reading the unix stream")?; - println!("{}", message); + println!("We received this message: {}\nReplying...", message); + + unix_stream + .write(b"I hear you!") + .context("Failed at writing onto the unix stream")?; Ok(()) } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..69bd6be --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub(self) mod common; \ No newline at end of file