//! D-Bus bindings for Rust //! //! [D-Bus](http://dbus.freedesktop.org/) is a message bus, and is mainly used in Linux //! for communication between processes. It is present by default on almost every //! Linux distribution out there, and runs in two instances - one per session, and one //! system-wide. //! //! In addition to the API documentation, which you're currently reading, you might want to //! look in the examples directory, which contains many examples and an argument guide. //! README.md also contain a few quick "getting started" examples. //! //! In addition to this crate, there are two companion crates, dbus-codegen for generating Rust //! code from D-Bus introspection data, and dbus-tokio for integrating D-Bus with [Tokio](http://tokio.rs). //! However, at the time of this writing, these are far less mature than this crate.
#![warn(missing_docs)]
externcrate libc;
pubuse ffi::DBusBusType as BusType; pubuse connection::DBusNameFlag as NameFlag; pubuse ffi::DBusRequestNameReply as RequestNameReply; pubuse ffi::DBusReleaseNameReply as ReleaseNameReply; pubuse ffi::DBusMessageType as MessageType;
// Note! For this Sync impl to be safe, it requires that no functions that take &self, // actually calls into FFI. All functions that call into FFI with a ffi::DBusError // must take &mut self.
/// Create a new custom D-Bus Error. pubfn new_custom(name: &str, message: &str) -> Error { let n = to_c_str(name); let m = to_c_str(&message.replace("%","%%")); letmut e = Error::empty();
unsafe { ffi::dbus_set_error(e.get_mut(), n.as_ptr(), m.as_ptr()) };
e
}
#[cfg(test)] mod test { usesuper::{Connection, Message, BusType, MessageItem, ConnectionItem, NameFlag,
RequestNameReply, ReleaseNameReply};
#[test] fn connection() { let c = Connection::get_private(BusType::Session).unwrap(); let n = c.unique_name();
assert!(n.starts_with(":1."));
println!("Connected to DBus, unique name: {}", n);
}
#[test] fn invalid_message() { let c = Connection::get_private(BusType::Session).unwrap(); let m = Message::new_method_call("foo.bar", "/", "foo.bar", "FooBar").unwrap(); let e = c.send_with_reply_and_block(m, 2000).err().unwrap();
assert!(e.name().unwrap() == "org.freedesktop.DBus.Error.ServiceUnknown");
}
#[test] fn message_listnames() { let c = Connection::get_private(BusType::Session).unwrap(); let m = Message::method_call(&"org.freedesktop.DBus".into(), &<span style='color:blue'>"/".into(),
&"org.freedesktop.DBus".into(), &"ListNames".into()); let r = c.send_with_reply_and_block(m, 2000).unwrap(); let reply = r.get_items();
println!("{:?}", reply);
}
#[test] fn message_namehasowner() { let c = Connection::get_private(BusType::Session).unwrap(); letmut m = Message::new_method_call("org.freedesktop.DBus", "/", "org.freedesktop.DBus", "NameHasOwner").unwrap();
m.append_items(&[MessageItem::Str("org.freedesktop.DBus".to_string())]); let r = c.send_with_reply_and_block(m, 2000).unwrap(); let reply = r.get_items();
println!("{:?}", reply);
assert_eq!(reply, vec!(MessageItem::Bool(true)));
}
#[test] fn object_path() { use std::sync::mpsc; let (tx, rx) = mpsc::channel(); let thread = ::std::thread::spawn(move || { let c = Connection::get_private(BusType::Session).unwrap();
c.register_object_path("/hello").unwrap(); // println!("Waiting...");
tx.send(c.unique_name()).unwrap(); for n in c.iter(1000) { // println!("Found message... ({})", n); match n {
ConnectionItem::MethodCall(ref m) => { let reply = Message::new_method_return(m).unwrap();
c.send(reply).unwrap(); break;
}
_ => {}
}
}
c.unregister_object_path("/hello");
});
let c = Connection::get_private(BusType::Session).unwrap(); let n = rx.recv().unwrap(); let m = Message::new_method_call(&n, "/hello", "com.example.hello", "Hello").unwrap();
println!("Sending..."); let r = c.send_with_reply_and_block(m, 8000).unwrap(); let reply = r.get_items();
println!("{:?}", reply);
thread.join().unwrap();
}
#[test] fn register_name() { let c = Connection::get_private(BusType::Session).unwrap(); let n = format!("com.example.hello.test.register_name");
assert_eq!(c.register_name(&n, NameFlag::ReplaceExisting as u32).unwrap(), RequestNameReply::PrimaryOwner);
assert_eq!(c.release_name(&n).unwrap(), ReleaseNameReply::Released);
}
#[test] fn signal() { let c = Connection::get_private(BusType::Session).unwrap(); let iface = "com.example.signaltest"; let mstr = format!("interface='{}',member='ThisIsASignal'", iface);
c.add_match(&mstr).unwrap(); let m = Message::new_signal("/mysignal", iface, "ThisIsASignal").unwrap(); let uname = c.unique_name();
c.send(m).unwrap(); for n in c.iter(1000) { match n {
ConnectionItem::Signal(s) => { let (_, p, i, m) = s.headers(); match (&*p.unwrap(), &*i.unwrap(), &*m.unwrap()) {
("/mysignal", "com.example.signaltest", "ThisIsASignal") => {
assert_eq!(&*s.sender().unwrap(), &*uname); break;
},
(_, _, _) => println!("Other signal: {:?}", s.headers()),
}
}
_ => {},
}
}
c.remove_match(&mstr).unwrap();
}
#[test] fn watch() { let c = Connection::get_private(BusType::Session).unwrap(); let d = c.watch_fds();
assert!(d.len() > 0);
println!("Fds to watch: {:?}", d);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.