summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-04-11 14:08:59 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-05-17 12:12:38 +0000
commitc80ab2aad7b97f88214bdb358751cc0c16e12408 (patch)
tree62831451b228c2c697978a9ef622bb4614edc73f
parentd0e614fa397386e25e131badde5e1c43f2332468 (diff)
downloadmullvadvpn-c80ab2aad7b97f88214bdb358751cc0c16e12408.tar.xz
mullvadvpn-c80ab2aad7b97f88214bdb358751cc0c16e12408.zip
Create method to obtain a `DaemonCommandSender`
-rw-r--r--mullvad-daemon/src/lib.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs
index f531ce9e36..37d6e7f6d5 100644
--- a/mullvad-daemon/src/lib.rs
+++ b/mullvad-daemon/src/lib.rs
@@ -20,7 +20,8 @@ mod relays;
mod rpc_uniqueness_check;
pub mod version;
-use crate::management_interface::{BoxFuture, ManagementCommand, ManagementInterfaceServer};
+pub use crate::management_interface::ManagementCommand;
+use crate::management_interface::{BoxFuture, ManagementInterfaceServer};
use futures::{
future,
sync::{mpsc::UnboundedSender, oneshot},
@@ -59,6 +60,9 @@ pub enum Error {
#[error(display = "Another instance of the daemon is already running")]
DaemonIsAlreadyRunning,
+ #[error(display = "Failed to send command to daemon because it is not running")]
+ DaemonUnavailable,
+
#[error(display = "Unable to initialize network event loop")]
InitIoEventLoop(#[error(cause)] io::Error),
@@ -162,6 +166,17 @@ impl DaemonExecutionState {
}
}
+pub struct DaemonCommandSender(IntoSender<ManagementCommand, InternalDaemonEvent>);
+
+impl DaemonCommandSender {
+ pub(crate) fn new(internal_event_sender: mpsc::Sender<InternalDaemonEvent>) -> Self {
+ DaemonCommandSender(IntoSender::from(internal_event_sender))
+ }
+
+ pub fn send(&self, command: ManagementCommand) -> Result<()> {
+ self.0.send(command).map_err(|_| Error::DaemonUnavailable)
+ }
+}
pub struct Daemon {
tunnel_command_tx: SyncUnboundedSender<TunnelCommand>,
@@ -309,6 +324,11 @@ impl Daemon {
});
}
+ /// Retrieve a channel for sending daemon commands.
+ pub fn command_sender(&self) -> DaemonCommandSender {
+ DaemonCommandSender::new(self.tx.clone())
+ }
+
/// Consume the `Daemon` and run the main event loop. Blocks until an error happens or a
/// shutdown event is received.
pub fn run(mut self) -> Result<()> {