Skip to content

Instantly share code, notes, and snippets.

@mohan43u
Created December 13, 2014 04:18
Show Gist options
  • Save mohan43u/82a7792861e4d6cfae5b to your computer and use it in GitHub Desktop.
Save mohan43u/82a7792861e4d6cfae5b to your computer and use it in GitHub Desktop.
#!/usr/bin/env vala
[DBus (name = "org.testdbus.Server")]
interface TestDBus.Client : GLib.Object {
public signal void finalized();
public abstract string say(string sentence) throws Error;
public abstract void quit() throws Error;
}
[DBus (name = "org.testdbus.Server")]
class TestDBus.Server : GLib.Object {
static GLib.MainLoop mainloop = null;
static const string name = "org.testdbus.Server";
static const string path = "/org/testdbus/server";
static TestDBus.Server instance = null;
static GLib.DBusConnection con = null;
static uint nameid = 0;
static uint objectid = 0;
public signal void finalized();
private static void name_acquired_cb(GLib.DBusConnection con, string name) {
TestDBus.Server.con = con;
TestDBus.Server.instance = new TestDBus.Server();
TestDBus.Server.objectid = con.register_object(TestDBus.Server.path,
TestDBus.Server.instance);
}
private static void acquire_name() {
TestDBus.Server.nameid = GLib.Bus.own_name(GLib.BusType.SESSION,
TestDBus.Server.name,
GLib.BusNameOwnerFlags.NONE,
TestDBus.Server.name_acquired_cb,
null,
null);
}
public static void init(GLib.MainLoop mainloop) {
if(TestDBus.Server.instance == null) {
TestDBus.Server.mainloop = mainloop;
TestDBus.Server.acquire_name();
}
}
public static TestDBus.Client get_new_client() {
TestDBus.Client client = GLib.Bus.get_proxy_sync(GLib.BusType.SESSION,
TestDBus.Server.name,
TestDBus.Server.path);
return client;
}
public static void startserver() {
GLib.MainLoop mainloop = new GLib.MainLoop();
TestDBus.Server.init(mainloop);
mainloop.run();
}
public static void startclient() {
GLib.MainLoop mainloop = new GLib.MainLoop();
TestDBus.Client client = TestDBus.Server.get_new_client();
client.finalized.connect(() => {mainloop.quit();});
print(client.say("helloworld") + "\n");
client.quit();
mainloop.run();
}
public static int main(string[] args) {
if(args[1] == "server") {
startserver();
}
if(args[1] == "client") {
startclient();
}
return 0;
}
public void quit() {
this.finalized();
TestDBus.Server.con.unregister_object(TestDBus.Server.objectid);
GLib.Bus.unown_name(TestDBus.Server.nameid);
TestDBus.Server.instance = null;
TestDBus.Server.mainloop.quit();
}
public string say(string sentence) {
return sentence;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment