From 5601c78a9ff23392cfbcc62f231fcfa4e0eeb309 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 27 Jun 2025 17:01:59 +0100 Subject: Minimal changes to convert to async --- src/main.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 14ccb7a..07288e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,19 +64,21 @@ fn topic_match(filter: &str, topic: &str) -> bool { } } -fn main() -> anyhow::Result<()> { +#[tokio::main(flavor = "current_thread")] +async fn main() -> anyhow::Result<()> { let mut conf_path: PathBuf = option_env!("SYSCONFDIR").unwrap_or("/usr/local/etc").into(); conf_path.push(format!("{PROGRAM}.toml")); let conf = config::load(&conf_path) .with_context(|| format!("Failed to load config: {:?}", &conf_path))?; - let (client, mut connection) = conf.mqtt_client(); + let (client, mut event_loop) = conf.mqtt_client(); for topic in conf.routes.keys() { // TODO: Configurable subscription QoS - if let Err(e) = client.subscribe(topic, QoS::AtMostOnce) { + if let Err(e) = client.subscribe(topic, QoS::AtMostOnce).await { eprintln!("warning: Failed to subscribe to '{topic}': {e:?}"); } } - for notification in connection.iter() { + loop { + let notification = event_loop.poll().await; match notification? { Incoming(Packet::Publish(p)) => { for (topic, programs) in conf.routes.iter() { @@ -92,7 +94,6 @@ fn main() -> anyhow::Result<()> { _ => (), } } - Ok(()) } #[cfg(test)] -- cgit v1.2.3-70-g09d2