diff options
Diffstat (limited to 'jsonrpc/examples')
-rw-r--r-- | jsonrpc/examples/client.py | 59 | ||||
-rw-r--r-- | jsonrpc/examples/client.rs | 34 | ||||
-rw-r--r-- | jsonrpc/examples/server.rs | 59 |
3 files changed, 152 insertions, 0 deletions
diff --git a/jsonrpc/examples/client.py b/jsonrpc/examples/client.py new file mode 100644 index 0000000..14b3cf9 --- /dev/null +++ b/jsonrpc/examples/client.py @@ -0,0 +1,59 @@ +import socket +import random +import json + +HOST = "127.0.0.1" +PORT = 60000 + +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.connect((HOST, PORT)) + +req = { + "jsonrpc": "2.0", + "id": str(random.randint(0, 1000)), + "method": "Calc.add", + "params": {"x": 4, "y": 3}, +} +print("Send: ", req) +s.sendall(json.dumps(req).encode()) +res = s.recv(1024) +res = json.loads(res) +print("Received: ", res) + +req = { + "jsonrpc": "2.0", + "id": str(random.randint(0, 1000)), + "method": "Calc.sub", + "params": {"x": 4, "y": 3}, +} +print("Send: ", req) +s.sendall(json.dumps(req).encode()) +res = s.recv(1024) +res = json.loads(res) +print("Received: ", res) + +req = { + "jsonrpc": "2.0", + "id": str(random.randint(0, 1000)), + "method": "Calc.ping", + "params": None, +} +print("Send: ", req) +s.sendall(json.dumps(req).encode()) +res = s.recv(1024) +res = json.loads(res) +print("Received: ", res) + +req = { + "jsonrpc": "2.0", + "id": str(random.randint(0, 1000)), + "method": "Calc.version", + "params": None, +} +print("Send: ", req) +s.sendall(json.dumps(req).encode()) +res = s.recv(1024) +res = json.loads(res) +print("Received: ", res) + +s.close() diff --git a/jsonrpc/examples/client.rs b/jsonrpc/examples/client.rs new file mode 100644 index 0000000..0063098 --- /dev/null +++ b/jsonrpc/examples/client.rs @@ -0,0 +1,34 @@ +use serde::{Deserialize, Serialize}; + +use karyons_jsonrpc::Client; + +#[derive(Deserialize, Serialize)] +struct Req { + x: u32, + y: u32, +} + +#[derive(Deserialize, Serialize, Debug)] +struct Pong {} + +fn main() { + env_logger::init(); + smol::future::block_on(async { + let endpoint = "tcp://127.0.0.1:60000".parse().unwrap(); + let client = Client::new_with_endpoint(&endpoint, None).await.unwrap(); + + let params = Req { x: 10, y: 7 }; + let result: u32 = client.call("Calc.add", params).await.unwrap(); + println!("result {result}"); + + let params = Req { x: 10, y: 7 }; + let result: u32 = client.call("Calc.sub", params).await.unwrap(); + println!("result {result}"); + + let result: Pong = client.call("Calc.ping", ()).await.unwrap(); + println!("result {:?}", result); + + let result: String = client.call("Calc.version", ()).await.unwrap(); + println!("result {result}"); + }); +} diff --git a/jsonrpc/examples/server.rs b/jsonrpc/examples/server.rs new file mode 100644 index 0000000..367bfe9 --- /dev/null +++ b/jsonrpc/examples/server.rs @@ -0,0 +1,59 @@ +use std::sync::Arc; + +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +use karyons_jsonrpc::{register_service, JsonRPCError, Server}; + +struct Calc { + version: String, +} + +#[derive(Deserialize, Serialize)] +struct Req { + x: u32, + y: u32, +} + +#[derive(Deserialize, Serialize)] +struct Pong {} + +impl Calc { + async fn ping(&self, _params: Value) -> Result<Value, JsonRPCError> { + Ok(serde_json::json!(Pong {})) + } + + async fn add(&self, params: Value) -> Result<Value, JsonRPCError> { + 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> { + 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> { + 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 endpoint = "tcp://127.0.0.1:60000".parse().unwrap(); + let server = Server::new_with_endpoint(&endpoint, ex).await.unwrap(); + + // Register the Calc service + register_service!(Calc, ping, add, sub, version); + let calc = Calc { + version: String::from("0.1"), + }; + server.attach_service(calc).await; + + // Start the server + server.start().await.unwrap(); + })); +} |