From 58d096ef1372cb4668e7d4f3a4d5ad761e8ea528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 19 Jul 2022 09:15:48 +0200 Subject: [PATCH] Try running queries and monitor at the same time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- Cargo.toml | 2 ++ src/main.rs | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f831da3..89f34da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,5 @@ async-trait = "0.1.56" mmdbus = "1.18.6" dbus-tokio = "0.7.5" tokio = { version = "1.19.2", features = [ "time", "macros", "rt-multi-thread" ] } +futures = "0.3" +futures-util = "0.3" diff --git a/src/main.rs b/src/main.rs index 722e5f0..1132457 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ use mm::modem::Modem as ModemAccess; use std::fmt; use std::sync::Arc; +use futures_util::stream::StreamExt; use tokio::{ task::JoinHandle, time::Duration, @@ -26,6 +27,7 @@ use tokio::{ use mmdbus::{ dbus::{ + message::MatchRule, nonblock::{ SyncConnection, Proxy, stdintf::org_freedesktop_dbus::ObjectManager, @@ -176,16 +178,42 @@ impl From for ModemState { } } -#[tokio::main] -async fn main() -> Result<(), DBusError> { - let modems = ModemManager::connect()?.modems().await?; +async fn print_foo(mm: ModemManager) -> Result<(), DBusError> { + let modems = mm.modems().await?; println!("Modems: {:?}", modems); - for modem in modems { println!("Modem: {:?}", modem); println!("Enabled: {:?}", modem.enabled().await); println!("Model: {:?}", modem.model().await); } + Ok(()) +} + +#[tokio::main] +pub async fn main() -> Result<(), Box> { + let dbus = DBus::connect()?; + + let mut interval = tokio::time::interval(Duration::from_secs(2)); + + let calls = async { + loop { + let mm = ModemManager { dbus: dbus.clone() }; + interval.tick().await; + let _ = print_foo(mm).await; + } + }; + + let mr = MatchRule::new_signal("org.freedesktop.DBus.Properties", "PropertiesChanged"); + + let (_in_signal, stream) = dbus.conn.add_match(mr).await?.stream(); + let stream = stream.for_each(|(foo, (source,)): (_, (String,))| { + println!("Foo {}; {:?}", source, foo); + async {} + }); + + stream.await; + + futures::join!(stream, calls); Ok(()) }