File: match_signal.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (61 lines) | stat: -rw-r--r-- 1,942 bytes parent folder | download | duplicates (8)
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))?; }
}