39 lines
1.2 KiB
Rust
39 lines
1.2 KiB
Rust
|
|
use crate::daemon::{events::Event, signals::Signal, DaemonResult};
|
||
|
|
use std::sync::Arc;
|
||
|
|
use tokio::sync::{mpsc, oneshot, Mutex};
|
||
|
|
|
||
|
|
/// XPC IPC agent that forwards daemon events and signals over libxpc.
|
||
|
|
#[derive(Clone)]
|
||
|
|
pub struct XpcAgent {
|
||
|
|
event_sink: mpsc::Sender<Event>,
|
||
|
|
signal_receiver: Arc<Mutex<Option<mpsc::Receiver<Signal>>>>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl XpcAgent {
|
||
|
|
/// Create a new XPC agent with an event sink and signal receiver.
|
||
|
|
pub fn new(event_sink: mpsc::Sender<Event>, signal_receiver: mpsc::Receiver<Signal>) -> Self {
|
||
|
|
Self {
|
||
|
|
event_sink,
|
||
|
|
signal_receiver: Arc::new(Mutex::new(Some(signal_receiver))),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Run the XPC agent: perform a basic GetVersion IPC call to the daemon and print the result.
|
||
|
|
pub async fn run(self) {
|
||
|
|
todo!()
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Send an event to the daemon and await its reply.
|
||
|
|
pub async fn send_event<T>(
|
||
|
|
&self,
|
||
|
|
make_event: impl FnOnce(crate::daemon::events::Reply<T>) -> Event,
|
||
|
|
) -> DaemonResult<T> {
|
||
|
|
let (tx, rx) = oneshot::channel();
|
||
|
|
self.event_sink
|
||
|
|
.send(make_event(tx))
|
||
|
|
.await
|
||
|
|
.map_err(|_| "Failed to send event")?;
|
||
|
|
rx.await.map_err(|_| "Failed to receive reply".into())
|
||
|
|
}
|
||
|
|
}
|