daemon-engine 0.1.0

A helper package for writing unix rust daemons with IPC based clients
Documentation

Rust Daemon

A library to simplify communication with daemons in rust, this uses tokio and serde 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

GitHub tag Build Status Crates.io Docs.rs

Usage

See src/examples/server.rs for an example server, and src/examples/client.rs for an example client.

Request and Response objects must implement serde Serialize and Deserialize traits, these may be implemented using serde_derive.

Client

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)
rx.map(|resp| -> Result<(), DaemonError> {
    println!("Response: {:?}", resp);
    Ok(())
}).wait().next();

Server

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.