1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use busrt::ipc::{Client, Config};
use busrt::rpc::{DummyHandlers, Rpc, RpcClient};
/// Demo of client RPC with no handler, which just calls methods
///
/// use client_rpc_handler example to test client/server
use busrt::{empty_payload, QoS};
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Deserialize)]
struct Amount {
    value: u64,
}

#[tokio::main]
async fn main() {
    let name = "test.client.123";
    let target = "test.client.rpc";
    // create a new client instance
    let config = Config::new("/tmp/busrt.sock", name);
    let client = Client::connect(&config).await.unwrap();
    // create RPC with no handlers
    let rpc = RpcClient::new(client, DummyHandlers {});
    // call the method with no confirm
    rpc.call0(target, "test", empty_payload!(), QoS::Processed)
        .await
        .unwrap();
    let mut payload: HashMap<&str, u32> = HashMap::new();
    payload.insert("value", 10);
    // call a method with confirm to make sure the value is added
    rpc.call(
        target,
        "add",
        rmp_serde::to_vec_named(&payload).unwrap().into(),
        QoS::Processed,
    )
    .await
    .unwrap();
    // call the method to read the sum
    let result = rpc
        .call(target, "get", empty_payload!(), QoS::Processed)
        .await
        .unwrap();
    let amount: Amount = rmp_serde::from_slice(result.payload()).unwrap();
    println!("{}", amount.value);
}