Skip to content

Commit 2b97af3

Browse files
committed
feat(orb-connd): event based dbus reporting
1 parent 3cfa086 commit 2b97af3

2 files changed

Lines changed: 55 additions & 29 deletions

File tree

‎orb-connd/src/network_manager/mod.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,20 @@ impl NetworkManager {
474474
let state = NMState::try_from(nm.state().await?)?;
475475
Ok(state)
476476
}
477+
478+
pub async fn state_stream(&self) -> Result<impl futures::Stream> {
479+
let nm = NetworkManagerProxy::new(&self.conn).await?;
480+
let stream = nm.receive_state_changed().await?;
481+
482+
Ok(stream)
483+
}
484+
485+
pub async fn primary_connection_stream(&self) -> Result<impl futures::Stream> {
486+
let nm = NetworkManagerProxy::new(&self.conn).await?;
487+
let stream = nm.receive_primary_connection_changed().await;
488+
489+
Ok(stream)
490+
}
477491
}
478492

479493
#[derive(Debug, Clone, PartialEq, Eq)]

‎orb-connd/src/telemetry/backend_status_wifi_reporter.rs‎

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ use orb_backend_status_dbus::{
55
types::{ConndReport, WifiNetwork, WifiProfile},
66
BackendStatusProxy,
77
};
8-
use rusty_network_manager::NetworkManagerProxy;
9-
use std::time::Duration;
8+
use std::time::{Duration, Instant};
109
use tokio::{
1110
task::{self, JoinHandle},
1211
time,
1312
};
14-
use tracing::{debug, error, info, warn};
13+
use tracing::{error, info, warn};
1514

1615
pub fn spawn(
1716
nm: NetworkManager,
@@ -33,45 +32,58 @@ async fn run_reporter(
3332
session_bus: zbus::Connection,
3433
report_interval: Duration,
3534
) -> Result<()> {
36-
let system_bus = zbus::Connection::system()
37-
.await
38-
.wrap_err("Failed to connect to system bus")?;
39-
40-
let nm_proxy = NetworkManagerProxy::new(&system_bus)
41-
.await
42-
.wrap_err("Failed to create NetworkManager proxy")?;
43-
44-
let mut state_stream = nm_proxy
45-
.receive_state_changed()
46-
.await
47-
.wrap_err("Failed to subscribe to StateChanged signal")?;
48-
49-
let mut primary_conn_stream = nm_proxy.receive_primary_connection_changed().await;
35+
let throttle = Duration::from_secs(2);
36+
let stream_backoff = Duration::from_secs(3);
37+
38+
let (mut state_stream, mut primary_conn_stream) = loop {
39+
match nm.state_stream().await {
40+
Ok(stream) => match nm.primary_connection_stream().await {
41+
Ok(pcs) => {
42+
info!("Successfully subscribed to NetworkManager streams");
43+
break (stream, pcs);
44+
}
45+
Err(e) => {
46+
error!("Failed to get primary connection stream: {e}");
47+
time::sleep(stream_backoff).await;
48+
continue;
49+
}
50+
},
51+
Err(e) => {
52+
error!("Failed to get state stream: {e}");
53+
time::sleep(stream_backoff).await;
54+
continue;
55+
}
56+
}
57+
};
5058

5159
let mut interval = time::interval(report_interval);
5260
interval.set_missed_tick_behavior(time::MissedTickBehavior::Skip);
5361

62+
let mut last_report = Instant::now() - throttle;
63+
5464
loop {
65+
let elapsed = last_report.elapsed();
66+
67+
if elapsed < throttle {
68+
time::sleep(throttle - elapsed).await;
69+
}
70+
5571
tokio::select! {
5672
_ = state_stream.next() => {
5773
info!("NetworkManager state changed - sending immediate WiFi status");
58-
if let Err(e) = report(&nm, &session_bus).await {
59-
error!("failed to report to backend status on state change: {e}");
60-
}
6174
}
75+
6276
_ = primary_conn_stream.next() => {
6377
info!("Primary connection changed - sending immediate WiFi status");
64-
if let Err(e) = report(&nm, &session_bus).await {
65-
error!("failed to report to backend status on connection change: {e}");
66-
}
67-
}
68-
_ = interval.tick() => {
69-
debug!("Periodic WiFi status report ({}s interval)", report_interval.as_secs());
70-
if let Err(e) = report(&nm, &session_bus).await {
71-
error!("failed to report to backend status on periodic check: {e}");
72-
}
7378
}
79+
80+
_ = interval.tick() => {}
81+
};
82+
83+
if let Err(e) = report(&nm, &session_bus).await {
84+
error!("failed to report to backend status: {e}");
7485
}
86+
last_report = Instant::now();
7587
}
7688
}
7789

0 commit comments

Comments
 (0)