Debian Codegraph @

third_party/rust/dbus/src/signalargs.rs

raw
1 use arg;
2 use {Message, MessageType, BusName, Path, Interface, Member, MatchRule};
3
4 /// Helper methods for structs representing a Signal
5 ///
6 /// # Example
7 ///
8 /// Listen to InterfacesRemoved signal from org.bluez.obex.
9 ///
10 /// ```rust,no_run
11 /// use dbus::{Connection, BusType, SignalArgs};
12 /// use dbus::stdintf::org_freedesktop_dbus::ObjectManagerInterfacesRemoved as IR;
13 ///
14 /// let c = Connection::get_private(BusType::Session).unwrap();
15 /// // Add a match for this signal
16 /// let mstr = IR::match_str(Some(&"org.bluez.obex".into()), None);
17 /// c.add_match(&mstr).unwrap();
18 ///
19 /// // Wait for the signal to arrive.
20 /// for msg in c.incoming(1000) {
21 /// if let Some(ir) = IR::from_message(&msg) {
22 /// println!("Interfaces {:?} have been removed from bluez on path {}.", ir.interfaces, ir.object);
23 /// }
24 /// }
25 /// ```
26
27 pub trait SignalArgs: Default {
28 /// D-Bus name of signal
29 const NAME: &'static str;
30
31 /// D-Bus name of interface this signal belongs to
32 const INTERFACE: &'static str;
33
34 /// Low-level method for appending this struct to a message.
35 ///
36 /// You're more likely to use one of the more high level functions.
37 fn append(&self, i: &mut arg::IterAppend);
38
39 /// Low-level method for getting arguments from a message.
40 ///
41 /// You're more likely to use one of the more high level functions.
42 fn get(&mut self, i: &mut arg::Iter) -> Result<(), arg::TypeMismatchError>;
43
44 /// Returns a message that emits the signal.
45 fn to_emit_message(&self, path: &Path) -> Message {
46 let mut m = Message::signal(path, &Interface::from(Self::INTERFACE), &Member::from(Self::NAME));
47 self.append(&mut arg::IterAppend::new(&mut m));
48 m
49 }
50
51 /// If the message is a signal of the correct type, return its arguments, otherwise return None.
52 ///
53 /// This does not check sender and path of the message, which is likely relevant to you as well.
54 fn from_message(m: &Message) -> Option<Self> {
55 if m.msg_type() != MessageType::Signal { None }
56 else if m.interface().as_ref().map(|x| &**x) != Some(Self::INTERFACE) { None }
57 else if m.member().as_ref().map(|x| &**x) != Some(Self::NAME) { None }
58 else {
59 let mut z: Self = Default::default();
60 z.get(&mut m.iter_init()).ok().map(|_| z)
61 }
62 }
63
64 /// Returns a match rule matching this signal.
65 ///
66 /// If sender and/or path is None, matches all senders and/or paths.
67 fn match_rule<'a>(sender: Option<&'a BusName>, path: Option<&'a Path>) -> MatchRule<'a> {
68 let mut m: MatchRule = Default::default();
69 m.sender = sender.map(|x| x.clone());
70 m.path = path.map(|x| x.clone());
71 m.msg_type = Some(MessageType::Signal);
72 m.interface = Some(Self::INTERFACE.into());
73 m.member = Some(Self::NAME.into());
74 m
75 }
76
77
78 /// Returns a string that can be sent to `Connection::add_match`.
79 ///
80 /// If sender and/or path is None, matches all senders and/or paths.
81 fn match_str(sender: Option<&BusName>, path: Option<&Path>) -> String {
82 Self::match_rule(sender, path).match_str()
83 }
84 }
85
86 #[test]
87 fn intf_removed() {
88 use {Connection, BusType};
89 use stdintf::org_freedesktop_dbus::ObjectManagerInterfacesRemoved as IR;
90 let c = Connection::get_private(BusType::Session).unwrap();
91 let mstr = IR::match_str(Some(&c.unique_name().into()), Some(&"/hello".into()));
92 println!("Match str: {}", mstr);
93 c.add_match(&mstr).unwrap();
94 let ir = IR { object: "/hello".into(), interfaces: vec!("ABC.DEF".into(), "GHI.JKL".into()) };
95
96 let cp = c.with_path("dbus.dummy", "/hello", 2000);
97 cp.emit(&ir).unwrap();
98
99 for msg in c.incoming(1000) {
100 if &*msg.sender().unwrap() != &*c.unique_name() { continue; }
101 if let Some(ir2) = IR::from_message(&msg) {
102 assert_eq!(ir2.object, ir.object);
103 assert_eq!(ir2.interfaces, ir.interfaces);
104 break;
105 }
106 }
107 }