[seed] DBus: Add support for synchronous calls



commit 8c8d7f54ee37f8bdfef49f6f8bf20b59e01d8057
Author: Robert Carr <racarr svn gnome org>
Date:   Tue May 12 03:42:10 2009 -0400

    DBus: Add support for synchronous calls
---
 examples/dbus-consolekit.js |    8 +------
 modules/dbus/dbus.js        |   51 +++++++++++++++++++++++++++++++++++++++++++
 modules/dbus/module.c       |   10 +++++---
 3 files changed, 58 insertions(+), 11 deletions(-)

diff --git a/examples/dbus-consolekit.js b/examples/dbus-consolekit.js
index 2b08606..3cf2cf2 100755
--- a/examples/dbus-consolekit.js
+++ b/examples/dbus-consolekit.js
@@ -44,13 +44,7 @@ manager = new ConsoleKitManager();
 manager.GetCurrentSessionRemote(
     function(result, exception){
 	session = new ConsoleKitSession(result);
-	session.IsLocalRemote (function (result){
-	    if (result)
-		Seed.print ("Session is local");
-	    else
-		Seed.print ("Session is remote");
-	    Seed.quit();
-	});
+	Seed.print (session.IsLocalRemoteSync());
     });
 
 mainloop = GLib.main_loop_new();
diff --git a/modules/dbus/dbus.js b/modules/dbus/dbus.js
index 52f8433..9cacfd6 100644
--- a/modules/dbus/dbus.js
+++ b/modules/dbus/dbus.js
@@ -94,6 +94,38 @@ function _proxyInvoker(obj, ifaceName, methodName, outSignature, inSignature, ti
                             replyFunc);
 }
 
+function _proxyInvokerSync(obj, ifaceName, methodName, outSignature, inSignature, timeout, arg_array) {
+    if (ifaceName == null)
+        ifaceName = obj._dbusInterface;
+
+    /* Convert arg_array to a *real* array */
+    arg_array = Array.prototype.slice.call(arg_array);
+
+    var expectedNumberArgs = this.signatureLength(inSignature);
+
+    if (arg_array.length < expectedNumberArgs) {
+        throw new Error("Not enough arguments passed for method: " + methodName +
+                       ". Expected " + expectedNumberArgs + ", got " + arg_array.length);
+    } else if (arg_array.length > expectedNumberArgs) {
+        throw new Error("Too many arguments passed for method: " + methodName +
+                       ". Maximum is " + expectedNumberArgs);
+    }
+
+    /* Auto-start on method calls is too unpredictable; in particular if
+     * something crashes we want to cleanly restart it, not have it
+     * come back next time someone tries to use it.
+     */
+    return obj._dbusBus.call(obj._dbusBusName,
+			     obj._dbusPath,
+			     ifaceName,
+			     methodName,
+			     outSignature,
+			     inSignature,
+			     NO_START_IF_NOT_FOUND,
+			     arg_array);
+
+}
+
 function _logReply(result, exc) {
     if (result != null) {
         log("Ignored reply to dbus method: " + result.toSource());
@@ -121,6 +153,24 @@ function _makeProxyMethod(member) {
     };
 }
 
+function _makeProxyMethodSync(member) {
+    return function() {
+        /* JSON methods are the default */
+        if (!("outSignature" in member))
+            member.outSignature = "a{sv}";
+
+        if (!("inSignature" in member))
+            member.inSignature = "a{sv}";
+
+        if (!("timeout" in member))
+            member.timeout = -1;
+
+        return _proxyInvokerSync(this, null, member.name,
+				 member.outSignature, member.inSignature,
+				 member.timeout, arguments);
+    };
+}
+
 // stub we insert in all proxy prototypes
 function _getBusName() {
     return this._dbusBusName;
@@ -240,6 +290,7 @@ function proxifyPrototype(proto, iface) {
              */
             var methodName = method.name + "Remote";
             proto[methodName] = _makeProxyMethod(method);
+	    proto[methodName+"Sync"] = _makeProxyMethodSync(method);
             proto[method.name] = function() {
                 log("PROXY-ERROR: " + method.name + " called, you should be using " +
                     methodName + " instead");
diff --git a/modules/dbus/module.c b/modules/dbus/module.c
index fac6a7b..4d18ae0 100644
--- a/modules/dbus/module.c
+++ b/modules/dbus/module.c
@@ -67,9 +67,11 @@ bus_check (SeedContext ctx, DBusBusType bus_type, SeedException * exception)
 static DBusMessage *
 prepare_call (SeedContext ctx,
 	      SeedObject obj,
+	      SeedObject arg_array,
 	      guint argc,
 	      const SeedValue * argv,
-	      DBusBusType bus_type, SeedException * exception)
+	      DBusBusType bus_type, 
+	      SeedException * exception)
 {
   DBusMessage *message;
   const char *bus_name;
@@ -141,7 +143,7 @@ prepare_call (SeedContext ctx,
     dbus_signature_iter_init (&sig_iter, "a{sv}");
 
   if (!seed_js_values_to_dbus
-      (ctx, 0, argv[8], &arg_iter, &sig_iter, exception))
+      (ctx, 0, arg_array, &arg_iter, &sig_iter, exception))
     {
       //  big_debug(BIG_DEBUG_JS_DBUS, "Failed to marshal call from JS to dbus");
       dbus_message_unref (message);
@@ -320,7 +322,7 @@ seed_js_dbus_call_async (SeedContext ctx,
   bus_type = get_bus_type_from_object (ctx, this_object, exception);
 
   message =
-    prepare_call (ctx, this_object, argument_count, arguments, bus_type,
+	  prepare_call (ctx, this_object, arguments[8], argument_count, arguments, bus_type,
 		  exception);
 
   if (message == NULL)
@@ -849,7 +851,7 @@ seed_js_dbus_call(SeedContext ctx,
 
     bus_type = get_bus_type_from_object (ctx, this_object, exception);
 
-    message = prepare_call(ctx, this_object, argument_count, arguments, bus_type, exception);
+    message = prepare_call(ctx, this_object, arguments[7], argument_count, arguments, bus_type, exception);
 
     bus_connection = DBUS_CONNECTION_FROM_TYPE(bus_type);
 



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