Private
Public Access
1
0

daemon: setting foundation for client creation

This commit is contained in:
2025-04-25 20:02:18 -07:00
parent fe32efef2c
commit 82192ffbe5
10 changed files with 210 additions and 27 deletions

View File

@@ -1,7 +1,8 @@
use dbus::arg;
use dbus_tree::MethodErr;
use std::sync::{Arc, Mutex, MutexGuard};
use log::info;
use std::future::Future;
use std::thread;
use crate::daemon::Daemon;
use crate::dbus::interface::NetBuzzertKordophoneRepository as DbusRepository;
@@ -47,10 +48,14 @@ impl DbusRepository for ServerImpl {
fn sync_all_conversations(&mut self) -> Result<bool, dbus::MethodErr> {
let mut daemon = self.get_daemon()?;
daemon.sync_all_conversations().map_err(|e| {
log::error!("Failed to sync conversations: {}", e);
MethodErr::failed(&format!("Failed to sync conversations: {}", e))
})?;
// TODO: We don't actually probably want to block here.
run_sync_future(daemon.sync_all_conversations())
.unwrap()
.map_err(|e| {
log::error!("Failed to sync conversations: {}", e);
MethodErr::failed(&format!("Failed to sync conversations: {}", e))
})?;
Ok(true)
}
@@ -90,3 +95,26 @@ impl DbusSettings for ServerImpl {
}
}
fn run_sync_future<F, T>(f: F) -> Result<T, MethodErr>
where
T: Send,
F: Future<Output = T> + Send,
{
// We use `scope` here to ensure that the thread is joined before the
// function returns. This allows us to capture references of values that
// have lifetimes shorter than 'static, which is what thread::spawn requires.
thread::scope(move |s| {
s.spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|_| MethodErr::failed("Unable to create tokio runtime"))?;
let result = rt.block_on(f);
Ok(result)
})
.join()
})
.expect("Error joining runtime thread")
}