Private
Public Access
1
0
Files
Kordophone/kordophoned/src/dbus/endpoint.rs

130 lines
4.1 KiB
Rust
Raw Normal View History

use log::info;
use std::sync::{Arc, Mutex};
2025-02-11 23:15:24 -08:00
use dbus::{
channel::{MatchingReceiver, Sender},
2025-02-11 23:15:24 -08:00
message::MatchRule,
nonblock::SyncConnection,
Path,
};
use dbus_crossroads::Crossroads;
2025-02-11 23:15:24 -08:00
#[derive(Clone)]
pub struct DbusRegistry {
connection: Arc<SyncConnection>,
crossroads: Arc<Mutex<Crossroads>>,
message_handler_started: Arc<Mutex<bool>>,
}
impl DbusRegistry {
pub fn new(connection: Arc<SyncConnection>) -> Self {
let mut cr = Crossroads::new();
// Enable async support for the crossroads instance.
// (Currently irrelevant since dbus generates sync code)
cr.set_async_support(Some((
connection.clone(),
Box::new(|x| {
tokio::spawn(x);
}),
)));
Self {
connection,
crossroads: Arc::new(Mutex::new(cr)),
message_handler_started: Arc::new(Mutex::new(false)),
}
}
pub fn register_object<T, F, R>(&self, path: &str, implementation: T, register_fn: F)
where
T: Send + Clone + 'static,
F: Fn(&mut Crossroads) -> R,
R: IntoIterator<Item = dbus_crossroads::IfaceToken<T>>,
{
let dbus_path = String::from(path);
2025-06-06 16:39:31 -07:00
let mut cr = self.crossroads.lock().unwrap();
let tokens: Vec<_> = register_fn(&mut cr).into_iter().collect();
cr.insert(dbus_path, &tokens, implementation);
2025-06-06 16:39:31 -07:00
// Start message handler if not already started
let mut handler_started = self.message_handler_started.lock().unwrap();
if !*handler_started {
let crossroads_clone = self.crossroads.clone();
self.connection.start_receive(
MatchRule::new_method_call(),
Box::new(move |msg, conn| {
let mut cr = crossroads_clone.lock().unwrap();
cr.handle_message(msg, conn).is_ok()
}),
);
*handler_started = true;
info!(target: "dbus", "Started D-Bus message handler");
}
info!(target: "dbus", "Registered object at {} with {} interfaces", path, tokens.len());
}
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(path).unwrap());
self.connection.send(message)
}
}
// Keep the old Endpoint struct for backward compatibility during transition
#[derive(Clone)]
pub struct Endpoint<T: Send + Clone + 'static> {
2025-02-11 23:15:24 -08:00
connection: Arc<SyncConnection>,
implementation: T,
2025-02-11 23:15:24 -08:00
}
impl<T: Send + Clone + 'static> Endpoint<T> {
pub fn new(connection: Arc<SyncConnection>, implementation: T) -> Self {
Self {
connection,
implementation,
}
2025-02-11 23:15:24 -08:00
}
pub async fn register_object<F, R>(&self, path: &str, register_fn: F)
where
F: Fn(&mut Crossroads) -> R,
R: IntoIterator<Item = dbus_crossroads::IfaceToken<T>>,
{
let dbus_path = String::from(path);
2025-02-11 23:15:24 -08:00
// Enable async support for the crossroads instance.
2025-02-11 23:15:24 -08:00
// (Currently irrelevant since dbus generates sync code)
let mut cr = Crossroads::new();
2025-02-11 23:15:24 -08:00
cr.set_async_support(Some((
self.connection.clone(),
Box::new(|x| {
tokio::spawn(x);
}),
)));
// 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());
2025-02-11 23:15:24 -08:00
// Start receiving messages.
self.connection.start_receive(
MatchRule::new_method_call(),
Box::new(move |msg, conn| cr.handle_message(msg, conn).is_ok()),
2025-02-11 23:15:24 -08:00
);
info!(target: "dbus", "Registered endpoint at {} with {} interfaces", path, tokens.len());
2025-02-11 23:15:24 -08:00
}
pub fn send_signal<S>(&self, path: &str, signal: S) -> Result<u32, ()>
2025-02-11 23:15:24 -08:00
where
S: dbus::message::SignalArgs + dbus::arg::AppendAll,
{
let message = signal.to_emit_message(&Path::new(path).unwrap());
2025-02-11 23:15:24 -08:00
self.connection.send(message)
}
}