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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use busrt::async_trait;
use busrt::broker::{Broker, ServerConfig, BROKER_NAME};
use busrt::client::AsyncClient;
use busrt::rpc::{Rpc, RpcClient, RpcError, RpcEvent, RpcHandlers, RpcResult};
use busrt::{Frame, QoS};
use serde::Deserialize;
use std::time::Duration;
use tokio::time::sleep;
struct MyHandlers {}
#[derive(Deserialize)]
struct PingParams<'a> {
message: Option<&'a str>,
}
#[async_trait]
impl RpcHandlers for MyHandlers {
async fn handle_call(&self, event: RpcEvent) -> RpcResult {
match event.parse_method()? {
"test" => Ok(Some("passed".as_bytes().to_vec())),
"ping" => {
let params: PingParams = rmp_serde::from_slice(event.payload())?;
Ok(params.message.map(|m| m.as_bytes().to_vec()))
}
_ => Err(RpcError::method(None)),
}
}
async fn handle_notification(&self, event: RpcEvent) {
println!(
"Got RPC notification from {}: {}",
event.sender(),
std::str::from_utf8(event.payload()).unwrap_or("something unreadable")
);
}
async fn handle_frame(&self, frame: Frame) {
println!(
"Got non-RPC frame from {}: {:?} {:?} {}",
frame.sender(),
frame.kind(),
frame.topic(),
std::str::from_utf8(frame.payload()).unwrap_or("something unreadable")
);
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut broker = Broker::new();
broker
.spawn_unix_server("/tmp/busrt.sock", ServerConfig::default())
.await?;
let mut core_client = broker.register_client(BROKER_NAME).await?;
core_client.subscribe("#", QoS::No).await?;
let handlers = MyHandlers {};
let crpc = RpcClient::new(core_client, handlers);
println!("Waiting for frames to {}", BROKER_NAME);
broker.set_core_rpc_client(crpc).await;
broker.spawn_fifo("/tmp/busrt.fifo", 8192).await?;
while broker
.core_rpc_client()
.lock()
.await
.as_ref()
.unwrap()
.is_connected()
{
sleep(Duration::from_secs(1)).await;
}
Ok(())
}