Code Monkey home page Code Monkey logo

dbus-rs's Introduction

A DBus binding for rust.

Current state: WIP, but these things should be up and working:

  • Connect to system or session bus
  • Method calls send/receive
  • Signals send/receive
  • Properties, on both client and server sides (set/get/getall methods, but no signals)
  • Server side introspection
  • Server side method dispatch (boxed closures) and property get/set dispatch (trait objects)

API Documentation

Examples

Client

This example opens a connection to the session bus and asks for a list of all names currently present.

let c = Connection::get_private(BusType::Session).unwrap();
let m = Message::new_method_call("org.freedesktop.DBus", "/", "org.freedesktop.DBus", "ListNames").unwrap();
let r = c.send_with_reply_and_block(m, 2000).unwrap();
let reply = r.get_items();
println!("{}", reply);

You can try a similar example by running:

cargo run --example client

Server

This example grabs the com.example.test bus name, registers the /hello path and adds a method which returns a string. It then listens for incoming D-Bus events and handles them accordingly.

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

let mut o = ObjectPath::new(&c, "/hello", true);
o.insert_interface("com.example.test", Interface::new(
    vec!(Method::new("Hello", vec!(),
        vec!(Argument::new("reply", "s")),
        Box::new(|msg| Ok(vec!(MessageItem::Str(format!("Hello {}!", msg.sender().unwrap())))))
    )),
    vec!(), vec!()
));
o.set_registered(true).unwrap();

for n in c.iter(1000) {
    match n {
        ConnectionItem::MethodCall(mut m) => {
            o.handle_message(&mut m);
        },
        _ => {},
    }
}

You can try a similar example by running:

cargo run --example server

Properties

This example gets the current version of the Policykit backend.

let c = Connection::get_private(BusType::System).unwrap();
let p = Props::new(&c, "org.freedesktop.PolicyKit1", "/org/freedesktop/PolicyKit1/Authority",
    "org.freedesktop.PolicyKit1.Authority", 10000);
let v = p.get("BackendVersion").unwrap();

You can try a this example by running:

cargo run --example properties

For an extended example, which also uses non-panicing error handling, see

examples/rtkit.rs

License

Apache / MIT dual licensed.

dbus-rs's People

Contributors

diwic avatar dradtke avatar hoodie avatar hugoduncan avatar infinityb avatar kylc avatar manuels avatar ryansname avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.