extern crate dbus;
extern crate futures;
extern crate tokio_timer;
extern crate dbus_tokio;
extern crate tokio;
use std::time::Duration;
use std::sync::Arc;
use std::rc::Rc;
use dbus::{Connection, BusType, NameFlag};
use dbus::tree::MethodErr;
use dbus_tokio::tree::{AFactory, ATree, ATreeServer};
use dbus_tokio::AConnection;
use tokio::reactor::Handle;
use tokio::runtime::current_thread::Runtime;
use futures::{Future, Stream};
fn main() {
let c = Rc::new(Connection::get_private(BusType::Session).unwrap());
c.register_name("com.example.dbustest", NameFlag::ReplaceExisting as u32).unwrap();
let f = AFactory::new_afn::<()>();
let signal = Arc::new(f.signal("HelloHappened", ()).sarg::<&str,_>("sender"));
let signal2 = signal.clone();
let tree = f.tree(ATree::new()).add(f.object_path("/hello", ()).introspectable().add(
f.interface("com.example.dbustest", ()).add_m(
f.amethod("Hello", (), move |m| {
let t: u32 = m.msg.read1().unwrap();
let sleep_future = tokio_timer::sleep(Duration::from_millis(t as u64));
let sender = m.msg.sender().unwrap().into_static();
let (pname, iname) = (m.path.get_name().clone(), m.iface.get_name().clone());
let mret = m.msg.method_return();
let signal3 = signal.clone();
sleep_future.and_then(move |_| {
let s = format!("Hello {}!", sender);
let mret = mret.append1(s);
let sig = signal3.msg(&pname, &iname).append1(&*sender);
Ok(vec!(mret, sig))
}).map_err(|e| MethodErr::failed(&e))
}).inarg::<u32,_>("sleep_millis")
.outarg::<&str,_>("reply")
).add_s(signal2)
));
tree.set_registered(&c, true).unwrap();
let mut rt = Runtime::new().unwrap();
let aconn = AConnection::new(c.clone(), Handle::current(), &mut rt).unwrap();
let server = ATreeServer::new(c.clone(), &tree, aconn.messages().unwrap());
let server = server.for_each(|m| { println!("Unhandled message: {:?}", m); Ok(()) });
rt.block_on(server).unwrap();
rt.run().unwrap();
}