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