summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-04-26 18:32:39 -0300
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-06-20 08:08:32 -0300
commitc0dbc4cb773a0350f24932eda1d9bfd4034cee7f (patch)
treea6409b4db75b3c0a19ab07d6857dcf4f7fe00d86
parent1c2cf009f9c191ec2f90c565c9e29b230b66815f (diff)
downloadmullvadvpn-c0dbc4cb773a0350f24932eda1d9bfd4034cee7f.tar.xz
mullvadvpn-c0dbc4cb773a0350f24932eda1d9bfd4034cee7f.zip
Allow subscribing to `new_state` RPC events
-rw-r--r--mullvad-ipc-client/src/lib.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/mullvad-ipc-client/src/lib.rs b/mullvad-ipc-client/src/lib.rs
index d4300c8605..a88a666307 100644
--- a/mullvad-ipc-client/src/lib.rs
+++ b/mullvad-ipc-client/src/lib.rs
@@ -9,6 +9,7 @@ extern crate talpid_types;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
+use std::sync::mpsc;
use mullvad_types::account::{AccountData, AccountToken};
use mullvad_types::location::GeoIpLocation;
@@ -59,6 +60,11 @@ error_chain! {
display("Failed to call RPC method \"{}\"", method)
}
+ RpcSubscribeError(event: String) {
+ description("Failed to subscribe to RPC event")
+ display("Failed to subscribe to RPC event \"{}\"", event)
+ }
+
StartRpcClient(address: String) {
description("Failed to start RPC client")
display("Failed to start RPC client to {}", address)
@@ -210,6 +216,25 @@ impl DaemonRpcClient {
.call(method, args)
.chain_err(|| ErrorKind::RpcCallError(method.to_owned()))
}
+
+ pub fn new_state_subscribe(&mut self) -> Result<mpsc::Receiver<DaemonState>> {
+ self.subscribe("new_state")
+ }
+
+ pub fn subscribe<T>(&mut self, event: &str) -> Result<mpsc::Receiver<T>>
+ where
+ T: for<'de> serde::Deserialize<'de> + Send + 'static,
+ {
+ let (event_tx, event_rx) = mpsc::channel();
+ let subscribe_method = format!("{}_subscribe", event);
+ let unsubscribe_method = format!("{}_unsubscribe", event);
+
+ self.rpc_client
+ .subscribe::<T, T>(subscribe_method, unsubscribe_method, event_tx)
+ .chain_err(|| ErrorKind::RpcSubscribeError(event.to_owned()))?;
+
+ Ok(event_rx)
+ }
}
#[cfg(unix)]