[rygel-grilo] Implement async version of get_children()



commit b316dcda749305de50e6a22b8ba615072bb651a6
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Wed Apr 14 21:48:34 2010 +0200

    Implement async version of get_children()

 lib/media-server2-client.c |  115 +++++++++++++++++++++++++++++++++++++------
 lib/media-server2-client.h |   12 +++++
 src/test-client.c          |   68 +++++++++++++++++++++++++-
 3 files changed, 177 insertions(+), 18 deletions(-)
---
diff --git a/lib/media-server2-client.c b/lib/media-server2-client.c
index f4591eb..c1c6014 100644
--- a/lib/media-server2-client.c
+++ b/lib/media-server2-client.c
@@ -212,6 +212,34 @@ get_properties_table (const gchar *id,
   return table;
 }
 
+static GList *
+get_children_list (GHashTable *result,
+                   const gchar **properties)
+{
+  GList *child_id;
+  GList *children = NULL;
+  GList *children_id;
+  GPtrArray *prop_array;
+
+  if (!result || g_hash_table_size (result) == 0) {
+    return NULL;
+  }
+
+  children_id = g_hash_table_get_keys (result);
+
+  for (child_id = children_id; child_id; child_id = g_list_next (child_id)) {
+    prop_array = g_hash_table_lookup (result, child_id->data);
+    children = g_list_prepend (children,
+                               get_properties_table (child_id->data,
+                                                     properties,
+                                                     prop_array));
+  }
+
+  g_list_free (children_id);
+
+  return children;
+}
+
 GHashTable *
 ms2_client_get_properties (MS2Client *client,
                            const gchar *id,
@@ -322,10 +350,7 @@ ms2_client_get_children (MS2Client *client,
                          GError **error)
 {
   GHashTable *result;
-  GList *child_id;
   GList *children = NULL;
-  GList *children_id;
-  GPtrArray *prop_array;
 
   g_return_val_if_fail (MS2_IS_CLIENT (client), NULL);
 
@@ -339,22 +364,80 @@ ms2_client_get_children (MS2Client *client,
     return NULL;
   }
 
-  if (!result || g_hash_table_size (result) == 0) {
-    return NULL;
-  }
+  children = get_children_list (result, properties);
 
-  children_id = g_hash_table_get_keys (result);
+  g_boxed_free (DBUS_TYPE_CHILDREN, result);
 
-  for (child_id = children_id; child_id; child_id = g_list_next (child_id)) {
-    prop_array = g_hash_table_lookup (result, child_id->data);
-    children = g_list_prepend (children,
-                               get_properties_table (child_id->data,
-                                                     properties,
-                                                     prop_array));
-  }
+  return children;
+}
+
+static void
+get_children_async_reply (DBusGProxy *proxy,
+                          GHashTable *result,
+                          GError *error,
+                          gpointer data)
+{
+  GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (data);
+  AsyncData *adata;
+
+  adata = g_simple_async_result_get_op_res_gpointer (res);
+
+  adata->children_result = get_children_list (result,
+                                              (const gchar **) adata->properties);
 
-  g_list_free (children_id);
   g_boxed_free (DBUS_TYPE_CHILDREN, result);
 
-  return children;
+  g_simple_async_result_complete (res);
+  g_object_unref (res);
+}
+
+void ms2_client_get_children_async (MS2Client *client,
+                                    const gchar *id,
+                                    guint offset,
+                                    gint max_count,
+                                    const gchar **properties,
+                                    GAsyncReadyCallback callback,
+                                    gpointer user_data)
+{
+  AsyncData *adata;
+  GSimpleAsyncResult *res;
+
+  g_return_if_fail (MS2_IS_CLIENT (client));
+
+  adata = g_slice_new0 (AsyncData);
+
+  res = g_simple_async_result_new (G_OBJECT (client),
+                                   callback,
+                                   user_data,
+                                   ms2_client_get_children_async);
+
+  adata->client = g_object_ref (client);
+  adata->id = g_strdup (id);
+  adata->properties = g_strdupv ((gchar **) properties);
+
+  g_simple_async_result_set_op_res_gpointer (res,
+                                             adata,
+                                             (GDestroyNotify) free_async_data);
+
+  org_gnome_UPnP_MediaServer2_get_children_async (client->priv->proxy_provider,
+                                                  id,
+                                                  offset,
+                                                  max_count,
+                                                  properties,
+                                                  get_children_async_reply,
+                                                  res);
+}
+
+GList *
+ms2_client_get_children_finish (MS2Client *client,
+                                GAsyncResult *res,
+                                GError **error)
+{
+  AsyncData *adata;
+
+  g_return_val_if_fail (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (res)) ==
+                        ms2_client_get_children_async, NULL);
+
+  adata = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res));
+  return adata->children_result;
 }
