# Rust Daemon
A library to simplify communication with daemons in rust, this uses [tokio](https://github.com/tokio-rs/tokio) and [serde](https://serde.rs/) to establish a type-safe json over unix socket interface for client-daemon communication, and is intended to be extended with other useful daemon-writing features as they are discovered.
This consists of a Server that handles Requests from and issues Responses to Clients, and a Client that issues Requests to and receives Responses from a Server.
## Status
[](https://github.com/ryankurte/daemon-engine)
[](https://travis-ci.com/ryankurte/rust-daemon)
[](https://crates.io/crates/daemon-engine)
[](https://docs.rs/daemon-engine)
## Usage
See [src/examples/server.rs](src/examples/server.rs) for an example server, and [src/examples/client.rs](src/examples/client.rs) for an example client.
`Request` and `Response` objects must implement [serde](https://serde.rs/) `Serialize` and `Deserialize` traits, these may be implemented using [serde_derive](https://serde.rs/derive.html).
### Client
```rust
extern crate daemon_engine;
use daemon_engine::Client;
...
// Create client instance
let client = Client::<_, Request, Response>::new(addr).unwrap();
// Split RX and TX
let (tx, rx) = client.split();
// Send something (remember to .wait())
tx.send(Request::Something).wait().unwrap();
// Receive something (also remember to wait)
Ok(())
}).wait().next();
```
### Server
```rust
extern crate daemon_engine;
use daemon_engine::Server;
...
let server_handle = future::lazy(move || {
// Create server instance, this must be executed from within a tokio context
let s = Server::<Request, Response>::new(&addr).unwrap();
// Handle requests from clients
s.incoming().unwrap().for_each(move |r| {
println!("Request: {:?}", r.data());
let data = r.data();
match data {
...
_ => {
r.send(Response::Something(v.to_string()))
}
// Remember you have to .wait or otherwise prompt for send to occur
}.wait().unwrap();
Ok(())
}).map_err(|_e| ());
// do more stuff
...
// Close the server when you're done
s.close();
Ok(())
});
// Create server task
tokio::run(server_handle);
``
------
If you have any questions, comments, or suggestions, feel free to open an issue or a pull request.