Private
Public Access
1
0

broken: started working on attachment dbus object, but order of endpoint creation seems to matter, need to reuse more parts

This commit is contained in:
2025-05-25 18:52:18 -07:00
parent 0d4c2e5104
commit c02d4ecdf3
5 changed files with 112 additions and 62 deletions

View File

@@ -1,60 +1,38 @@
use log::info;
use std::sync::Arc;
use dbus_crossroads::Crossroads;
use dbus_tokio::connection;
use dbus::{
channel::{MatchingReceiver, Sender},
message::MatchRule,
nonblock::SyncConnection,
channel::{Sender, MatchingReceiver},
Path,
};
use dbus_crossroads::Crossroads;
#[derive(Clone)]
pub struct Endpoint<T: Send + Clone + 'static> {
connection: Arc<SyncConnection>,
implementation: T,
}
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
// reactor ASAP. If the resource ever finishes, you lost connection to D-Bus.
//
// To shut down the connection, both call _handle.abort() and drop the connection.
let _handle = tokio::spawn(async {
let err = resource.await;
panic!("Lost connection to D-Bus: {}", err);
});
Self {
connection,
implementation
pub fn new(connection: Arc<SyncConnection>, implementation: T) -> Self {
Self {
connection,
implementation,
}
}
pub async fn register<F, R>(
&self,
name: &str,
path: &str,
register_fn: F
)
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);
self.connection
.request_name(name, false, true, false)
.await
.expect("Unable to acquire dbus name");
let mut cr = Crossroads::new();
// Enable async support for the crossroads instance.
// Enable async support for the crossroads instance.
// (Currently irrelevant since dbus generates sync code)
let mut cr = Crossroads::new();
cr.set_async_support(Some((
self.connection.clone(),
Box::new(|x| {
@@ -69,9 +47,7 @@ impl<T: Send + Clone + 'static> Endpoint<T> {
// Start receiving messages.
self.connection.start_receive(
MatchRule::new_method_call(),
Box::new(move |msg, conn|
cr.handle_message(msg, conn).is_ok()
),
Box::new(move |msg, conn| cr.handle_message(msg, conn).is_ok()),
);
info!(target: "dbus", "Registered endpoint at {} with {} interfaces", path, tokens.len());