From b94c621ab110c52056cca1e56048cadfcbb597dd Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Tue, 24 Jan 2023 08:58:52 +0100 Subject: [PATCH] Add unix example --- .gitignore | 1 + .idea/.gitignore | 8 ++++++++ .idea/command_gateway.iml | 11 +++++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 17 +++++++++++++++++ src/client/bin/main.rs | 22 ++++++++++++++++++++++ src/daemon/bin/main.rs | 36 ++++++++++++++++++++++++++++++++++++ 9 files changed, 125 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/command_gateway.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/client/bin/main.rs create mode 100644 src/daemon/bin/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6f89c2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/command_gateway.iml b/.idea/command_gateway.iml new file mode 100644 index 0000000..c254557 --- /dev/null +++ b/.idea/command_gateway.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..19847ad --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..b301a60 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" + +[[package]] +name = "command_gateway" +version = "0.1.0" +dependencies = [ + "anyhow", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f1a5ebb --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "command_gateway" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[[bin]] +name = "daemon" +path = "src/daemon/bin/main.rs" + +[[bin]] +name = "client" +path = "src/client/bin/main.rs" + +[dependencies] +anyhow = "^1.0.42" \ No newline at end of file diff --git a/src/client/bin/main.rs b/src/client/bin/main.rs new file mode 100644 index 0000000..16a3b07 --- /dev/null +++ b/src/client/bin/main.rs @@ -0,0 +1,22 @@ +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/daemon/bin/main.rs b/src/daemon/bin/main.rs new file mode 100644 index 0000000..5d2d672 --- /dev/null +++ b/src/daemon/bin/main.rs @@ -0,0 +1,36 @@ +use std::os::unix::net::{UnixListener, UnixStream}; +use anyhow::Context; +use std::io::{Read, Write}; + +fn main() -> anyhow::Result<()> { + let socket_path = "mysocket"; + + if std::fs::metadata(socket_path).is_ok() { + println!("A socket is already present. Deleting..."); + std::fs::remove_file(socket_path).with_context(|| { + format!("could not delete previous socket at {:?}", socket_path) + })?; + } + + let unix_listener = + UnixListener::bind(socket_path).context("Could not create the unix socket")?; + + // put the daemon logic in a loop to accept several connections + loop { + let (mut unix_stream, socket_address) = unix_listener + .accept() + .context("Failed at accepting a connection on the unix listener")?; + handle_stream(unix_stream)?; + } + Ok(()) +} + +fn handle_stream(mut unix_stream: UnixStream) -> anyhow::Result<()> { + let mut message = String::new(); + unix_stream + .read_to_string(&mut message) + .context("Failed at reading the unix stream")?; + + println!("{}", message); + Ok(()) +} \ No newline at end of file