client_rpc/
client_rpc.rs

1// Demo of client RPC with no handler which just calls methods
2//
3// use client_rpc_handler example to test client/server
4use busrt::ipc::{Client, Config};
5use busrt::rpc::{Rpc, RpcClient};
6use busrt::{empty_payload, QoS};
7use serde::Deserialize;
8use std::collections::BTreeMap;
9
10#[derive(Deserialize)]
11struct Amount {
12    value: u64,
13}
14
15#[tokio::main]
16async fn main() -> Result<(), Box<dyn std::error::Error>> {
17    let name = "test.client.123";
18    let target = "test.client.rpc";
19    // create a new client instance
20    let config = Config::new("/tmp/busrt.sock", name);
21    let client = Client::connect(&config).await?;
22    // create RPC with no handlers
23    let rpc = RpcClient::new0(client);
24    // call the method with no confirm
25    rpc.call0(target, "test", empty_payload!(), QoS::Processed)
26        .await?;
27    let mut payload: BTreeMap<&str, u32> = <_>::default();
28    payload.insert("value", 10);
29    // call a method with confirm to make sure the value is added
30    rpc.call(
31        target,
32        "add",
33        rmp_serde::to_vec_named(&payload)?.into(),
34        QoS::Processed,
35    )
36    .await?;
37    // call the method to read the sum
38    let result = rpc
39        .call(target, "get", empty_payload!(), QoS::Processed)
40        .await?;
41    let amount: Amount = rmp_serde::from_slice(result.payload())?;
42    println!("{}", amount.value);
43    Ok(())
44}