diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-07-06 14:44:49 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-07-06 22:37:00 +0200 |
| commit | e068a355ea24d8c779fcf983cdb2b49b5f566744 (patch) | |
| tree | 4ac540edcf2054beb71c72eb222d93893a947fcb | |
| parent | 82a5ddf1bd3e697ae165af991ea0a3e2476f7bc8 (diff) | |
| download | mullvadvpn-e068a355ea24d8c779fcf983cdb2b49b5f566744.tar.xz mullvadvpn-e068a355ea24d8c779fcf983cdb2b49b5f566744.zip | |
Change WsIpcClient to return result of response
| -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(); |
