Complete moving from dbus-crossroads to dbus::tree

This commit is contained in:
2020-09-13 14:49:59 +02:00
parent a4bf221c92
commit f0066c8e76
5 changed files with 242 additions and 2 deletions

View File

@@ -1,10 +1,51 @@
use log::{ debug, info, warn, error };
use dbus::{
message::MatchRule,
channel::MatchingReceiver
channel::MatchingReceiver,
tree::Factory,
tree::MethodErr
};
use ifaces::Interface;
use std::sync::Arc;
mod ifaces;
const DBUS_NAME: &'static str = "org.ddnss.sfs.mc";
// Interface tokio runtime
static mut IFACE_RT: Option<tokio::runtime::Runtime> = None;
static mut IFACE_RT_HANDLE: Option<Arc<tokio::runtime::Handle>> = None;
static IFACE_RT_INIT: std::sync::Once = std::sync::Once::new();
fn iface_name(iface: &str) -> String {
DBUS_NAME.to_owned() + "." + iface
}
fn iface_rt_handle() -> Arc<tokio::runtime::Handle> {
IFACE_RT_INIT.call_once(|| {
unsafe {
IFACE_RT = Some(tokio::runtime::Builder::new()
.thread_name("iface-runtime-worker")
.build()
.unwrap()
);
IFACE_RT_HANDLE = match &IFACE_RT {
Some(iface_rt) => Some(Arc::new(iface_rt
.handle()
.clone()
)),
_ => unreachable!()
};
}
});
unsafe {
match &IFACE_RT_HANDLE {
Some(iface_rt_handle) => iface_rt_handle.clone(),
_ => unreachable!()
}
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -17,6 +58,46 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
debug!("PID: {}, User: {}", std::process::id(), std::env::var("USER").unwrap_or("N/A".into()));
// Build interfaces
let f = Factory::new_sync::<ifaces::Data>();
let mut iface_map = std::collections::HashMap::new();
iface_map.insert(iface_name("Service"), Arc::new({
use ifaces::Service::*;
f.interface(iface_name("Service"), ())
// org_ddnss_sfs_mc_Service::start()
.add_m(f.method_sync("Start", (), handle!(m, |data| async move {
org_ddnss_sfs_mc_Service::start(&mut *data.write().await)
.map(|_| vec![m.msg.method_return()])
.map_err(|err| MethodErr::failed(&err))
})))
// org_ddnss_sfs_mc_Service::stop()
.add_m(f.method_sync("Stop", (), handle!(m, |data| async move {
org_ddnss_sfs_mc_Service::stop(&mut *data.write().await)
.map(|_| vec![m.msg.method_return()])
.map_err(|err| MethodErr::failed(&err))
})))
// org_ddnss_sfs_mc_Service::restart()
.add_m(f.method_sync("Restart", (), handle!(m, |data| async move {
org_ddnss_sfs_mc_Service::restart(&mut *data.write().await)
.map(|_| vec![m.msg.method_return()])
.map_err(|err| MethodErr::failed(&err))
})))
// org_ddnss_sfs_mc_Service::is_running()
.add_m(f.method_sync("IsRunning", (), handle!(m, |data| async move {
Ok(vec![m.msg.method_return().append1(
org_ddnss_sfs_mc_Service::is_running(&*data.read().await)
)])
})).out_arg("b"))
}));
// Add interfaces to object paths in a new tree
let tree = f.tree(())
.add(f.object_path(ifaces::Service::DaemonService::path(), ifaces::Service::DaemonService.into())
.introspectable()
.add(iface_map[&iface_name("Service")].clone())
);
// Connect with D-Bus
let (resource, c) = dbus_tokio::connection::new_system_sync()?;
debug!("D-Bus unique name: {}", c.unique_name());
@@ -37,7 +118,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
};
// Receive method calls
c.start_receive(MatchRule::new_method_call(), Box::new(move |msg, conn| {
c.start_receive(MatchRule::new_method_call(), Box::new(move |msg, c| {
use dbus::channel::Sender;
if let Some(replies) = tree.handle(&msg) {
for r in replies {
let _ = c.send(r);
}
}
true
}));