Private
Public Access
1
0

daemon: scaffolding for settings / sync

This commit is contained in:
2025-04-25 18:02:54 -07:00
parent 0c6b55fa38
commit fe32efef2c
10 changed files with 204 additions and 45 deletions

View File

@@ -1,6 +1,5 @@
use log::info;
use std::sync::{Arc, Mutex};
use crate::{daemon::Daemon, dbus::interface};
use dbus_crossroads::Crossroads;
use dbus_tokio::connection;
@@ -11,13 +10,13 @@ use dbus::{
Path,
};
pub struct Endpoint {
pub struct Endpoint<T: Send + Clone + 'static> {
connection: Arc<SyncConnection>,
daemon: Arc<Mutex<Daemon>>,
implementation: T,
}
impl Endpoint {
pub fn new(daemon: Daemon) -> Self {
impl<T: Send + Clone + 'static> Endpoint<T> {
pub fn new(implementation: T) -> Self {
let (resource, connection) = connection::new_session_sync().unwrap();
// The resource is a task that should be spawned onto a tokio compatible
@@ -31,15 +30,24 @@ impl Endpoint {
Self {
connection,
daemon: Arc::new(Mutex::new(daemon))
implementation
}
}
pub async fn start(&self) {
use crate::dbus::interface;
pub async fn register<F, R>(
&self,
name: &str,
path: &str,
register_fn: F
)
where
F: Fn(&mut Crossroads) -> R,
R: IntoIterator<Item = dbus_crossroads::IfaceToken<T>>,
{
let dbus_path = String::from(path);
self.connection
.request_name(interface::NAME, false, true, false)
.request_name(name, false, true, false)
.await
.expect("Unable to acquire dbus name");
@@ -54,9 +62,9 @@ impl Endpoint {
}),
)));
// Register the daemon as a D-Bus object.
let token = interface::register_net_buzzert_kordophone_server(&mut cr);
cr.insert(interface::OBJECT_PATH, &[token], self.daemon.clone());
// Register the daemon as a D-Bus object with multiple interfaces
let tokens: Vec<_> = register_fn(&mut cr).into_iter().collect();
cr.insert(dbus_path, &tokens, self.implementation.clone());
// Start receiving messages.
self.connection.start_receive(
@@ -66,14 +74,14 @@ impl Endpoint {
),
);
info!(target: "dbus", "DBus server started");
info!(target: "dbus", "Registered endpoint at {} with {} interfaces", path, tokens.len());
}
pub fn send_signal<S>(&self, signal: S) -> Result<u32, ()>
pub fn send_signal<S>(&self, path: &str, signal: S) -> Result<u32, ()>
where
S: dbus::message::SignalArgs + dbus::arg::AppendAll,
{
let message = signal.to_emit_message(&Path::new(interface::OBJECT_PATH).unwrap());
let message = signal.to_emit_message(&Path::new(path).unwrap());
self.connection.send(message)
}
}