From ef7397293a82e513633f9319ef1580a0446ca827 Mon Sep 17 00:00:00 2001 From: Reinout Meliesie Date: Wed, 5 Jun 2024 00:55:02 +0200 Subject: [PATCH] refactor: re-add proxy trait to client --- src/clients/networkmanager.rs | 94 ++++++++++++++--------------------- 1 file changed, 37 insertions(+), 57 deletions(-) diff --git a/src/clients/networkmanager.rs b/src/clients/networkmanager.rs index 32487c0..6a89987 100644 --- a/src/clients/networkmanager.rs +++ b/src/clients/networkmanager.rs @@ -1,16 +1,19 @@ use std::sync::Arc; +use std::thread; use color_eyre::Result; use futures_signals::signal::{Mutable, MutableSignalCloned}; use tracing::error; +use zbus::blocking::fdo::PropertiesProxy; +use zbus::blocking::Connection; use zbus::{ - blocking::{fdo::PropertiesProxy, Connection}, + dbus_proxy, names::InterfaceName, zvariant::{Error as ZVariantError, ObjectPath, Str}, Error as ZBusError, }; -use crate::{register_fallible_client, spawn_blocking}; +use crate::{register_fallible_client, spawn, spawn_blocking}; const DBUS_BUS: &str = "org.freedesktop.NetworkManager"; const DBUS_PATH: &str = "/org/freedesktop/NetworkManager"; @@ -20,7 +23,7 @@ const DBUS_INTERFACE: &str = "org.freedesktop.NetworkManager"; pub struct Client { client_state: Mutable, interface_name: InterfaceName<'static>, - props_proxy: PropertiesProxy<'static>, + dbus_connection: Connection, } #[derive(Clone, Debug)] @@ -34,75 +37,52 @@ pub enum ClientState { Unknown, } +#[dbus_proxy( + default_service = "org.freedesktop.NetworkManager", + interface = "org.freedesktop.NetworkManager", + default_path = "/org/freedesktop/NetworkManager" +)] +trait NetworkManagerDbus { + #[dbus_proxy(property)] + fn active_connections(&self) -> Result>; + + #[dbus_proxy(property)] + fn devices(&self) -> Result>; + + #[dbus_proxy(property)] + fn networking_enabled(&self) -> Result; + + #[dbus_proxy(property)] + fn primary_connection(&self) -> Result; + + #[dbus_proxy(property)] + fn primary_connection_type(&self) -> Result; + + #[dbus_proxy(property)] + fn wireless_enabled(&self) -> Result; +} + impl Client { fn new() -> Result { let client_state = Mutable::new(ClientState::Unknown); let dbus_connection = Connection::system()?; - let props_proxy = PropertiesProxy::builder(&dbus_connection) - .destination(DBUS_BUS)? - .path(DBUS_PATH)? - .build()?; let interface_name = InterfaceName::from_static_str(DBUS_INTERFACE)?; Ok(Self { client_state, interface_name, - props_proxy, + dbus_connection, }) } fn run(&self) -> Result<()> { - let props = self.props_proxy.get_all(self.interface_name.clone())?; - let mut primary_connection = props["PrimaryConnection"] - .downcast_ref::() - .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))? - .to_string(); - let mut primary_connection_type = props["PrimaryConnectionType"] - .downcast_ref::() - .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))? - .to_string(); - let mut wireless_enabled = *props["WirelessEnabled"] - .downcast_ref::() - .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?; - self.client_state.set(determine_state( - &primary_connection, - &primary_connection_type, - wireless_enabled, - )); + let proxy = NetworkManagerDbusProxyBlocking::new(&self.dbus_connection)?; - let changed_props_stream = self.props_proxy.receive_properties_changed()?; - for signal in changed_props_stream { - let args = signal.args()?; - if args.interface_name != self.interface_name { - continue; - } - let changed_props = args.changed_properties; - if let Some(new_primary_connection) = changed_props.get("PrimaryConnection") { - let new_primary_connection = new_primary_connection - .downcast_ref::() - .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?; - primary_connection = new_primary_connection.to_string(); - } - if let Some(new_primary_connection_type) = changed_props.get("PrimaryConnectionType") { - let new_primary_connection_type = new_primary_connection_type - .downcast_ref::() - .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?; - primary_connection_type = new_primary_connection_type.to_string(); - } - if let Some(new_wireless_enabled) = changed_props.get("WirelessEnabled") { - let new_wireless_enabled = new_wireless_enabled - .downcast_ref::() - .ok_or(ZBusError::Variant(ZVariantError::IncorrectType))?; - wireless_enabled = *new_wireless_enabled; - } - self.client_state.set(determine_state( - &primary_connection, - &primary_connection_type, - wireless_enabled, - )); - } + let mut primary_connection = proxy.primary_connection()?; + let mut primary_connection_type = proxy.primary_connection_type()?; + let mut wireless_enabled = proxy.wireless_enabled()?; - Ok(()) + todo!() } pub fn subscribe(&self) -> MutableSignalCloned {