From acfa130e927c2522fb17cad415ab009fef0914b3 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Thu, 28 Mar 2013 17:50:16 +0400 Subject: [PATCH] dbus example added --- vala/live.gnome.org_tutorial/dbus-client.sh | 12 +++++ .../dbus-demo-service.vala | 54 +++++++++++++++++++ vala/live.gnome.org_tutorial/run.sh | 4 +- 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100755 vala/live.gnome.org_tutorial/dbus-client.sh create mode 100644 vala/live.gnome.org_tutorial/dbus-demo-service.vala diff --git a/vala/live.gnome.org_tutorial/dbus-client.sh b/vala/live.gnome.org_tutorial/dbus-client.sh new file mode 100755 index 0000000..3983bab --- /dev/null +++ b/vala/live.gnome.org_tutorial/dbus-client.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +dbus-send --type=method_call \ + --dest=org.example.DemoService \ + /org/example/demo \ + org.example.DemoService.SomeMethod + +dbus-send --type=method_call \ + --dest=org.example.DemoService \ + /org/example/demo \ + org.example.DemoService.SomeMethodSender \ + string:'hello world' diff --git a/vala/live.gnome.org_tutorial/dbus-demo-service.vala b/vala/live.gnome.org_tutorial/dbus-demo-service.vala new file mode 100644 index 0000000..ad9a8c8 --- /dev/null +++ b/vala/live.gnome.org_tutorial/dbus-demo-service.vala @@ -0,0 +1,54 @@ +[DBus(name = "org.example.DemoService")] +public class DemoService : Object { + /* Private field, not exported via D-Bus */ + int counter; + + /* Public field, not exported via D-Bus */ + public int status; + + /* Public property, exported via D-Bus */ + public int something { get; set; } + + /* Public signal, exported via D-Bus + * Can be emitted on the server side and can be connected to on the client side. + */ + public signal void sig1(); + + /* Public method, exported via D-Bus */ + public void some_method() { + counter++; + stdout.printf("heureka! counter = %d\n", counter); + sig1(); // emit signal + } + + /* Public method, exported via D-Bus and showing the sender who is + is calling the method (not exported in the D-Bus inteface) */ + public void some_method_sender(string message, GLib.BusName sender) { + counter++; + stdout.printf("heureka! counter = %d, '%s' message from sender %s\n", + counter, message, sender); + } +} + +void on_bus_aquired (DBusConnection conn) { + try { + // start service and register it as dbus object + var service = new DemoService(); + conn.register_object ("/org/example/demo", service); + } catch (IOError e) { + stderr.printf ("Could not register service: %s\n", e.message); + } +} + +void main () { + // try to register service name in session bus + Bus.own_name (BusType.SESSION, "org.example.DemoService", /* name to register */ + BusNameOwnerFlags.NONE, /* flags */ + on_bus_aquired, /* callback function on registration succeded */ + () => {}, /* callback on name register succeded */ + () => stderr.printf ("Could not aquire name\n")); + /* callback on name lost */ + + // start main loop + new MainLoop ().run (); +} diff --git a/vala/live.gnome.org_tutorial/run.sh b/vala/live.gnome.org_tutorial/run.sh index bc59972..df7921b 100755 --- a/vala/live.gnome.org_tutorial/run.sh +++ b/vala/live.gnome.org_tutorial/run.sh @@ -1,10 +1,8 @@ #!/bin/sh -# valac --thread --target-glib=2.32 threading-sample.vala \ valac --thread threads.vala valac --thread --target-glib=2.32 threads-2.32.vala valac --thread philosophers.vala valac --thread async-queue-test.vala - - +valac --pkg gio-2.0 dbus-demo-service.vala