[rygel-grilo] Implement async version of get_properties
- From: Juan A. Suarez Romero <jasuarez src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [rygel-grilo] Implement async version of get_properties
- Date: Wed, 14 Apr 2010 19:48:56 +0000 (UTC)
commit 115d5d2a46ae4db9009085c5b79399c9b7e81323
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date: Wed Apr 14 21:22:41 2010 +0200
Implement async version of get_properties
configure.ac | 1 +
lib/media-server2-client.c | 84 ++++++++++++++++++++++++++++++++++++++++++++
lib/media-server2-client.h | 13 ++++++-
src/test-client.c | 83 ++++++++++++++++++++++++++++++++++++++++---
4 files changed, 175 insertions(+), 6 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index d5bca5b..d80a010 100644
--- a/configure.ac
+++ b/configure.ac
@@ -51,6 +51,7 @@ AM_CONDITIONAL([DEBUG], [test "x$enable_debug" = "xyes"])
PKG_CHECK_MODULES(DEPS, glib-2.0 \
gobject-2.0 \
gmodule-2.0 \
+ gio-2.0 \
grilo-0.1 \
dbus-glib-1)
diff --git a/lib/media-server2-client.c b/lib/media-server2-client.c
index 12c5c0b..f4591eb 100644
--- a/lib/media-server2-client.c
+++ b/lib/media-server2-client.c
@@ -46,6 +46,14 @@
#define MS2_CLIENT_GET_PRIVATE(o) \
G_TYPE_INSTANCE_GET_PRIVATE((o), MS2_TYPE_CLIENT, MS2ClientPrivate)
+typedef struct {
+ GHashTable *properties_result;
+ GList *children_result;
+ MS2Client *client;
+ gchar **properties;
+ gchar *id;
+} AsyncData;
+
struct _MS2ClientPrivate {
DBusGProxy *proxy_provider;
};
@@ -229,6 +237,82 @@ ms2_client_get_properties (MS2Client *client,
return prop_result;
}
+static void
+free_async_data (AsyncData *adata)
+{
+ g_object_unref (adata->client);
+ g_free (adata->id);
+ g_strfreev (adata->properties);
+ g_slice_free (AsyncData, adata);
+}
+
+static void
+get_properties_async_reply (DBusGProxy *proxy,
+ GPtrArray *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->properties_result = get_properties_table (adata->id,
+ (const gchar **) adata->properties,
+ result);
+ g_boxed_free (DBUS_TYPE_PROPERTIES, result);
+
+ g_simple_async_result_complete (res);
+ g_object_unref (res);
+}
+
+void ms2_client_get_properties_async (MS2Client *client,
+ const gchar *id,
+ 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_properties_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_properties_async (client->priv->proxy_provider,
+ id,
+ properties,
+ get_properties_async_reply,
+ res);
+}
+
+GHashTable *
+ms2_client_get_properties_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_properties_async, NULL);
+
+ adata = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res));
+ return adata->properties_result;
+}
+
GList *
ms2_client_get_children (MS2Client *client,
const gchar *id,
diff --git a/lib/media-server2-client.h b/lib/media-server2-client.h
index 5816ea7..1acad17 100644
--- a/lib/media-server2-client.h
+++ b/lib/media-server2-client.h
@@ -23,8 +23,9 @@
#ifndef _MEDIA_SERVER2_CLIENT_H_
#define _MEDIA_SERVER2_CLIENT_H_
-#include <glib.h>
+#include <gio/gio.h>
#include <glib-object.h>
+#include <glib.h>
#include "media-server2-common.h"
@@ -83,6 +84,16 @@ GHashTable *ms2_client_get_properties (MS2Client *client,
const gchar **properties,
GError **error);
+void ms2_client_get_properties_async (MS2Client *client,
+ const gchar *id,
+ const gchar **properties,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+
+GHashTable *ms2_client_get_properties_finish (MS2Client *client,
+ GAsyncResult *res,
+ GError **error);
+
GList *ms2_client_get_children (MS2Client *client,
const gchar *id,
guint offset,
diff --git a/src/test-client.c b/src/test-client.c
index 40ffab9..14f4d82 100644
--- a/src/test-client.c
+++ b/src/test-client.c
@@ -2,10 +2,76 @@
#include <glib.h>
#include <string.h>
-static const gchar *properties[] = { MS2_PROP_DISPLAY_NAME,
- MS2_PROP_PARENT,
- MS2_PROP_CHILD_COUNT,
- NULL };
+static const gchar *properties[] = { MS2_PROP_DISPLAY_NAME,
+ MS2_PROP_PARENT,
+ MS2_PROP_CHILD_COUNT,
+ NULL };
+
+static void
+properties_reply (GObject *source,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GError *error = NULL;
+ GHashTable *result;
+ GValue *v;
+ const gchar **p;
+ gchar *provider = (gchar *) user_data;
+
+ result = ms2_client_get_properties_finish (MS2_CLIENT (source), res, &error);
+
+ if (!result) {
+ g_print ("Did not get any property, %s\n", error->message);
+ return;
+ }
+
+ g_print ("\n* Provider '%s'\n", provider);
+ g_free (provider);
+ for (p = properties; *p; p++) {
+ v = g_hash_table_lookup (result, *p);
+ if (v && G_VALUE_HOLDS_INT (v)) {
+ g_print ("\t* '%s' value: '%d'\n", *p, g_value_get_int (v));
+ } else if (v && G_VALUE_HOLDS_STRING (v)) {
+ g_print ("\t* '%s' value: '%s'\n", *p, g_value_get_string (v));
+ } else {
+ g_print ("\t* '%s' value: ---\n", *p);
+ }
+ }
+ g_hash_table_unref (result);
+ g_object_unref (source);
+}
+
+static void
+test_properties_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_properties_async (client,
+ MS2_ROOT,
+ properties,
+ properties_reply,
+ g_strdup (*provider));
+ }
+
+ g_strfreev (providers);
+}
static void
test_properties_sync ()
@@ -108,8 +174,15 @@ test_children_sync ()
int main (int argc, char **argv)
{
+ GMainLoop *mainloop;
+
g_type_init ();
if (0) test_properties_sync ();
- if (1) test_children_sync ();
+ if (0) test_children_sync ();
+ if (1) test_properties_async ();
+
+ mainloop = g_main_loop_new (NULL, FALSE);
+ g_main_loop_run (mainloop);
+ g_main_loop_unref (mainloop);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]