From a58d54012626dd3fd409e5c171869f984b4ad910 Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Sun, 23 Aug 2020 01:05:22 +0200 Subject: [PATCH] Add basic logging and dbus functionality to sfsmcd --- sfsmcd/src/main.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 sfsmcd/src/main.rs diff --git a/sfsmcd/src/main.rs b/sfsmcd/src/main.rs new file mode 100644 index 0000000..697ad28 --- /dev/null +++ b/sfsmcd/src/main.rs @@ -0,0 +1,58 @@ +use log::{ debug, info, warn, error }; +use dbus_crossroads::Crossroads; +use dbus::{ + message::MatchRule, + channel::MatchingReceiver +}; + +const DBUS_NAME: &'static str = "org.ddnss.sfs.mc"; + +#[tokio::main] +async fn main() -> Result<(), Box> { + // env_logger initialization + // If running with debug profile and `RUST_LOG` environment variable isn't set, set it to "debug" + if cfg!(debug_assertions) && std::env::var_os(env_logger::DEFAULT_FILTER_ENV).is_none() { + std::env::set_var(env_logger::DEFAULT_FILTER_ENV, "debug"); + } + env_logger::init(); + + // New Crossroads instance + let mut cr = Crossroads::new(); + + // Insert object paths + cr.insert("/", &[], ()); + + // Connect with D-Bus + let (resource, c) = dbus_tokio::connection::new_session_sync()?; + debug!("D-Bus unique name: {}", c.unique_name()); + + // Spawn a new task for the D-Bus resource + tokio::spawn(async move { + let err = resource.await; + panic!("Lost connection to D-Bus: {}", err); + }); + + // Allow methods to be async + cr.set_async_support(Some((c.clone(), Box::new(|x| { tokio::spawn(x); })))); + + // Aquire name on D-Bus + match c.request_name(DBUS_NAME, false, true, false).await { + Ok(_) => debug!("D-Bus name: {}", DBUS_NAME), + Err(err) => { + error!("Couldn't request name on D-Bus: {}", err); + return Err(err.into()); + } + }; + + // Receive method calls + c.start_receive(MatchRule::new_method_call(), Box::new(move |msg, conn| { + cr.handle_message(msg, conn).unwrap_or_else(|_| warn!("Incoming message wasn't a method call")); + // Return bool whether message should be handled only once (false) or another time (true) + true + })); + + // Will (afaik) be stabilized in Rust 1.46 + // core::future::pending::<()>().await; + loop {}; + unreachable!(); +} \ No newline at end of file