[at-spi2-atk/p2p: 5/5] Peer-to-peer fixes; work in progress



commit c3917234bd94cf1ed37369c9392c1031d0bbcdaa
Author: Mike Gorse <mgorse novell com>
Date:   Mon Aug 30 19:07:01 2010 -0400

    Peer-to-peer fixes; work in progress

 atk-adaptor/bridge.c |   74 ++++++++++++++++++++-----------------------
 atk-adaptor/bridge.h |    3 ++
 atk-adaptor/event.c  |   35 +++++++++++---------
 droute/droute.c      |   86 +++++++++++++++++++++++++++++++++++++-------------
 droute/droute.h      |   11 +++++--
 5 files changed, 128 insertions(+), 81 deletions(-)
---
diff --git a/atk-adaptor/bridge.c b/atk-adaptor/bridge.c
index 86dcc82..5b697a0 100644
--- a/atk-adaptor/bridge.c
+++ b/atk-adaptor/bridge.c
@@ -175,23 +175,6 @@ set_reply (DBusPendingCall *pending, void *user_data)
     *replyptr = dbus_pending_call_steal_reply (pending);
 }
 
-static DBusMessage *
-send_and_allow_reentry (DBusConnection *bus, DBusMessage *message, DBusError *error)
-{
-    DBusPendingCall *pending;
-    DBusMessage *reply = NULL;
-
-    if (!dbus_connection_send_with_reply (bus, message, &pending, -1))
-    {
-        return NULL;
-    }
-    dbus_pending_call_set_notify (pending, set_reply, (void *)&reply, NULL);
-    while (!reply)
-    {
-      if (!dbus_connection_read_write_dispatch (bus, -1)) return NULL;
-    }
-    return reply;
-}
 /*---------------------------------------------------------------------------*/
 
 static void
@@ -273,7 +256,6 @@ register_reply (DBusPendingCall *pending, void *user_data)
       if (strcmp (dbus_message_get_signature (reply), "(so)") != 0)
         {
           g_warning ("AT-SPI: Could not obtain desktop path or name\n");
-printf("sig: %s\n", dbus_message_get_signature(reply));
         }
       else
         {
@@ -305,6 +287,7 @@ register_application (SpiBridge * app)
   DBusMessageIter iter;
   DBusError error;
   DBusPendingCall *pending;
+  const int max_addr_length = 128; /* should be long enough */
 
   dbus_error_init (&error);
 
@@ -337,12 +320,7 @@ mkdir("/tmp/at-spi2/", S_IRWXU);
 app->app_bus_addr = g_malloc(max_addr_length * sizeof(char));
 sprintf(app->app_bus_addr, "unix:path=/tmp/at-spi2/socket-%d-%d", getpid(),
 rand());
-    }
-  else
-    {
-      g_warning ("AT-SPI: Could not embed inside desktop: %s\n", error.message);
-      return FALSE;
-    }
+
   return TRUE;
 }
 
@@ -520,27 +498,39 @@ install_plug_hooks ()
 }
 
 static void
-new_connection_cb(DBusServer *server, DBusConnection *con, void *data)
+new_connection_cb (DBusServer *server, DBusConnection *con, void *data)
 {
-dbus_connection_ref(con);
-dbus_connection_setup_with_g_main(con, NULL);
+  GList *new_list;
+
+  dbus_connection_ref(con);
+  dbus_connection_setup_with_g_main(con, NULL);
+  droute_intercept_dbus (con);
+  droute_context_register (spi_global_app_data->droute, con);
+
+  new_list = g_list_append (spi_global_app_data->direct_connections, con);
+  if (new_list)
+    spi_global_app_data->direct_connections = new_list;
 }
 