diff --git a/lib/media-server2-client.h b/lib/media-server2-client.h
index 1acad17..5213573 100644
--- a/lib/media-server2-client.h
+++ b/lib/media-server2-client.h
@@ -101,4 +101,16 @@ GList *ms2_client_get_children (MS2Client *client,
                                 const gchar **properties,
                                 GError **error);
 
+void ms2_client_get_children_async (MS2Client *client,
+                                    const gchar *id,
+                                    guint offset,
+                                    gint max_count,
+                                    const gchar **properties,
+                                    GAsyncReadyCallback callback,
+                                    gpointer user_data);
+
+GList *ms2_client_get_children_finish (MS2Client *client,
+                                       GAsyncResult *res,
+                                       GError **error);
+
 #endif /* _MEDIA_SERVER2_CLIENT_H_ */
diff --git a/src/test-client.c b/src/test-client.c
index 14f4d82..873aa22 100644
--- a/src/test-client.c
+++ b/src/test-client.c
@@ -8,6 +8,70 @@ static const gchar *properties[] = { MS2_PROP_DISPLAY_NAME,
                                      NULL };
 
 static void
+children_reply (GObject *source,
+                GAsyncResult *res,
+                gpointer user_data)
+{
+  GError *error = NULL;
+  GList *children;
+  GList *child;
+  gchar *provider = (gchar *) user_data;
+
+  children = ms2_client_get_children_finish (MS2_CLIENT (source), res, &error);
+
+  if (!children) {
+    g_print ("Did not get any child, %s\n", error->message);
+    return;
+  }
+
+  g_print ("\n* Provider '%s'\n", provider);
+  g_free (provider);
+  for (child = children; child; child = g_list_next (child)) {
+    g_print ("\t* '%s', '%s'\n",
+             g_value_get_string(g_hash_table_lookup (child->data, MS2_PROP_ID)),
+             g_value_get_string(g_hash_table_lookup (child->data, MS2_PROP_DISPLAY_NAME)));
+  }
+
+  g_list_foreach (children, (GFunc) g_hash_table_unref, NULL);
+  g_list_free (children);
+  g_object_unref (source);
+}
+
+static void
+test_children_async ()
+{
+  MS2Client *client;
+  gchar **provider;
+  gchar **providers;
+
+  providers = ms2_client_get_providers ();
+
+  if (!providers) {
+    g_print ("There is no MediaServer2 provider\n");
+    return;
+  }
+
+  for (provider = providers; *provider; provider ++) {
+    client = ms2_client_new (*provider);
+
+    if (!client) {
+      g_printerr ("Unable to create a client\n");
+      return;
+    }
+
+    ms2_client_get_children_async (client,
+                                   MS2_ROOT,
+                                   0,
+                                   -1,
+                                   properties,
+                                   children_reply,
+                                   g_strdup (*provider));
+  }
+
+  g_strfreev (providers);
+}
+
+static void
 properties_reply (GObject *source,
                   GAsyncResult *res,
                   gpointer user_data)
@@ -171,7 +235,6 @@ test_children_sync ()
   g_strfreev (providers);
 }
 
-
 int main (int argc, char **argv)
 {
   GMainLoop *mainloop;
@@ -180,7 +243,8 @@ int main (int argc, char **argv)
 
   if (0) test_properties_sync ();
   if (0) test_children_sync ();
-  if (1) test_properties_async ();
+  if (0) test_properties_async ();
+  if (1) test_children_async ();
 
   mainloop = g_main_loop_new (NULL, FALSE);
   g_main_loop_run (mainloop);



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