Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server emit signal example #43

Closed
sbwtw opened this issue May 25, 2016 · 3 comments
Closed

server emit signal example #43

sbwtw opened this issue May 25, 2016 · 3 comments

Comments

@sbwtw
Copy link

sbwtw commented May 25, 2016

hello, is there have some examples for emit signal at server side?

@diwic
Copy link
Owner

diwic commented May 26, 2016

Hi! I added "sending a signal" to my existing server example. While I did that I realized that there are some room to make things work more ergonomic. Anyhow, here's something that works for now:

extern crate dbus;

use std::cell::RefCell;
use std::sync::Arc;
use dbus::{Connection, BusType, NameFlag};
use dbus::tree::{Factory, Signal};

fn main() {
    let c = Connection::get_private(BusType::Session).unwrap();
    c.register_name("com.example.dbustest", NameFlag::ReplaceExisting as u32).unwrap();

    let f = Factory::new_fn();
    let signal: RefCell<Option<Arc<Signal>>> = RefCell::new(None);
    let tree = f.tree().add(f.object_path("/hello").introspectable().add({
        let mut i = f.interface("com.example.dbustest").add_m(
            f.method("Hello", |m,_,_| {
                let s = format!("Hello {}!", m.sender().unwrap());
                Ok(vec!(m.method_return().append1(s), signal.borrow().as_ref().unwrap().emit(&[])))
            }).outarg::<&str,_>("reply") // One output argument, no input arguments
        );
        *signal.borrow_mut() = Some(i.add_s_ref(f.signal("HelloHappened")));
        i
    }));

    tree.set_registered(&c, true).unwrap();
    for _ in tree.run(&c, c.iter(1000)) {}
}

diwic added a commit that referenced this issue May 26, 2016
Signed-off-by: David Henningsson <diwic@ubuntu.com>
@diwic
Copy link
Owner

diwic commented May 26, 2016

...and the committed code now includes a slightly more ergonomic example (which avoids RefCell<Option<>>) which uses some just added API.

@diwic diwic closed this as completed May 26, 2016
@sbwtw
Copy link
Author

sbwtw commented May 27, 2016

thanks a lot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants