Crates.io | suss |
lib.rs | suss |
version | 0.0.5 |
source | src |
created_at | 2022-08-05 21:43:51.266255 |
updated_at | 2022-08-09 13:11:51.480932 |
description | Create collections of single-instance unix socket services, started on-demand by any clients using them. |
homepage | https://github.com/infomorphic-matti/suss |
repository | https://github.com/infomorphic-matti/suss |
max_upload_size | |
id | 639574 |
size | 103,858 |
Suss is a library to create collections of unix services that can interact with each other and (optionally) start each other on-demand.
Furthermore, it is pluggable into different async frameworks as long as you provide an implementation of [socket_shims::UnixSocketInterface
] . If you don't care about async at all, either use the standard threadpool implementation (based on [blocking
] ), or write a synchronous API implementation and use a crate like pollster
to un-async things.
It is hosted at:
Provide a single interface for a simple echo service, over any compatible unix socket interface - with the ability to start on demand by running a command.
use suss::prelude::*;
use suss::blocking;
use std::io::{Result as IoResult, BufRead};
struct EchoClient<U: UnixSocketInterface>(U::UnixStream);
impl <U: UnixSocketInterface> EchoClient<U> {
pub fn new(u: U::UnixStream) -> Self {
Self(u)
}
pub async fn write_and_receive<'b>(&mut self, in_value: &'b [u8]) -> IoResult<Vec<u8>> {
U::unix_stream_write_all(&mut self.0, &(in_value.len() as u64).to_be_bytes()).await?;
U::unix_stream_write_all(&mut self.0, in_value).await?;
let mut out_length_bytes = [0u8;8];
U::unix_stream_read_exact(&mut self.0, &mut out_length_bytes).await?;
let mut out_length = u64::from_be_bytes(out_length_bytes);
let mut out = vec![0u8;out_length.try_into().unwrap()];
U::unix_stream_read_exact(&mut self.0, &mut out).await?;
Ok(out)
}
}
declare_service!{
/// This is a very nice echo service!
EchoService <U> = {
"my-echo-service" "--server" @ "echo-service.socks" as raw |stream| -> Io<EchoClient<U>> { Ok(EchoClient(stream)) }
} impl {U: UnixSocketInterface}
}
fn main() {
let reified: ReifiedService<_, suss::socket_shims::StdThreadpoolUSocks, _> = EchoService.reify("/var/run/".as_ref());
futures_lite_block_on(async move {
let mut service_api = reified.connect_to_running().await.expect("No service running");
let stdin = std::io::stdin();
let mut stdin = stdin.lock();
let mut buf = String::new();
loop {
stdin.read_line(&mut buf).unwrap();
if buf.trim() == "die" {
break
};
let received_line = service_api.write_and_receive(buf.as_bytes()).await.unwrap();
let valid_utf8 = std::str::from_utf8(&received_line).unwrap();
println!("{valid_utf8}");
}
});
}
This library allows you to:
async-std
, tokio
, and a threadpool-based std
fallback are currently available). All of these frameworks can convert to and from standard library unix sockets, so you can use them freely in client and server implementations even as suss
remains runtime-agnostic.