/
client.rs
47 lines (36 loc) · 1.17 KB
/
client.rs
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
use anyhow::{bail, Result};
use tokio_dbus::{Connection, MessageKind, ObjectPath};
const NAME: &str = "se.tedro.DBusExample";
const INTERFACE: &str = "se.tedro.DBusExample.Pingable";
const PATH: &ObjectPath = ObjectPath::new_const(b"/se/tedro/DBusExample");
#[tokio::main]
async fn main() -> Result<()> {
let mut c = Connection::session_bus().await?;
let (_, send, body) = c.buffers();
body.store(42u32)?;
let m = send
.method_call(PATH, "Ping")
.with_destination(NAME)
.with_interface(INTERFACE)
.with_body(body);
let serial = m.serial();
send.write_message(m)?;
let reply = loop {
c.wait().await?;
let message = c.last_message()?;
match message.kind() {
MessageKind::MethodReturn { reply_serial } if reply_serial == serial => {
break message.body().load::<u32>()?;
}
MessageKind::Error {
error_name,
reply_serial,
} if reply_serial == serial => {
bail!("{error_name}: {}", message.body().read::<str>()?)
}
_ => {}
}
};
dbg!(reply);
Ok(())
}