Add basic logging and dbus functionality to sfsmcd
This commit is contained in:
parent
5852f80b1f
commit
a58d540126
58
sfsmcd/src/main.rs
Normal file
58
sfsmcd/src/main.rs
Normal file
@ -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<dyn std::error::Error>> {
|
||||||
|
// 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!();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user