use std::thread;
use std::time::Duration;
use buswatch_tui::{ChannelSource, DataSource, Snapshot};
fn main() {
println!("Channel source example");
println!("Generating synthetic monitor data...\n");
let (tx, mut source) = ChannelSource::create("synthetic-data");
thread::spawn(move || {
let mut counter = 0u64;
loop {
counter += 1;
let snapshot = Snapshot::builder()
.module("Producer", |m| {
m.write("events", |w| {
let mut builder = w.count(counter * 10);
if counter % 5 == 0 {
builder = builder.pending(Duration::from_millis(100));
}
builder
})
})
.module("Consumer", |m| {
m.read("events", |r| r.count(counter * 10 - 2).backlog(2))
})
.build();
if tx.send(snapshot).is_err() {
break; }
thread::sleep(Duration::from_secs(1));
}
});
println!("Receiving snapshots (press Ctrl+C to stop):\n");
loop {
if let Some(snapshot) = source.poll() {
println!("Received snapshot:");
for (name, state) in snapshot.iter() {
println!(" Module: {}", name);
for (topic, read) in &state.reads {
println!(
" Read from '{}': {} messages, {} unread",
topic,
read.count,
read.backlog.unwrap_or(0)
);
}
for (topic, write) in &state.writes {
println!(
" Write to '{}': {} messages{}",
topic,
write.count,
write
.pending
.map(|d| format!(" (pending: {}us)", d.as_micros()))
.unwrap_or_default()
);
}
}
println!();
}
thread::sleep(Duration::from_millis(100));
}
}