Private
Public Access
1
0

Started to factor out DbusRegistry from Endpoint

This commit is contained in:
2025-05-26 15:49:29 -07:00
parent c02d4ecdf3
commit 831e490eb4
3 changed files with 92 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
use log::info;
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use dbus::{
channel::{MatchingReceiver, Sender},
@@ -9,6 +9,72 @@ use dbus::{
};
use dbus_crossroads::Crossroads;
#[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);
let mut cr = self.crossroads.lock().unwrap();
let tokens: Vec<_> = register_fn(&mut cr).into_iter().collect();
cr.insert(dbus_path, &tokens, implementation);
// 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> {
connection: Arc<SyncConnection>,

View File

@@ -1,8 +1,6 @@
use dbus::arg;
use dbus::nonblock::SyncConnection;
use dbus_tree::MethodErr;
use std::future::Future;
use std::sync::Arc;
use std::thread;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
@@ -13,24 +11,22 @@ use crate::daemon::{
Attachment, DaemonResult,
};
use crate::dbus::endpoint::Endpoint;
use crate::dbus::endpoint::DbusRegistry;
use crate::dbus::interface::NetBuzzertKordophoneAttachment as DbusAttachment;
use crate::dbus::interface::NetBuzzertKordophoneRepository as DbusRepository;
use crate::dbus::interface::NetBuzzertKordophoneSettings as DbusSettings;
#[derive(Clone)]
pub struct ServerImpl {
connection: Arc<SyncConnection>,
event_sink: mpsc::Sender<Event>,
attachment_objects: Vec<Endpoint<Attachment>>,
dbus_registry: DbusRegistry,
}
impl ServerImpl {
pub fn new(connection: Arc<SyncConnection>, event_sink: mpsc::Sender<Event>) -> Self {
pub fn new(event_sink: mpsc::Sender<Event>, dbus_registry: DbusRegistry) -> Self {
Self {
connection: connection,
event_sink: event_sink,
attachment_objects: vec![],
dbus_registry: dbus_registry,
}
}
@@ -175,13 +171,11 @@ impl DbusRepository for ServerImpl {
let obj_path = format!("/net/buzzert/kordophonecd/attachments/{}", &id);
log::trace!("Registering attachment at path: {}", &obj_path);
let endpoint = Endpoint::new(self.connection.clone(), attachment);
run_sync_future(endpoint.register_object(obj_path.as_str(), |cr| {
vec![interface::register_net_buzzert_kordophone_attachment(cr)]
}))?;
self.attachment_objects.push(endpoint);
log::trace!("Attachment objects: {:?}", self.attachment_objects.len());
self.dbus_registry.register_object(
&obj_path,
attachment,
|cr| vec![interface::register_net_buzzert_kordophone_attachment(cr)]
);
Ok(obj_path.into())
})