-static int setup_bus(void)
+static int
+setup_bus (void)
 {
-DBusServer *server;
-DBusError err;
+  DBusServer *server;
+  DBusError err;
+
+  dbus_error_init(&err);
+  server = dbus_server_listen(spi_global_app_data->app_bus_addr, &err);
 
-dbus_error_init(&err);
-	server = dbus_server_listen(spi_global_app_data->app_bus_addr, &err);
+  /* is there a better way to handle this */
+  if (server == NULL)
+    return -1;
 
-/* is there a better way to handle this */
-if(server == NULL) return -1;
+  dbus_server_setup_with_g_main(server, NULL);
+  dbus_server_set_new_connection_function(server, new_connection_cb, NULL, NULL);
 
-dbus_server_setup_with_g_main(server, NULL);
-dbus_server_set_new_connection_function(server, new_connection_cb, NULL, NULL);
+  spi_global_app_data->server = server;
 
-return 0;
+  return 0;
 }
 
 
@@ -725,8 +715,9 @@ adaptor_init (gint * argc, gchar ** argv[])
         }
     }
 
-  dbus_connection_setup_with_g_main (spi_global_app_data->bus,
-                                     g_main_context_default ());
+  spi_global_app_data->main_context = g_main_context_new ();
+
+  dbus_connection_setup_with_g_main (spi_global_app_data->bus, NULL);
 
   /* Hook our plug-and socket functions */
   install_plug_hooks ();
@@ -742,7 +733,7 @@ adaptor_init (gint * argc, gchar ** argv[])
 
   /* Register droute for routing AT-SPI messages */
   spi_global_app_data->droute =
-    droute_new (spi_global_app_data->bus);
+    droute_new ();
 
   treepath = droute_add_one (spi_global_app_data->droute,
                              "/org/a11y/atspi/cache", spi_global_cache);
@@ -780,6 +771,9 @@ adaptor_init (gint * argc, gchar ** argv[])
   spi_initialize_text (accpath);
   spi_initialize_value (accpath);
 
+  droute_context_register (spi_global_app_data->droute,
+                           spi_global_app_data->bus);
+
   /* Register methods to send D-Bus signals on certain ATK events */
   spi_atk_register_event_listeners ();
 
diff --git a/atk-adaptor/bridge.h b/atk-adaptor/bridge.h
index 7410235..728b245 100644
--- a/atk-adaptor/bridge.h
+++ b/atk-adaptor/bridge.h
@@ -48,6 +48,9 @@ struct _SpiBridge
 
   DBusConnection *bus;
   DRouteContext  *droute;
+  GMainContext *main_context;
+  DBusServer *server;
+  GList *direct_connections;
 
 /*
   SpiRegister *reg;
diff --git a/atk-adaptor/event.c b/atk-adaptor/event.c
index b973021..28de5ff 100644
--- a/atk-adaptor/event.c
+++ b/atk-adaptor/event.c
@@ -53,11 +53,23 @@ typedef struct _SpiReentrantCallClosure
 } SpiReentrantCallClosure;
 
 static void
+switch_main_context (GMainContext *cnx)
+{
+  GList *list;
+
+  dbus_server_setup_with_g_main (spi_global_app_data->server, cnx);
+  dbus_connection_setup_with_g_main (spi_global_app_data->bus, cnx);
+  for (list = spi_global_app_data->direct_connections; list; list = list->next)
+    dbus_connection_setup_with_g_main (list->data, cnx);
+}
+
+static void
 set_reply (DBusPendingCall * pending, void *user_data)
 {
   SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data; 
 
   closure->reply = dbus_pending_call_steal_reply (pending);
+  switch_main_context (NULL);
   g_main_loop_quit (closure->loop);
 }
 
@@ -67,25 +79,16 @@ send_and_allow_reentry (DBusConnection * bus, DBusMessage * message)
   DBusPendingCall *pending;
   SpiReentrantCallClosure closure;
 
-  if (!dbus_connection_send_with_reply (bus, message, &pending, -1))
-      return NULL;
-  dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL);
-  closure.loop = g_main_loop_new (NULL, FALSE);
+  closure.loop = g_main_loop_new (spi_global_app_data->main_context, FALSE);
+  switch_main_context (spi_global_app_data->main_context);
 
-  /* TODO: Remove old AT_SPI_CLIENT name */
-  if (getenv ("AT_SPI_CLIENT") || getenv ("AT_SPI_REENTER_G_MAIN_LOOP"))
-    {
-      g_main_loop_run  (closure.loop);
-    }
-  else
+  if (!dbus_connection_send_with_reply (bus, message, &pending, -1))
     {
-      closure.reply = NULL;
-      while (!closure.reply)
-        {
-          if (!dbus_connection_read_write_dispatch (spi_global_app_data->bus, 1000))
-            return NULL;
-        }
+      switch_main_context (NULL);
+      return NULL;
     }
