From 3429caa87699d986f799a11f6e0f4526e723b655 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Fri, 14 Jun 2024 22:49:53 +0200 Subject: jsonrpc: client use unbounded channels as buffer for sending requests & clean up examples --- jsonrpc/examples/client.rs | 45 ++++++++++++++++++------------- jsonrpc/examples/pubsub_client.rs | 4 ++- jsonrpc/examples/pubsub_server.rs | 7 ++--- jsonrpc/examples/server.rs | 2 +- jsonrpc/examples/tokio_server/src/main.rs | 2 +- 5 files changed, 35 insertions(+), 25 deletions(-) (limited to 'jsonrpc/examples') diff --git a/jsonrpc/examples/client.rs b/jsonrpc/examples/client.rs index 662aacb..0f87ecb 100644 --- a/jsonrpc/examples/client.rs +++ b/jsonrpc/examples/client.rs @@ -1,5 +1,6 @@ use std::time::Duration; +use log::info; use serde::{Deserialize, Serialize}; use smol::Timer; @@ -21,29 +22,35 @@ fn main() { .expect("Create client builder") .build() .await - .unwrap(); - - let clientc = client.clone(); - smol::spawn(async move { - loop { - Timer::after(Duration::from_millis(500)).await; - let result: Pong = clientc.call("Calc.ping", ()).await.unwrap(); - println!("ping msg result: {:?}", result); - } - }) - .detach(); + .expect("Create rpc client"); let params = Req { x: 10, y: 7 }; - let result: u32 = client.call("Calc.add", params).await.unwrap(); - println!("result {result}"); + let result: u32 = client + .call("Calc.add", params) + .await + .expect("Call Calc.add method"); + info!("Add result: {result}"); let params = Req { x: 10, y: 7 }; - let result: u32 = client.call("Calc.sub", params).await.unwrap(); - println!("result {result}"); - - let result: String = client.call("Calc.version", ()).await.unwrap(); - println!("result {result}"); + let result: u32 = client + .call("Calc.sub", params) + .await + .expect("Call Calc.sub method"); + info!("Sub result: {result}"); - Timer::after(Duration::from_secs(10)).await; + let result: String = client + .call("Calc.version", ()) + .await + .expect("Call Calc.version method"); + info!("Version result: {result}"); + + loop { + Timer::after(Duration::from_millis(100)).await; + let result: Pong = client + .call("Calc.ping", ()) + .await + .expect("Call Calc.ping method"); + info!("Ping result: {:?}", result); + } }); } diff --git a/jsonrpc/examples/pubsub_client.rs b/jsonrpc/examples/pubsub_client.rs index 830b32f..73570b8 100644 --- a/jsonrpc/examples/pubsub_client.rs +++ b/jsonrpc/examples/pubsub_client.rs @@ -1,5 +1,6 @@ use std::time::Duration; +use log::info; use serde::{Deserialize, Serialize}; use smol::Timer; @@ -25,7 +26,8 @@ async fn run_client() { smol::spawn(async move { loop { - let _m = sub.recv().await.unwrap(); + let m = sub.recv().await.expect("Receive new log msg"); + info!("Receive new log {m}"); } }) .detach(); diff --git a/jsonrpc/examples/pubsub_server.rs b/jsonrpc/examples/pubsub_server.rs index 74eb907..40ea756 100644 --- a/jsonrpc/examples/pubsub_server.rs +++ b/jsonrpc/examples/pubsub_server.rs @@ -1,5 +1,6 @@ use std::{sync::Arc, time::Duration}; +use log::error; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -36,9 +37,9 @@ impl Calc { let sub_id = sub.id.clone(); smol::spawn(async move { loop { - smol::Timer::after(std::time::Duration::from_secs(1)).await; + sleep(Duration::from_millis(500)).await; if let Err(err) = sub.notify(serde_json::json!("Hello")).await { - println!("Error send notification {err}"); + error!("Error send notification {err}"); break; } } @@ -75,7 +76,7 @@ fn main() { .expect("Build a new server"); // Start the server - server.start().await; + server.start(); sleep(Duration::MAX).await; }); diff --git a/jsonrpc/examples/server.rs b/jsonrpc/examples/server.rs index 470bd02..31e65dd 100644 --- a/jsonrpc/examples/server.rs +++ b/jsonrpc/examples/server.rs @@ -57,7 +57,7 @@ fn main() { .expect("start a new server"); // Start the server - server.start().await; + server.start(); sleep(Duration::MAX).await; }); diff --git a/jsonrpc/examples/tokio_server/src/main.rs b/jsonrpc/examples/tokio_server/src/main.rs index 0a47fda..a9b2b32 100644 --- a/jsonrpc/examples/tokio_server/src/main.rs +++ b/jsonrpc/examples/tokio_server/src/main.rs @@ -91,7 +91,7 @@ async fn main() { .expect("start a new server"); // Start the server - server.start().await; + server.start(); tokio::time::sleep(Duration::MAX).await; } -- cgit v1.2.3