From 073e0df4564f3c4841a153a8a57fd23d54bfcf12 Mon Sep 17 00:00:00 2001 From: adria0 Date: Sun, 9 Feb 2020 17:42:24 +0100 Subject: [PATCH] wip --- Cargo.toml | 3 +-- examples/iot-solar_std.rs | 0 examples/ssb-cli.rs | 2 +- src/patchwork/api.rs | 45 +++++++++++++++++++++++++------------ src/rpc/client.rs | 47 ++++++++++++++++++++++++++++----------- src/rpc/mod.rs | 2 +- 6 files changed, 68 insertions(+), 31 deletions(-) create mode 100644 examples/iot-solar_std.rs diff --git a/Cargo.toml b/Cargo.toml index 72c13ad..eb3cb28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,9 +17,8 @@ async-std = { version = "1.1.0", features=["unstable","attributes"] } crossbeam = "0.7.3" log = "0.4.8" env_logger = "0.7.1" -serde = "1.0.104" +serde = { version = "1.0.104", features = ["derive"] } serde_json = { version = "1.0.41", features=["preserve_order","arbitrary_precision"] } -serde_derive = "1.0.104" dirs = "2.0" futures = "0.3.1" snap = "0.2.5" diff --git a/examples/iot-solar_std.rs b/examples/iot-solar_std.rs new file mode 100644 index 0000000..e69de29 diff --git a/examples/ssb-cli.rs b/examples/ssb-cli.rs index fd11825..32c2657 100644 --- a/examples/ssb-cli.rs +++ b/examples/ssb-cli.rs @@ -69,7 +69,7 @@ async fn main() -> AnyResult<()> { println!("💃 handshake complete"); let (box_stream_read, box_stream_write) = - BoxStream::from_handhake(&mut socket, handshake, 0x8000) + BoxStream::from_handshake(&socket,&socket,handshake, 0x8000) .split_read_write(); let mut client = ApiClient::new(RpcClient::new(box_stream_read, box_stream_write)); diff --git a/src/patchwork/api.rs b/src/patchwork/api.rs index 0a70cb2..f9639d1 100644 --- a/src/patchwork/api.rs +++ b/src/patchwork/api.rs @@ -2,7 +2,7 @@ use async_std::io::{Read, Write}; use serde_json; use std::str::FromStr; -use crate::rpc::{RpcClient, Header, RequestNo, RpcType}; +use crate::rpc::{RpcClient, Header, RequestNo, RpcType, Body}; use crate::feed::Message; use crate::feed::Feed; @@ -273,15 +273,24 @@ impl ApiClient { // whoami: sync // Get information about the current ssb-server user. pub async fn send_whoami(&mut self) -> Result { - let args: [&str; 0] = []; - let req_no = self.rpc.send(&["whoami"], RpcType::Async, &args).await?; + let body = Body { + name : vec!["whoami".to_string()], + rpc_type : RpcType::Async, + args : Vec::::new() + }; + let req_no = self.rpc.send(&body).await?; Ok(req_no) } // get: async // Get a message by its hash-id. (sould start with %) pub async fn send_get(&mut self, msg_id: &str) -> Result { - let req_no = self.rpc.send(&["get"], RpcType::Async, &msg_id).await?; + let body = Body { + name : vec!["get".to_string()], + rpc_type : RpcType::Async, + args : vec![msg_id.to_string()] + }; + let req_no = self.rpc.send(&body).await?; Ok(req_no) } // createHistoryStream: source @@ -290,10 +299,12 @@ impl ApiClient { &mut self, args: &'a CreateHistoryStreamArgs<'a>, ) -> Result { - let req_no = self - .rpc - .send(&["createHistoryStream"], RpcType::Source, &args) - .await?; + let body = Body { + name : vec!["createHistoryStream".to_string()], + rpc_type : RpcType::Source, + args : args + }; + let req_no = self.rpc.send(&body).await?; Ok(req_no) } @@ -303,18 +314,24 @@ impl ApiClient { &mut self, args: &'a CreateStreamArgs, ) -> Result { - let req_no = self - .rpc - .send(&["createFeedStream"], RpcType::Source, &args) - .await?; + let body = Body { + name : vec!["createFeedStream".to_string()], + rpc_type : RpcType::Source, + args + }; + let req_no = self.rpc.send(&body).await?; Ok(req_no) } // latest: source // Get the seq numbers of the latest messages of all users in the database. pub async fn send_latest(&mut self) -> Result { - let args: [&str; 0] = []; - let req_no = self.rpc.send(&["latest"], RpcType::Source, &args).await?; + let body = Body { + name : vec!["latest".to_string()], + rpc_type : RpcType::Source, + args : Vec::::new(), + }; + let req_no = self.rpc.send(&body).await?; Ok(req_no) } } diff --git a/src/rpc/client.rs b/src/rpc/client.rs index 646663c..ff827c3 100644 --- a/src/rpc/client.rs +++ b/src/rpc/client.rs @@ -24,9 +24,35 @@ pub enum BodyType { JSON, } -#[derive(Debug,PartialEq)] +/* + let mut body = String::from("{\"name\":"); + body.push_str(&serde_json::to_string(&name)?); + body.push_str(",\"type\":\""); + body.push_str(rpc_type.rpc_id()); + body.push_str("\",\"args\":["); + body.push_str(&serde_json::to_string(&args)?); + body.push_str("]}"); +*/ + +#[derive(Serialize)] +pub struct Body { + pub name : Vec, + #[serde(rename="type")] + pub rpc_type : RpcType, + pub args : T, +} + +impl Body { + pub fn new(name: Vec, rpc_type : RpcType, args:T) -> Self { + Body { name, rpc_type, args } + } +} + +#[derive(Serialize,Debug,PartialEq)] pub enum RpcType { + #[serde(rename="async")] Async, + #[serde(rename="source")] Source, } @@ -38,7 +64,6 @@ impl RpcType { } } } - #[derive(Debug,PartialEq)] pub struct Header { pub req_no : RequestNo, @@ -124,28 +149,24 @@ impl RpcClient { Ok((rpc_header,rpc_body)) } - pub async fn send(&mut self, name : &[&str], rpc_type: RpcType, args :&T) -> Result{ + pub async fn send(&mut self, body : &Body) -> Result{ self.req_no+=1; - let mut body = String::from("{\"name\":"); - body.push_str(&serde_json::to_string(&name)?); - body.push_str(",\"type\":\""); - body.push_str(rpc_type.rpc_id()); - body.push_str("\",\"args\":["); - body.push_str(&serde_json::to_string(&args)?); - body.push_str("]}"); + let body_str = serde_json::to_string(body)?; let rpc_header = Header { req_no : self.req_no, - is_stream : rpc_type == RpcType::Source, + is_stream : body.rpc_type == RpcType::Source, is_end_or_error : false, body_type : BodyType::JSON, - body_len : body.len() as u32, + body_len : body_str.as_bytes().len() as u32, }.to_array(); + println!("\n{}\n",body_str); + self.box_writer.write_all(&rpc_header[..]).await?; - self.box_writer.write_all(body.as_bytes()).await?; + self.box_writer.write_all(body_str.as_bytes()).await?; self.box_writer.flush().await?; Ok(self.req_no) diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index 965416d..c7d98ee 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -1,5 +1,5 @@ mod client; mod error; -pub use client::{RpcClient,Header, RequestNo, RpcType}; +pub use client::{RpcClient,Header, RequestNo, RpcType, Body}; pub use error::{Error,Result};