use std::time::{Duration, SystemTime};
use hiroz::{Builder, Result, context::ZContextBuilder};
use hiroz_msgs::std_msgs::String as RosString;
pub async fn run(
ctx: hiroz::context::ZContext,
topic: String,
capacity: usize,
window_ms: u64,
count: usize,
) -> Result<()> {
let node = ctx.create_node("cache_consumer").build()?;
let cache = node.create_cache::<RosString>(&topic, capacity).build()?;
println!(
"[cache/zenoh] subscribed to '{}', capacity={}, window={}ms",
topic, capacity, window_ms
);
tokio::time::sleep(Duration::from_millis(300)).await;
let window = Duration::from_millis(window_ms);
let mut i = 0usize;
loop {
let now = SystemTime::now();
let msgs = cache.get_interval(now - window, now);
let newest = cache.get_before(now);
println!(
"[cache/zenoh] window=[now-{}ms, now]: {} messages | newest: {}",
window_ms,
msgs.len(),
newest.as_ref().map(|m| m.data.as_str()).unwrap_or("(none)"),
);
i += 1;
if count > 0 && i >= count {
break;
}
tokio::time::sleep(Duration::from_millis(500)).await;
}
Ok(())
}
#[cfg(not(test))]
#[tokio::main]
async fn main() -> Result<()> {
zenoh::init_log_from_env_or("error");
let ctx = ZContextBuilder::default().build()?;
run(ctx, "/cache_demo".into(), 20, 500, 0).await
}