Crate netidx

source ·
Expand description

Netidx is middleware that enables publishing a value, like 42, in one program and consuming it in another program, either on the same machine or across the network.

For more details see the netidx book

Here is an example service that publishes a cpu temperature, along with the corresponding subscriber that consumes the data.

§Publisher

use netidx::{
    publisher::{PublisherBuilder, Value, BindCfg, DesiredAuth},
    config::Config,
    path::Path,
};
use tokio::time;
use std::time::Duration;

// load the site cluster config. You can also just use a file.
let cfg = Config::load_default()?;

// listen on any unique address matching 192.168.0.0/16
let publisher = PublisherBuilder::new(cfg)
    .desired_auth(DesiredAuth::Anonymous)
    .bind_cfg(Some("192.168.0.0/16".parse()?))
    .build()
    .await?;

let temp = publisher.publish(
    Path::from("/hw/washu-chan/cpu-temp"),
    Value::F32(get_cpu_temp())
)?;

loop {
    time::sleep(Duration::from_millis(500)).await;
    let mut batch = publisher.start_batch();
    temp.update(&mut batch, Value::F32(get_cpu_temp()));
    batch.commit(None).await;
}

§Subscriber

use netidx::{
    subscriber::{Subscriber, UpdatesFlags, DesiredAuth},
    config::Config,
    path::Path,
};
use futures::{prelude::*, channel::mpsc};

let cfg = Config::load_default()?;
let subscriber = Subscriber::new(cfg, DesiredAuth::Anonymous)?;
let path = Path::from("/hw/washu-chan/cpu-temp");
let temp = subscriber.subscribe(path);
temp.wait_subscribed().await;
println!("washu-chan cpu temp is: {:?}", temp.last());

let (tx, mut rx) = mpsc::channel(10);
temp.updates(UpdatesFlags::empty(), tx);
while let Some(mut batch) = rx.next().await {
    for (_, v) in batch.drain(..) {
        println!("washu-chan cpu temp is: {:?}", v);
    }
}

Published things always have a value, which new subscribers receive initially. Thereafter a subscription is a lossless ordered stream, just like a tcp connection, except that instead of bytes publisher::Value is the unit of transmission. Since the subscriber can write values back to the publisher, the connection is bidirectional, also like a Tcp stream.

Values include many useful primitives, including zero copy bytes buffers (using the awesome bytes crate), so you can easily use netidx to efficiently send any kind of message you like. However it’s advised to stick to primitives and express structure with multiple published values in a hierarchy, since this makes your system more discoverable, and is also quite efficient.

netidx includes optional support for kerberos v5 (including Active Directory). If enabled, all components will do mutual authentication between the resolver, subscriber, and publisher as well as encryption of all data on the wire.

In krb5 mode the resolver server maintains and enforces a set of authorization permissions for the entire namespace. The system administrator can centrally enforce who can publish where, and who can subscribe to what.

Re-exports§

Modules§