diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-07-10 09:10:48 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-07-10 09:10:48 +0200 |
| commit | 5e82c3b62b7bac95e27f7c7782ceed6379537643 (patch) | |
| tree | 9f8f482b236e321bb617eff87c1582723d769fa8 /talpid_ipc/src | |
| parent | 82a5ddf1bd3e697ae165af991ea0a3e2476f7bc8 (diff) | |
| parent | ec4d8bd1a5e35bd2f653042d673079dec20e9f49 (diff) | |
| download | mullvadvpn-5e82c3b62b7bac95e27f7c7782ceed6379537643.tar.xz mullvadvpn-5e82c3b62b7bac95e27f7c7782ceed6379537643.zip | |
Merge branch 'cli-with-rpc'
Diffstat (limited to 'talpid_ipc/src')
| -rw-r--r-- | talpid_ipc/src/client.rs | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/talpid_ipc/src/client.rs b/talpid_ipc/src/client.rs index 1776d46273..763d5b225e 100644 --- a/talpid_ipc/src/client.rs +++ b/talpid_ipc/src/client.rs @@ -12,7 +12,7 @@ pub use self::errors::*; struct Factory { request: String, - result_tx: mpsc::Sender<Result<()>>, + result_tx: mpsc::Sender<Result<serde_json::Value>>, } impl ws::Factory for Factory { @@ -34,30 +34,37 @@ impl ws::Factory for Factory { struct Handler { sender: ws::Sender, - result_tx: mpsc::Sender<Result<()>>, + result_tx: mpsc::Sender<Result<serde_json::Value>>, } impl Handler { - fn validate_reply(&self, msg: ws::Message) -> ws::Result<()> { + fn validate_reply(&self, msg: ws::Message) -> ws::Result<serde_json::Value> { let json: serde_json::Value = match msg { ws::Message::Text(s) => serde_json::from_str(&s), ws::Message::Binary(b) => serde_json::from_slice(&b), } .map_err(|e| ws::Error::from(Box::new(e)))?; debug!("JSON response: {}", json); + let result = + match json { + serde_json::Value::Object(mut map) => map.remove("result"), + _ => None, + } + .ok_or(ws::Error::new(ws::ErrorKind::Protocol, "Invalid reply, no 'result'"),)?; // TODO(linus): Properly validate reply - Ok(()) + Ok(result) } } impl ws::Handler for Handler { fn on_message(&mut self, msg: ws::Message) -> ws::Result<()> { - self.validate_reply(msg)?; + debug!("WsIpcClient incoming message: {:?}", msg); + let reply = self.validate_reply(msg)?; let close_result = self.sender.close(ws::CloseCode::Normal); if let Err(e) = close_result.chain_err(|| "Unable to close WebSocket") { self.result_tx.send(Err(e)).unwrap(); } - self.result_tx.send(Ok(())).unwrap(); + self.result_tx.send(Ok(reply)).unwrap(); Ok(()) } } @@ -74,7 +81,7 @@ impl WsIpcClient { Ok(WsIpcClient { url, next_id: 1 }) } - pub fn call<T>(&mut self, method: &str, params: &T) -> Result<()> + pub fn call<T>(&mut self, method: &str, params: &T) -> Result<serde_json::Value> where T: serde::Serialize { let (result_tx, result_rx) = mpsc::channel(); |
