summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2017-08-23 16:38:58 +0200
committerLinus Färnstrand <linus@mullvad.net>2017-08-24 11:25:19 +0200
commit06f62cdc64b04ab14c3188acad92bddea5265357 (patch)
treecd96a856b16cbb1d0573b12e81aaa2c28c6aad47
parent188fc9d3cf882ecca6b699bef7dc1ff7c09dd008 (diff)
downloadmullvadvpn-06f62cdc64b04ab14c3188acad92bddea5265357.tar.xz
mullvadvpn-06f62cdc64b04ab14c3188acad92bddea5265357.zip
Refactor communication from mgmt to daemon
-rw-r--r--mullvad-daemon/src/management_interface.rs34
1 files changed, 22 insertions, 12 deletions
diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs
index d89c5f6024..d01d951124 100644
--- a/mullvad-daemon/src/management_interface.rs
+++ b/mullvad-daemon/src/management_interface.rs
@@ -232,6 +232,16 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterface<T> {
};
result.boxed()
}
+
+ /// Sends a command to the daemon and maps a successful send into the future `item`.
+ fn send_command_to_daemon<R, F>(&self, command: TunnelCommand, item: F) -> BoxFuture<R, Error>
+ where F: Future<Item = R, Error = Error> + Send + 'static
+ {
+ future::result(self.tx.lock().unwrap().send(command))
+ .map_err(|_| Error::internal_error())
+ .and_then(|_| item)
+ .boxed()
+ }
}
impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for ManagementInterface<T> {
@@ -255,19 +265,19 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for Managem
fn set_account(&self, account_token: Option<AccountToken>) -> BoxFuture<(), Error> {
trace!("set_account");
let (tx, rx) = sync::oneshot::channel();
- match self.tx.lock().unwrap().send(TunnelCommand::SetAccount(tx, account_token)) {
- Ok(()) => rx.map_err(|_| Error::internal_error()).boxed(),
- Err(_) => future::err(Error::internal_error()).boxed(),
- }
+ self.send_command_to_daemon(
+ TunnelCommand::SetAccount(tx, account_token),
+ rx.map_err(|_| Error::internal_error()),
+ )
}
fn get_account(&self) -> BoxFuture<Option<AccountToken>, Error> {
trace!("get_account");
let (tx, rx) = sync::oneshot::channel();
- match self.tx.lock().unwrap().send(TunnelCommand::GetAccount(tx)) {
- Ok(()) => rx.map_err(|_| Error::internal_error()).boxed(),
- Err(_) => future::err(Error::internal_error()).boxed(),
- }
+ self.send_command_to_daemon(
+ TunnelCommand::GetAccount(tx),
+ rx.map_err(|_| Error::internal_error()),
+ )
}
fn set_country(&self, _country_code: CountryCode) -> Result<(), Error> {
@@ -301,10 +311,10 @@ impl<T: From<TunnelCommand> + 'static + Send> ManagementInterfaceApi for Managem
fn get_state(&self) -> BoxFuture<DaemonState, Error> {
trace!("get_state");
let (state_tx, state_rx) = sync::oneshot::channel();
- match self.tx.lock().unwrap().send(TunnelCommand::GetState(state_tx)) {
- Ok(()) => state_rx.map_err(|_| Error::internal_error()).boxed(),
- Err(_) => future::err(Error::internal_error()).boxed(),
- }
+ self.send_command_to_daemon(
+ TunnelCommand::GetState(state_tx),
+ state_rx.map_err(|_| Error::internal_error()),
+ )
}
fn get_ip(&self) -> Result<IpAddr, Error> {