+  dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL);
+  g_main_loop_run  (closure.loop);
   
   g_main_loop_unref (closure.loop);
   return closure.reply;
diff --git a/droute/droute.c b/droute/droute.c
index a6fe157..321144e 100644
--- a/droute/droute.c
+++ b/droute/droute.c
@@ -39,7 +39,6 @@
 
 struct _DRouteContext
 {
-    DBusConnection       *bus;
     GPtrArray            *registered_paths;
 
     gchar                *introspect_string;
@@ -48,6 +47,8 @@ struct _DRouteContext
 struct _DRoutePath
 {
     DRouteContext        *cnx;
+    gchar *path;
+    gboolean prefix;
     GStringChunk         *chunks;
     GPtrArray            *interfaces;
     GPtrArray            *introspection;
@@ -80,6 +81,8 @@ droute_object_does_not_exist_error (DBusMessage *message);
 
 static DRoutePath *
 path_new (DRouteContext *cnx,
+          const char *path,
+          gboolean prefix,
           void    *user_data,
           DRouteIntrospectChildrenFunction introspect_children_cb,
           void *introspect_children_data,
@@ -89,6 +92,8 @@ path_new (DRouteContext *cnx,
 
     new_path = g_new0 (DRoutePath, 1);
     new_path->cnx = cnx;
+    new_path->path = g_strdup (path);
+    new_path->prefix = prefix;
     new_path->chunks = g_string_chunk_new (CHUNKS_DEFAULT);
     new_path->interfaces = g_ptr_array_new ();
     new_path->introspection = g_ptr_array_new ();
@@ -114,6 +119,7 @@ path_new (DRouteContext *cnx,
 static void
 path_free (DRoutePath *path, gpointer user_data)
 {
+    g_free (path->path);
     g_string_chunk_free  (path->chunks);
     g_ptr_array_free     (path->interfaces, TRUE);
     g_ptr_array_free     (path->introspection, FALSE);
@@ -133,12 +139,11 @@ path_get_datum (DRoutePath *path, const gchar *pathstr)
 /*---------------------------------------------------------------------------*/
 
 DRouteContext *
-droute_new (DBusConnection *bus)
+droute_new ()
 {
     DRouteContext *cnx;
 
     cnx = g_new0 (DRouteContext, 1);
-    cnx->bus = bus;
     cnx->registered_paths = g_ptr_array_new ();
 
     return cnx;
@@ -153,12 +158,6 @@ droute_free (DRouteContext *cnx)
 
 /*---------------------------------------------------------------------------*/
 
-DBusConnection *
-droute_get_bus (DRouteContext *cnx)
-{
-    return cnx->bus;
-}
-
 /*---------------------------------------------------------------------------*/
 
 static DBusObjectPathVTable droute_vtable =
@@ -176,14 +175,7 @@ droute_add_one (DRouteContext *cnx,
     DRoutePath *new_path;
     gboolean registered;
 
-    new_path = path_new (cnx, (void *)data, NULL, NULL, NULL);
-
-    registered = dbus_connection_register_object_path (cnx->bus, path, &droute_vtable, new_path);
-    if (!registered)
-      {
-        path_free (new_path, NULL);
-        return NULL;
-      }
+    new_path = path_new (cnx, path, FALSE, (void *)data, NULL, NULL, NULL);
 
     g_ptr_array_add (cnx->registered_paths, new_path);
     return new_path;
@@ -199,10 +191,9 @@ droute_add_many (DRouteContext *cnx,
 {
     DRoutePath *new_path;
 
-    new_path = path_new (cnx, (void *) data, introspect_children_cb, introspect_children_data, get_datum);
-
-    if (!dbus_connection_register_fallback (cnx->bus, path, &droute_vtable, new_path))
-        oom();
+    new_path = path_new (cnx, path, TRUE, (void *) data,
+                         introspect_children_cb, introspect_children_data,
+                         get_datum);
 
     g_ptr_array_add (cnx->registered_paths, new_path);
     return new_path;
@@ -378,6 +369,32 @@ impl_prop_GetSet (DBusMessage *message,
 }
 
 static DBusHandlerResult
+handle_dbus (DBusConnection *bus,
+                   DBusMessage    *message,
+                   const gchar    *iface,
+                   const gchar    *member,
+                   const gchar    *pathstr)
+{
+  static int id = 1;
+  char *id_str = (char *) g_malloc(40);
+  DBusMessage *reply;
+
+    if (strcmp (iface, DBUS_INTERFACE_DBUS) != 0 ||
+        strcmp (member, "Hello") != 0)
+    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+    /* TODO: Fix this hack (we don't handle wrap-around, for instance) */
+    sprintf (id_str, ":1.%d", id++);
+    reply = dbus_message_new_method_return (message);
+    dbus_message_append_args (reply, DBUS_TYPE_STRING, &id_str, DBUS_TYPE_INVALID);
+    dbus_connection_send (bus, reply, NULL);
+  dbus_connection_flush (bus);
+    dbus_message_unref (reply);
+  g_free (id_str);
+    return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
 handle_properties (DBusConnection *bus,
                    DBusMessage    *message,
                    DRoutePath     *path,
@@ -545,7 +562,9 @@ handle_message (DBusConnection *bus, DBusMessage *message, void *user_data)
         iface  == NULL)
         return result;
 
-    if (!strcmp (iface, "org.freedesktop.DBus.Properties"))
+    if (!strcmp (pathstr, DBUS_PATH_DBUS))
+        result = handle_dbus (bus, message, iface, member, pathstr);
+    else if (!strcmp (iface, "org.freedesktop.DBus.Properties"))
         result = handle_properties (bus, message, path, iface, member, pathstr);
     else if (!strcmp (iface, "org.freedesktop.DBus.Introspectable"))
         result = handle_introspection (bus, message, path, iface, member, pathstr);
@@ -636,4 +655,27 @@ droute_invalid_arguments_error (DBusMessage *message)
     return reply;
 }
 
+void
+droute_path_register (DRoutePath *path, DBusConnection *bus)
+{
+    if (path->prefix)
+      dbus_connection_register_fallback (bus, path->path, &droute_vtable, path);
+    else
+      dbus_connection_register_object_path (bus, path->path,
+                                            &droute_vtable, path);
+}
+
+void
+droute_context_register (DRouteContext *cnx, DBusConnection *bus)
+{
+    g_ptr_array_foreach (cnx->registered_paths, (GFunc) droute_path_register,
+                         bus);
+}
+
+void
+droute_intercept_dbus (DBusConnection *bus)
+{
+    dbus_connection_register_object_path (bus, DBUS_PATH_DBUS,
+                                          &droute_vtable, NULL);
+}
 /*END------------------------------------------------------------------------*/
diff --git a/droute/droute.h b/droute/droute.h
index 3fdfacb..766340e 100644
--- a/droute/droute.h
+++ b/droute/droute.h
@@ -59,7 +59,7 @@ typedef struct _DRoutePath    DRoutePath;
 /*---------------------------------------------------------------------------*/
 
 DRouteContext *
-droute_new      (DBusConnection *bus);
+droute_new      ();
 
 void
 droute_free     (DRouteContext *cnx);
@@ -93,7 +93,12 @@ droute_invalid_arguments_error (DBusMessage *message);
 DBusMessage *
 droute_out_of_memory_error     (DBusMessage *message);
 
-DBusConnection *
-droute_get_bus (DRouteContext *cnx);
+void
+droute_path_register (DRoutePath *path, DBusConnection *bus);
+
+void
+droute_context_register (DRouteContext *cnx, DBusConnection *bus);
 
+void
+droute_intercept_dbus (DBusConnection *connection);
 #endif /* _DROUTE_H */



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