-
Notifications
You must be signed in to change notification settings - Fork 134
/
match_signal.rs
61 lines (47 loc) · 1.9 KB
/
match_signal.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/// Connects to the "server" example. Have the server example running when you're running this example.
use dbus::arg;
// First, let's autogenerate some code using the dbus-codegen-rust tool.
// With the server example running, the below was (part of) the output when running:
// `dbus-codegen-rust -d com.example.dbustest -p /hello -m None`
#[derive(Debug)]
pub struct ComExampleDbustestHelloHappened {
pub sender: String,
}
impl arg::AppendAll for ComExampleDbustestHelloHappened {
fn append(&self, i: &mut arg::IterAppend) {
arg::RefArg::append(&self.sender, i);
}
}
impl arg::ReadAll for ComExampleDbustestHelloHappened {
fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> {
Ok(ComExampleDbustestHelloHappened {
sender: i.read()?,
})
}
}
impl dbus::message::SignalArgs for ComExampleDbustestHelloHappened {
const NAME: &'static str = "HelloHappened";
const INTERFACE: &'static str = "com.example.dbustest";
}
// Autogenerated code ends here.
use dbus::blocking::Connection;
use dbus::Message;
use std::error::Error;
use std::time::Duration;
fn main() -> Result<(), Box<dyn Error>> {
// Let's start by starting up a connection to the session bus.
let c = Connection::new_session()?;
{
let proxy = c.with_proxy("com.example.dbustest", "/hello", Duration::from_millis(5000));
// Let's start listening to signals.
let _id = proxy.match_signal(|h: ComExampleDbustestHelloHappened, _: &Connection, _: &Message| {
println!("Hello happened from sender: {}", h.sender);
true
});
// Say hello to the server example, so we get a signal.
let (s,): (String,) = proxy.method_call("com.example.dbustest", "Hello", ("match_signal example",))?;
println!("{}", s);
}
// Listen to incoming signals forever.
loop { c.process(Duration::from_millis(1000))?; }
}