Add unix example
This commit is contained in:
parent
49bab13f6e
commit
b94c621ab1
9 changed files with 125 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target/
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
|
@ -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
|
11
.idea/command_gateway.iml
Normal file
11
.idea/command_gateway.iml
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="CPP_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/command_gateway.iml" filepath="$PROJECT_DIR$/.idea/command_gateway.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
16
Cargo.lock
generated
Normal file
16
Cargo.lock
generated
Normal file
|
@ -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",
|
||||
]
|
17
Cargo.toml
Normal file
17
Cargo.toml
Normal file
|
@ -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"
|
22
src/client/bin/main.rs
Normal file
22
src/client/bin/main.rs
Normal file
|
@ -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(())
|
||||
}
|
36
src/daemon/bin/main.rs
Normal file
36
src/daemon/bin/main.rs
Normal file
|
@ -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(())
|
||||
}
|
Reference in a new issue