diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-07-21 14:43:13 +0200 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-07-25 08:00:45 +0200 |
| commit | 56127fc7d3d84b6510b5d4ac242cb7f90ee27b11 (patch) | |
| tree | bd96a0a02b9137c8594afc8ba7842ffc59ec8e31 | |
| parent | 3d5472e94a7762174db086ca5c8fe7bc743c9e4f (diff) | |
| download | mullvadvpn-56127fc7d3d84b6510b5d4ac242cb7f90ee27b11.tar.xz mullvadvpn-56127fc7d3d84b6510b5d4ac242cb7f90ee27b11.zip | |
Extract management api event responses to separate methods
| -rw-r--r-- | mullvad-daemon/src/main.rs | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index 3710adc087..dfce02606b 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -31,6 +31,7 @@ mod settings; mod shutdown; use error_chain::ChainedError; +use jsonrpc_core::futures::sync; use management_interface::{ManagementInterfaceServer, TunnelCommand}; use mullvad_types::states::{DaemonState, SecurityState, TargetState}; use std::io; @@ -259,34 +260,43 @@ impl Daemon { fn handle_management_interface_event(&mut self, event: TunnelCommand) -> Result<()> { use TunnelCommand::*; match event { - SetTargetState(state) => { - if !self.shutdown { - self.set_target_state(state)?; - } else { - warn!("Ignoring target state change request due to shutdown"); - } - } - GetState(tx) => { - if let Err(_) = tx.send(self.last_broadcasted_state) { - warn!("Unable to send current state to management interface client",); - } - } - SetAccount(tx, account_token) => { - let save_result = self.settings.set_account_token(account_token); - match save_result.chain_err(|| "Unable to save settings") { - Ok(()) => if let Err(_) = tx.send(()) { - warn!("Unable to send response to management interface client"); - }, - Err(e) => error!("{}", e.display()), - } - } - GetAccount(tx) => { - if let Err(_) = tx.send(self.settings.get_account_token()) { - warn!("Unable to send current account to management interface client"); - } - } + SetTargetState(state) => self.on_set_target_state(state), + GetState(tx) => Ok(self.on_get_state(tx)), + SetAccount(tx, account_token) => Ok(self.on_set_account(tx, account_token)), + GetAccount(tx) => Ok(self.on_get_account(tx)), + } + } + + fn on_set_target_state(&mut self, new_target_state: TargetState) -> Result<()> { + if !self.shutdown { + self.set_target_state(new_target_state) + } else { + warn!("Ignoring target state change request due to shutdown"); + Ok(()) + } + } + + fn on_get_state(&self, tx: sync::oneshot::Sender<DaemonState>) { + if let Err(_) = tx.send(self.last_broadcasted_state) { + warn!("Unable to send current state to management interface client",); + } + } + + fn on_set_account(&mut self, tx: sync::oneshot::Sender<()>, account_token: Option<String>) { + let save_result = self.settings.set_account_token(account_token.clone()); + + match save_result.chain_err(|| "Unable to save settings") { + Ok(()) => if let Err(_) = tx.send(()) { + warn!("Unable to send response to management interface client"); + }, + Err(e) => error!("{}", e.display()), + } + } + + fn on_get_account(&self, tx: sync::oneshot::Sender<Option<String>>) { + if let Err(_) = tx.send(self.settings.get_account_token()) { + warn!("Unable to send current account to management interface client"); } - Ok(()) } fn handle_management_interface_exited(&self, result: talpid_ipc::Result<()>) -> Result<()> { |
