aboutsummaryrefslogtreecommitdiff
path: root/jsonrpc/examples
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-04-11 10:19:20 +0200
committerhozan23 <hozan23@karyontech.net>2024-05-19 13:51:30 +0200
commit0992071a7f1a36424bcfaf1fbc84541ea041df1a (patch)
tree961d73218af672797d49f899289bef295bc56493 /jsonrpc/examples
parenta69917ecd8272a4946cfd12c75bf8f8c075b0e50 (diff)
add support for tokio & improve net crate api
Diffstat (limited to 'jsonrpc/examples')
-rw-r--r--jsonrpc/examples/client.py10
-rw-r--r--jsonrpc/examples/client.rs11
-rw-r--r--jsonrpc/examples/server.rs34
3 files changed, 27 insertions, 28 deletions
diff --git a/jsonrpc/examples/client.py b/jsonrpc/examples/client.py
index 2066e82..745d5db 100644
--- a/jsonrpc/examples/client.py
+++ b/jsonrpc/examples/client.py
@@ -3,7 +3,7 @@ import random
import json
HOST = "127.0.0.1"
-PORT = 60000
+PORT = 6000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
@@ -15,7 +15,7 @@ req = {
"params": {"x": 4, "y": 3},
}
print("Send: ", req)
-s.sendall((json.dumps(req) + '\n').encode())
+s.sendall((json.dumps(req)).encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)
@@ -27,7 +27,7 @@ req = {
"params": {"x": 4, "y": 3},
}
print("Send: ", req)
-s.sendall((json.dumps(req) + '\n').encode())
+s.sendall((json.dumps(req)).encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)
@@ -39,7 +39,7 @@ req = {
"params": None,
}
print("Send: ", req)
-s.sendall((json.dumps(req) + '\n').encode())
+s.sendall((json.dumps(req)).encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)
@@ -51,7 +51,7 @@ req = {
"params": None,
}
print("Send: ", req)
-s.sendall((json.dumps(req) + '\n').encode())
+s.sendall((json.dumps(req)).encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)
diff --git a/jsonrpc/examples/client.rs b/jsonrpc/examples/client.rs
index 2c8cf83..3289772 100644
--- a/jsonrpc/examples/client.rs
+++ b/jsonrpc/examples/client.rs
@@ -1,7 +1,6 @@
use serde::{Deserialize, Serialize};
-use smol::net::TcpStream;
-use karyon_jsonrpc::{Client, ClientConfig};
+use karyon_jsonrpc::Client;
#[derive(Deserialize, Serialize)]
struct Req {
@@ -15,9 +14,11 @@ struct Pong {}
fn main() {
env_logger::init();
smol::future::block_on(async {
- let conn = TcpStream::connect("127.0.0.1:60000").await.unwrap();
- let config = ClientConfig::default();
- let client = Client::new(conn, config);
+ let client = Client::builder("tcp://127.0.0.1:6000")
+ .expect("Create client builder")
+ .build()
+ .await
+ .unwrap();
let params = Req { x: 10, y: 7 };
let result: u32 = client.call("Calc.add", params).await.unwrap();
diff --git a/jsonrpc/examples/server.rs b/jsonrpc/examples/server.rs
index 6953433..841e276 100644
--- a/jsonrpc/examples/server.rs
+++ b/jsonrpc/examples/server.rs
@@ -1,10 +1,7 @@
-use std::sync::Arc;
-
use serde::{Deserialize, Serialize};
use serde_json::Value;
-use smol::net::TcpListener;
-use karyon_jsonrpc::{register_service, JsonRPCError, Server, ServerConfig};
+use karyon_jsonrpc::{rpc_impl, Error, Server};
struct Calc {
version: String,
@@ -19,43 +16,44 @@ struct Req {
#[derive(Deserialize, Serialize)]
struct Pong {}
+#[rpc_impl]
impl Calc {
- async fn ping(&self, _params: Value) -> Result<Value, JsonRPCError> {
+ async fn ping(&self, _params: Value) -> Result<Value, Error> {
Ok(serde_json::json!(Pong {}))
}
- async fn add(&self, params: Value) -> Result<Value, JsonRPCError> {
+ async fn add(&self, params: Value) -> Result<Value, Error> {
let params: Req = serde_json::from_value(params)?;
Ok(serde_json::json!(params.x + params.y))
}
- async fn sub(&self, params: Value) -> Result<Value, JsonRPCError> {
+ async fn sub(&self, params: Value) -> Result<Value, Error> {
let params: Req = serde_json::from_value(params)?;
Ok(serde_json::json!(params.x - params.y))
}
- async fn version(&self, _params: Value) -> Result<Value, JsonRPCError> {
+ async fn version(&self, _params: Value) -> Result<Value, Error> {
Ok(serde_json::json!(self.version))
}
}
fn main() {
env_logger::init();
- let ex = Arc::new(smol::Executor::new());
- smol::block_on(ex.clone().run(async {
- // Creates a new server
- let listener = TcpListener::bind("127.0.0.1:60000").await.unwrap();
- let config = ServerConfig::default();
- let server = Server::new(listener, config, ex);
-
+ smol::block_on(async {
// Register the Calc service
- register_service!(Calc, ping, add, sub, version);
let calc = Calc {
version: String::from("0.1"),
};
- server.attach_service(calc).await;
+
+ // Creates a new server
+ let server = Server::builder("tcp://127.0.0.1:6000")
+ .expect("Create a new server builder")
+ .service(calc)
+ .build()
+ .await
+ .expect("start a new server");
// Start the server
server.start().await.unwrap();
- }));
+ });
}