helles is a library providing a Unix-socket based client and server for sending JSON-based messages to a daemon application.
helles is a library providing a Unix-socket based client and server for sending JSON-based messages to a daemon application.
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::TryRecvError;
use std::sync::Arc;
use std::time::Duration;
use helles::Server;
fn main() -> std::io::Result<()> {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst);
})
.unwrap();
let (server, rx) = Server::new("/tmp/test.sock")?;
let server_context = Server::start(server, running.clone());
while running.load(Ordering::SeqCst) {
std::thread::sleep(Duration::from_secs(1));
match rx.try_recv() {
Ok(cmd) => println!("Server got command: {}", cmd),
Err(err) => match err {
TryRecvError::Disconnected => {
running.store(false, Ordering::SeqCst);
}
_ => {}
},
}
}
server_context.join().expect("Failed to join server thread");
Ok(())
}
Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you shall be dual licensed as above, without any
additional terms or conditions.