Private
Public Access
1
0

daemon: reorg: use channels for comms instead of copying daemon arc/mutex

This commit is contained in:
2025-04-27 12:53:45 -07:00
parent ef74df9f28
commit 22554a7644
4 changed files with 124 additions and 78 deletions

View File

@@ -4,66 +4,74 @@ use std::sync::Arc;
use tokio::sync::{Mutex, MutexGuard};
use std::future::Future;
use std::thread;
use std::sync::mpsc;
use tokio::sync::oneshot;
use tokio::sync::mpsc;
use futures_util::future::FutureExt;
use crate::daemon::{Daemon, Event};
use crate::daemon::{
Daemon,
DaemonResult,
events::{Event, Reply},
};
use crate::dbus::interface::NetBuzzertKordophoneRepository as DbusRepository;
use crate::dbus::interface::NetBuzzertKordophoneSettings as DbusSettings;
#[derive(Clone)]
pub struct ServerImpl {
daemon: Arc<Mutex<Daemon>>,
event_sender: mpsc::Sender<Event>,
event_sink: mpsc::Sender<Event>,
}
impl ServerImpl {
pub fn new(daemon: Arc<Mutex<Daemon>>, event_sender: mpsc::Sender<Event>) -> Self {
Self { daemon, event_sender }
pub fn new(event_sink: mpsc::Sender<Event>) -> Self {
Self { event_sink }
}
pub async fn get_daemon(&self) -> MutexGuard<'_, Daemon> {
self.daemon.lock().await // .map_err(|_| MethodErr::failed("Failed to lock daemon"))
pub async fn send_event<T>(
&self,
make_event: impl FnOnce(Reply<T>) -> Event,
) -> DaemonResult<T> {
let (reply_tx, reply_rx) = oneshot::channel();
self.event_sink.send(make_event(reply_tx))
.await
.map_err(|_| "Failed to send event")?;
reply_rx.await.map_err(|_| "Failed to receive reply".into())
}
pub fn daemon_then<F, T>(&self, f: F) -> Result<T, MethodErr>
where F: FnOnce(MutexGuard<'_, Daemon>) -> T + Send,
T: Send,
{
run_sync_future(self.get_daemon().then(|daemon| async move {
f(daemon)
}))
pub fn send_event_sync<T: Send>(
&self,
make_event: impl FnOnce(Reply<T>) -> Event + Send,
) -> Result<T, MethodErr> {
run_sync_future(self.send_event(make_event))
.unwrap()
.map_err(|e| MethodErr::failed(&format!("Daemon error: {}", e)))
}
}
impl DbusRepository for ServerImpl {
fn get_version(&mut self) -> Result<String, MethodErr> {
self.daemon_then(|daemon| daemon.version.clone())
self.send_event_sync(Event::GetVersion)
}
fn get_conversations(&mut self) -> Result<Vec<arg::PropMap>, dbus::MethodErr> {
self.daemon_then(|mut daemon| {
let conversations = daemon.get_conversations();
// Convert conversations to DBus property maps
let result = conversations.into_iter().map(|conv| {
let mut map = arg::PropMap::new();
map.insert("guid".into(), arg::Variant(Box::new(conv.guid)));
map.insert("display_name".into(), arg::Variant(Box::new(conv.display_name.unwrap_or_default())));
map.insert("unread_count".into(), arg::Variant(Box::new(conv.unread_count as i32)));
map
}).collect();
Ok(result)
})?
self.send_event_sync(Event::GetAllConversations)
.and_then(|conversations| {
// Convert conversations to DBus property maps
let result = conversations.into_iter().map(|conv| {
let mut map = arg::PropMap::new();
map.insert("guid".into(), arg::Variant(Box::new(conv.guid)));
map.insert("display_name".into(), arg::Variant(Box::new(conv.display_name.unwrap_or_default())));
map.insert("unread_count".into(), arg::Variant(Box::new(conv.unread_count as i32)));
map
}).collect();
Ok(result)
})
}
fn sync_all_conversations(&mut self) -> Result<(), dbus::MethodErr> {
self.event_sender.send(Event::SyncAllConversations).unwrap_or_else(|e| {
log::error!("Error sending sync event: {}", e);
});
Ok(())
self.send_event_sync(Event::SyncAllConversations)
}
}