diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2018-01-11 10:46:45 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2018-01-15 15:54:16 +0100 |
| commit | ba7346a62cdf71e7fbb810b1358ca055c6906199 (patch) | |
| tree | d4de95d8a7340f2779d7674040217a1d6d90ea74 | |
| parent | 6bf68387ae5d5a70bd2d0738b3291c66368059b8 (diff) | |
| download | mullvadvpn-ba7346a62cdf71e7fbb810b1358ca055c6906199.tar.xz mullvadvpn-ba7346a62cdf71e7fbb810b1358ca055c6906199.zip | |
Add event loop creation to mullvad-rpc
| -rw-r--r-- | mullvad-daemon/src/bin/list-relays.rs | 2 | ||||
| -rw-r--r-- | mullvad-rpc/Cargo.toml | 2 | ||||
| -rw-r--r-- | mullvad-rpc/src/event_loop.rs | 41 | ||||
| -rw-r--r-- | mullvad-rpc/src/lib.rs | 14 |
4 files changed, 57 insertions, 2 deletions
diff --git a/mullvad-daemon/src/bin/list-relays.rs b/mullvad-daemon/src/bin/list-relays.rs index 2718c05396..7a1fcb092a 100644 --- a/mullvad-daemon/src/bin/list-relays.rs +++ b/mullvad-daemon/src/bin/list-relays.rs @@ -17,7 +17,7 @@ error_chain!{} quick_main!(run); fn run() -> Result<()> { - let rpc_http_handle = mullvad_rpc::connect().chain_err(|| "Unable to connect RPC")?; + let rpc_http_handle = mullvad_rpc::standalone().chain_err(|| "Unable to connect RPC")?; let mut client = mullvad_rpc::RelayListProxy::new(rpc_http_handle); let relays = client diff --git a/mullvad-rpc/Cargo.toml b/mullvad-rpc/Cargo.toml index d49557cb96..69d6cd5bbf 100644 --- a/mullvad-rpc/Cargo.toml +++ b/mullvad-rpc/Cargo.toml @@ -7,8 +7,10 @@ license = "GPL-3.0" [dependencies] chrono = { version = "0.4", features = ["serde"] } +error-chain = "0.11" jsonrpc-client-core = "0.2.1" jsonrpc-client-http = "0.2.1" serde_json = "1.0" +tokio-core = "0.1" mullvad-types = { path = "../mullvad-types" } diff --git a/mullvad-rpc/src/event_loop.rs b/mullvad-rpc/src/event_loop.rs new file mode 100644 index 0000000000..aaae5900ec --- /dev/null +++ b/mullvad-rpc/src/event_loop.rs @@ -0,0 +1,41 @@ +use std::thread; +use tokio_core::reactor::Core; + +error_chain! { + errors { + CoreError { description("Error when creating event loop") } + InitCallbackError { description("Error while executing supplied init closure") } + } +} + +/// Creates a new tokio event loop on a new thread, runs the provided `init` closure on the thread +/// and sends back the result. +/// Used to spawn futures on the core in the separate thread and be able to return sendable handles. +pub fn create<F, T, E>(init: F) -> Result<T> +where + F: FnOnce(&mut Core) -> ::std::result::Result<T, E> + Send + 'static, + T: Send + 'static, + E: ::std::error::Error + Send + 'static, +{ + let (tx, rx) = ::std::sync::mpsc::channel(); + thread::spawn(move || match create_core(init) { + Err(e) => tx.send(Err(e)).unwrap(), + Ok((mut core, out)) => { + tx.send(Ok(out)).unwrap(); + loop { + core.turn(None); + } + } + }); + rx.recv().unwrap() +} + +fn create_core<F, T, E>(init: F) -> Result<(Core, T)> +where + F: FnOnce(&mut Core) -> ::std::result::Result<T, E> + Send + 'static, + E: ::std::error::Error + Send + 'static, +{ + let mut core = Core::new().chain_err(|| ErrorKind::CoreError)?; + let out = init(&mut core).chain_err(|| ErrorKind::InitCallbackError)?; + Ok((core, out)) +} diff --git a/mullvad-rpc/src/lib.rs b/mullvad-rpc/src/lib.rs index 19d9a29287..51da875891 100644 --- a/mullvad-rpc/src/lib.rs +++ b/mullvad-rpc/src/lib.rs @@ -8,15 +8,19 @@ extern crate chrono; #[macro_use] +extern crate error_chain; +#[macro_use] extern crate jsonrpc_client_core; extern crate jsonrpc_client_http; extern crate serde_json; +extern crate tokio_core; extern crate mullvad_types; use chrono::DateTime; use chrono::offset::Utc; use jsonrpc_client_http::HttpTransport; +use tokio_core::reactor::Handle; pub use jsonrpc_client_core::{Error, ErrorKind}; pub use jsonrpc_client_http::{Error as HttpError, HttpHandle}; @@ -26,11 +30,19 @@ use mullvad_types::relay_list::RelayList; use std::collections::HashMap; +pub mod event_loop; + static MASTER_API_URI: &str = "https://api.mullvad.net/rpc/"; -pub fn connect() -> Result<HttpHandle, HttpError> { +/// Create and returns a `HttpHandle` running on the given core handle. +pub fn shared(handle: &Handle) -> Result<HttpHandle, HttpError> { + HttpTransport::shared(handle)?.handle(MASTER_API_URI) +} + +/// Spawns a tokio core on a new thread and returns a `HttpHandle` running on that core. +pub fn standalone() -> Result<HttpHandle, HttpError> { HttpTransport::new()?.handle(MASTER_API_URI) } |
