[gnome-chess/chess-telepathy-networking-support-664946-rebase: 61/64] Add test for DBus RMI



commit e6dabc9b0217dfd53cbc4f8d4e5e806e00673078
Author: Chandni Verma <chandniverma2112 gmail com>
Date:   Wed Dec 12 02:00:06 2012 +0530

    Add test for DBus RMI

 tests/Makefile.am                 |   28 ++++++++++++++++-
 tests/test-demo-gdbus-client.vala |   44 +++++++++++++++++++++++++++
 tests/test-demo-gdbus-server.vala |   60 +++++++++++++++++++++++++++++++++++++
 3 files changed, 131 insertions(+), 1 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index aac91af..0f3f4ff 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -9,11 +9,33 @@ LDADD = \
 	$(top_builddir)/libgames-contacts/libgames-contacts.la \
 	$(GNOME_CHESS_LIBS)
 
+test_demo_gdbus_server_CFLAGS = \
+	$(GIO_CFLAGS)
+
+test_demo_gdbus_server_LDADD = \
+	$(GIO_LIBS)
+
+test_demo_gdbus_client_CFLAGS = \
+	$(GIO_CFLAGS)
+
+test_demo_gdbus_client_LDADD = \
+	$(GIO_LIBS)
+
+test_demo_gdbus_server_VALAFLAGS = \
+	--pkg gio-2.0
+
+test_demo_gdbus_client_VALAFLAGS = \
+	--pkg gio-2.0
+
 networking_tests = \
 	test-fetch-contacts
 
-noinst_PROGRAMS =
+vala_tests = \
+	test-demo-gdbus-client \
+	test-demo-gdbus-server
 
+noinst_PROGRAMS = \
+	$(vala_tests)
 
 if ENABLE_NETWORKING
 noinst_PROGRAMS += \
@@ -28,3 +50,7 @@ LDADD += \
 endif #ENABLE_NETWORKING
 
 test_fetch_contacts_SOURCES = test-fetch-contacts.c
+
+test_demo_gdbus_server_SOURCES = test-demo-gdbus-server.vala
+test_demo_gdbus_client_SOURCES = test-demo-gdbus-client.vala
+
diff --git a/tests/test-demo-gdbus-client.vala b/tests/test-demo-gdbus-client.vala
new file mode 100644
index 0000000..354806f
--- /dev/null
+++ b/tests/test-demo-gdbus-client.vala
@@ -0,0 +1,44 @@
+/* A simple test client to show how to do RMI over DBus */
+
+[DBus (name = "org.example.Demo")]
+interface Demo : Object {
+    public abstract int ping (string msg) throws IOError;
+    public abstract int ping_with_sender (string msg) throws IOError;
+    public abstract int ping_with_signal (string msg) throws IOError;
+    public signal void pong (int count, string msg);
+}
+
+void main () {
+    /* Needed only if your client is listening to signals; you can omit it
+ * otherwise */
+    var loop = new MainLoop();
+
+    /* Important: keep demo variable out of try/catch scope not lose signals! */
+    Demo demo = null;
+
+    try {
+        demo = Bus.get_proxy_sync (BusType.SESSION, "org.example.Demo",
+                                                    "/org/example/demo");
+
+        /* Connecting to signal pong! */
+        demo.pong.connect((c, m) => {
+            stdout.printf ("Got pong %d for msg '%s'\n", c, m);
+            loop.quit ();
+        });
+
+        int pong = demo.ping ("Hello from Vala");
+        stdout.printf ("%d\n", pong);
+
+        pong = demo.ping_with_sender ("Hello from Vala with sender");
+        stdout.printf ("%d\n", pong);
+
+        pong = demo.ping_with_signal ("Hello from Vala with signal");
+        stdout.printf ("%d\n", pong);
+
+    } catch (IOError e) {
+        stderr.printf ("%s\n", e.message);
+    }
+    loop.run();
+}
+
+/* $ valac --pkg gio-2.0 gdbus-demo-client.vala */
diff --git a/tests/test-demo-gdbus-server.vala b/tests/test-demo-gdbus-server.vala
new file mode 100644
index 0000000..9034177
--- /dev/null
+++ b/tests/test-demo-gdbus-server.vala
@@ -0,0 +1,60 @@
+/* A simple demo server to show performing RMI over DBus */
+
+/* Note: this attribute specifies the _interface_ name.  It
+ * is called 'name =' for historical reasons.
+ */
+[DBus (name = "org.example.Demo")]
+public class DemoServer : Object {
+
+    private int counter;
+
+    public int ping (string msg) {
+        stdout.printf ("%s\n", msg);
+        return counter++;
+    }
+
+    public int ping_with_signal (string msg) {
+        stdout.printf ("%s\n", msg);
+        pong(counter, msg);
+        return counter++;
+    }
+
+    /* Including any parameter of type GLib.BusName won't be added to the
+       interface and will return the dbus sender name (who is calling the
+method) */
+    public int ping_with_sender (string msg, GLib.BusName sender) {
+        stdout.printf ("%s, from: %s\n", msg, sender);
+        return counter++;
+    }
+
+    public void ping_error () throws Error {
+        throw new DemoError.SOME_ERROR ("There was an error!");
+    }
+
+    public signal void pong (int count, string msg);
+}
+
+[DBus (name = "org.example.DemoError")]
+public errordomain DemoError
+{
+    SOME_ERROR
+}
+
+void on_bus_aquired (DBusConnection conn) {
+    try {
+        conn.register_object ("/org/example/demo", new DemoServer ());
+    } catch (IOError e) {
+        stderr.printf ("Could not register service\n");
+    }
+}
+
+void main () {
+    Bus.own_name (BusType.SESSION, "org.example.Demo", BusNameOwnerFlags.NONE,
+                  on_bus_aquired,
+                  () => {},
+                  () => stderr.printf ("Could not aquire name\n"));
+
+    new MainLoop ().run ();
+}
+
+/* $ valac --pkg gio-2.0 gdbus-demo-server.vala */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]