Complete moving from dbus-crossroads
to dbus::tree
This commit is contained in:
16
sfsmcd/src/ifaces/errors.rs
Normal file
16
sfsmcd/src/ifaces/errors.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
#[derive(Debug)]
|
||||
pub enum ServiceError {
|
||||
NoPermission,
|
||||
AlreadyStarted,
|
||||
AlreadyStopped
|
||||
}
|
||||
|
||||
impl core::fmt::Display for ServiceError {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
Self::NoPermission => write!(f, "no permission"),
|
||||
Self::AlreadyStarted => write!(f, "already started"),
|
||||
Self::AlreadyStopped => write!(f, "already stopped")
|
||||
}
|
||||
}
|
||||
}
|
116
sfsmcd/src/ifaces/mod.rs
Normal file
116
sfsmcd/src/ifaces/mod.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
use dbus::tree::DataType;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
#[allow(non_camel_case_types)]
|
||||
pub mod traits;
|
||||
mod structs;
|
||||
mod errors;
|
||||
|
||||
pub trait Interface {
|
||||
fn path() -> dbus::Path<'static>;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Data {
|
||||
daemon: Arc<RwLock<structs::DaemonService>>
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ObjectPathData {
|
||||
daemon: Option<Arc<RwLock<structs::DaemonService>>>
|
||||
}
|
||||
|
||||
impl ObjectPathData {
|
||||
pub fn daemon(&self) -> Option<Arc<RwLock<structs::DaemonService>>> {
|
||||
self.daemon.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<structs::DaemonService> for ObjectPathData {
|
||||
fn from(d: structs::DaemonService) -> Self {
|
||||
Self {
|
||||
daemon: Some(Arc::new(RwLock::new(d)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DataType for Data {
|
||||
type Tree = ();
|
||||
type ObjectPath = ObjectPathData;
|
||||
type Property = ();
|
||||
type Interface = ();
|
||||
type Method = ();
|
||||
type Signal = ();
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! handle_iface_generic {
|
||||
($m:ident, $no_path:ident, $path:ident, |$data:ident: $data_ty:ty| {$($d:tt)+} $($f:tt)+) => {move |$m| tokio::task::block_in_place(move || {
|
||||
// Prepare no-path error
|
||||
let $no_path = dbus::tree::MethodErr::no_path($m.path.get_name());
|
||||
let $path = $m.path.get_name();
|
||||
let $data = $m.path.get_data();
|
||||
// Get data depending on the object path
|
||||
$($d)+
|
||||
// If the object path couldn't be found, the method will return the no-path error
|
||||
|
||||
let f = |$data: $data_ty| $($f)+;
|
||||
|
||||
// Run a Future on the dedicated tokio runtime
|
||||
iface_rt_handle().block_on(f($data))
|
||||
})}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[inline]
|
||||
macro_rules! handle_org_ddnss_sfs_mc_Service {
|
||||
($m:ident, |$data:ident| $($f:tt)+) => {
|
||||
$crate::handle_iface_generic!($m, no_path, path, |$data: std::sync::Arc<tokio::sync::RwLock<_>>| {
|
||||
let $data = if path == &DaemonService::path() {
|
||||
$data.daemon().ok_or(no_path)
|
||||
} else {
|
||||
Err(no_path)
|
||||
}?;
|
||||
} $($f)+)
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub mod Service {
|
||||
pub use super::{
|
||||
traits::org_ddnss_sfs_mc_Service,
|
||||
structs::DaemonService,
|
||||
errors::ServiceError
|
||||
};
|
||||
pub use crate::handle_org_ddnss_sfs_mc_Service as handle;
|
||||
|
||||
impl super::Interface for DaemonService {
|
||||
fn path() -> dbus::Path<'static> {
|
||||
dbus::Path::new("/").unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DaemonService {
|
||||
fn default() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl org_ddnss_sfs_mc_Service for DaemonService {
|
||||
fn start(&mut self) -> Result<tokio::task::JoinHandle<()>, ServiceError> {
|
||||
Err(ServiceError::AlreadyStarted)
|
||||
}
|
||||
|
||||
fn is_running(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn stop(&mut self) -> Result<(), ServiceError> {
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
fn restart(&mut self) -> Result<tokio::task::JoinHandle<()>, ServiceError> {
|
||||
Err(ServiceError::NoPermission)
|
||||
}
|
||||
}
|
||||
}
|
2
sfsmcd/src/ifaces/structs.rs
Normal file
2
sfsmcd/src/ifaces/structs.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
#[derive(Debug)]
|
||||
pub struct DaemonService;
|
19
sfsmcd/src/ifaces/traits.rs
Normal file
19
sfsmcd/src/ifaces/traits.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use super::errors::ServiceError;
|
||||
pub trait org_ddnss_sfs_mc_Service {
|
||||
|
||||
fn start(&mut self) -> Result<tokio::task::JoinHandle<()>, ServiceError>;
|
||||
fn stop(&mut self) -> Result<(), ServiceError>;
|
||||
fn is_running(&self) -> bool;
|
||||
fn restart(&mut self) -> Result<tokio::task::JoinHandle<()>, ServiceError> {
|
||||
if self.is_running() {
|
||||
self.stop()?;
|
||||
}
|
||||
self.start()
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Debug for (dyn org_ddnss_sfs_mc_Service + 'static) {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
write!(f, "org.ddnss.sfs.mc.Service")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user