summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-02-20 14:44:41 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-02-24 12:45:09 +0000
commit9e13ffbc4f80f7f8be58d209dc42ab0e01ad8e4c (patch)
tree99133dfc59f323f798b9f40ad6e8aa9ac9595f98
parentf102b1fdee47f9604dcb5a5214dbe885cd1f3874 (diff)
downloadmullvadvpn-9e13ffbc4f80f7f8be58d209dc42ab0e01ad8e4c.tar.xz
mullvadvpn-9e13ffbc4f80f7f8be58d209dc42ab0e01ad8e4c.zip
Create `DaemonEventSender` helper type
-rw-r--r--mullvad-daemon/src/lib.rs60
1 files changed, 59 insertions, 1 deletions
diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs
index 3af174e192..8d5f386f5b 100644
--- a/mullvad-daemon/src/lib.rs
+++ b/mullvad-daemon/src/lib.rs
@@ -43,7 +43,9 @@ use settings::Settings;
#[cfg(not(target_os = "android"))]
use std::path::Path;
use std::{
- io, mem,
+ io,
+ marker::PhantomData,
+ mem,
path::PathBuf,
sync::{mpsc, Arc},
thread,
@@ -314,6 +316,62 @@ impl DaemonCommandSender {
}
}
+pub(crate) struct DaemonEventSender<E = InternalDaemonEvent> {
+ sender: UnboundedSender<InternalDaemonEvent>,
+ _event: PhantomData<E>,
+}
+
+impl<E> Clone for DaemonEventSender<E>
+where
+ InternalDaemonEvent: From<E>,
+{
+ fn clone(&self) -> Self {
+ DaemonEventSender {
+ sender: self.sender.clone(),
+ _event: PhantomData,
+ }
+ }
+}
+
+impl DaemonEventSender {
+ pub fn new(sender: UnboundedSender<InternalDaemonEvent>) -> Self {
+ DaemonEventSender {
+ sender,
+ _event: PhantomData,
+ }
+ }
+
+ pub fn to_specialized_sender<E>(&self) -> DaemonEventSender<E>
+ where
+ InternalDaemonEvent: From<E>,
+ {
+ DaemonEventSender {
+ sender: self.sender.clone(),
+ _event: PhantomData,
+ }
+ }
+}
+
+impl<E> DaemonEventSender<E>
+where
+ InternalDaemonEvent: From<E>,
+{
+ pub fn is_closed(&self) -> bool {
+ self.sender.is_closed()
+ }
+}
+
+impl<E> Sender<E> for DaemonEventSender<E>
+where
+ InternalDaemonEvent: From<E>,
+{
+ fn send(&self, event: E) -> Result<(), ()> {
+ self.sender
+ .unbounded_send(InternalDaemonEvent::from(event))
+ .map_err(|_| ())
+ }
+}
+
/// Trait representing something that can broadcast daemon events.
pub trait EventListener {
/// Notify that the tunnel state changed.