| 1 |
/* This example creates a D-Bus server with the following functionality: |
| 2 |
It registers the "com.example.dbustest" name, creates a "/hello" object path, |
| 3 |
which has an "com.example.dbustest" interface. |
| 4 |
|
| 5 |
The interface has a "Hello" method (which takes no arguments and returns a string), |
| 6 |
and a "HelloHappened" signal (with a string argument) which is sent every time |
| 7 |
someone calls the "Hello" method. |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
extern crate dbus; |
| 12 |
|
| 13 |
use std::sync::Arc; |
| 14 |
use dbus::{Connection, BusType, NameFlag}; |
| 15 |
use dbus::tree::Factory; |
| 16 |
|
| 17 |
fn main() { |
| 18 |
// Let's start by starting up a connection to the session bus and register a name. |
| 19 |
let c = Connection::get_private(BusType::Session).unwrap(); |
| 20 |
c.register_name("com.example.dbustest", NameFlag::ReplaceExisting as u32).unwrap(); |
| 21 |
|
| 22 |
// The choice of factory tells us what type of tree we want, |
| 23 |
// and if we want any extra data inside. We pick the simplest variant. |
| 24 |
let f = Factory::new_fn::<()>(); |
| 25 |
|
| 26 |
// We create the signal first, since we'll need it in both inside the method callback |
| 27 |
// and when creating the tree. |
| 28 |
let signal = Arc::new(f.signal("HelloHappened", ()).sarg::<&str,_>("sender")); |
| 29 |
let signal2 = signal.clone(); |
| 30 |
|
| 31 |
// We create a tree with one object path inside and make that path introspectable. |
| 32 |
let tree = f.tree(()).add(f.object_path("/hello", ()).introspectable().add( |
| 33 |
|
| 34 |
// We add an interface to the object path... |
| 35 |
f.interface("com.example.dbustest", ()).add_m( |
| 36 |
|
| 37 |
// ...and a method inside the interface. |
| 38 |
f.method("Hello", (), move |m| { |
| 39 |
|
| 40 |
// This is the callback that will be called when another peer on the bus calls our method. |
| 41 |
// the callback receives "MethodInfo" struct and can return either an error, or a list of |
| 42 |
// messages to send back. |
| 43 |
|
| 44 |
let name: &str = m.msg.read1()?; |
| 45 |
let s = format!("Hello {}!", name); |
| 46 |
let mret = m.msg.method_return().append1(s); |
| 47 |
|
| 48 |
let sig = signal.msg(m.path.get_name(), m.iface.get_name()) |
| 49 |
.append1(&*name); |
| 50 |
|
| 51 |
// Two messages will be returned - one is the method return (and should always be there), |
| 52 |
// and in our case we also have a signal we want to send at the same time. |
| 53 |
Ok(vec!(mret, sig)) |
| 54 |
|
| 55 |
// Our method has one output argument and one input argument. |
| 56 |
}).outarg::<&str,_>("reply") |
| 57 |
.inarg::<&str,_>("name") |
| 58 |
|
| 59 |
// We also add the signal to the interface. This is mainly for introspection. |
| 60 |
).add_s(signal2) |
| 61 |
)); |
| 62 |
|
| 63 |
// We register all object paths in the tree. |
| 64 |
tree.set_registered(&c, true).unwrap(); |
| 65 |
|
| 66 |
// We add the tree to the connection so that incoming method calls will be handled |
| 67 |
// automatically during calls to "incoming". |
| 68 |
c.add_handler(tree); |
| 69 |
|
| 70 |
// Serve other peers forever. |
| 71 |
loop { c.incoming(1000).next(); } |
| 72 |
} |