[tracker/direct-access] libtracker-bus: Implement query_async for D-Bus FD passing



commit 7fb6a241a534773cf4be30126a139bf021209086
Author: Jürg Billeter <j bitron ch>
Date:   Wed Jul 21 13:58:49 2010 +0200

    libtracker-bus: Implement query_async for D-Bus FD passing

 src/libtracker-bus/Makefile.am                |    4 +-
 src/libtracker-bus/tracker-bus-fd-cursor.c    |   98 +++++++++++++++++++++++++
 src/libtracker-bus/tracker-bus-fd-cursor.h    |   44 +++++++++++
 src/libtracker-bus/tracker-bus-fd-cursor.vapi |   21 +++++
 src/libtracker-bus/tracker-bus.vala           |    8 ++-
 5 files changed, 172 insertions(+), 3 deletions(-)
---
diff --git a/src/libtracker-bus/Makefile.am b/src/libtracker-bus/Makefile.am
index 4117377..84d8184 100644
--- a/src/libtracker-bus/Makefile.am
+++ b/src/libtracker-bus/Makefile.am
@@ -33,6 +33,7 @@ noinst_HEADERS = 			\
 # Vala sources
 vapi_sources =						\
 	$(top_srcdir)/src/libtracker-sparql/tracker-sparql-$(TRACKER_API_VERSION).vapi	\
+	$(top_srcdir)/src/libtracker-bus/tracker-bus-fd-cursor.vapi \
 	$(top_srcdir)/src/libtracker-common/libtracker-common.vapi
 
 libtracker-bus.vala.stamp: $(libtracker_bus_la_VALASOURCES) $(vapi_sources)
@@ -57,4 +58,5 @@ MAINTAINERCLEANFILES =					\
 
 EXTRA_DIST = 						\
 	$(libtracker_bus_la_VALASOURCES) \
-	libtracker-bus.vala.stamp
+	libtracker-bus.vala.stamp \
+	$(top_srcdir)/src/libtracker-bus/tracker-bus-fd-cursor.vapi
diff --git a/src/libtracker-bus/tracker-bus-fd-cursor.c b/src/libtracker-bus/tracker-bus-fd-cursor.c
index cbd0f0c..034d0dd 100644
--- a/src/libtracker-bus/tracker-bus-fd-cursor.c
+++ b/src/libtracker-bus/tracker-bus-fd-cursor.c
@@ -280,3 +280,101 @@ tracker_bus_fd_query (DBusGConnection  *gconnection,
 #endif /* HAVE_DBUS_FD_PASSING */
 }
 
+static void
+query_async_cb (gpointer  buffer,
+                gssize    buffer_size,
+                GError   *error,
+                gpointer  user_data)
+{
+	GSimpleAsyncResult *res;
+
+	res = G_SIMPLE_ASYNC_RESULT (user_data);
+
+	if (G_UNLIKELY (error)) {
+		g_simple_async_result_set_from_error (res, error);
+	} else {
+		TrackerBusFDCursor *cursor;
+
+		cursor = g_object_new (TRACKER_TYPE_BUS_FD_CURSOR, NULL);
+
+		cursor->buffer = buffer;
+		cursor->buffer_size = buffer_size;
+
+		g_simple_async_result_set_op_res_gpointer (res, cursor, g_object_unref);
+	}
+
+	g_simple_async_result_complete (res);
+	g_object_unref (res);
+}
+
+void
+tracker_bus_fd_query_async (DBusGConnection     *gconnection,
+                            const gchar         *query,
+                            GAsyncReadyCallback  callback,
+                            gpointer             user_data)
+{
+#ifdef HAVE_DBUS_FD_PASSING
+	GSimpleAsyncResult *res;
+	DBusConnection *connection;
+	DBusMessage *message;
+	DBusMessageIter iter;
+	int pipefd[2];
+
+	g_return_if_fail (gconnection != NULL);
+	g_return_if_fail (query != NULL);
+
+	if (pipe (pipefd) < 0) {
+		/* FIXME: Use proper error domain/code */
+		res = g_simple_async_result_new_error (NULL,
+		                                       callback, user_data,
+		                                       0, 0, "Cannot open pipe");
+		g_simple_async_result_complete_in_idle (res);
+		g_object_unref (res);
+		return;
+	}
+
+	connection = dbus_g_connection_get_connection (gconnection);
+
+	message = dbus_message_new_method_call (TRACKER_DBUS_SERVICE,
+	                                        TRACKER_DBUS_OBJECT_STEROIDS,
+	                                        TRACKER_DBUS_INTERFACE_STEROIDS,
+	                                        "Query");
+
+	dbus_message_iter_init_append (message, &iter);
+	dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &query);
+	dbus_message_iter_append_basic (&iter, DBUS_TYPE_UNIX_FD, &pipefd[1]);
+	close (pipefd[1]);
+
+	res = g_simple_async_result_new (NULL,
+	                                 callback,
+	                                 user_data,
+	                                 tracker_bus_fd_query_async);
+
+	tracker_dbus_send_and_splice_async (connection,
+	                                    message,
+	                                    pipefd[0],
+	                                    NULL,
+	                                    query_async_cb, res);
+	/* message is destroyed by tracker_dbus_send_and_splice_async */
+#else /* HAVE_DBUS_FD_PASSING */
+	g_assert_not_reached ();
+#endif /* HAVE_DBUS_FD_PASSING */
+}
+
+TrackerSparqlCursor *
+tracker_bus_fd_query_finish (GAsyncResult     *res,
+                             GError          **error)
+{
+#ifdef HAVE_DBUS_FD_PASSING
+	g_return_val_if_fail (res != NULL, NULL);
+
+	if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) {
+		return NULL;
+	}
+
+	return g_object_ref (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)));
+#else /* HAVE_DBUS_FD_PASSING */
+	g_assert_not_reached ();
+	return NULL;
+#endif /* HAVE_DBUS_FD_PASSING */
+}
diff --git a/src/libtracker-bus/tracker-bus-fd-cursor.h b/src/libtracker-bus/tracker-bus-fd-cursor.h
new file mode 100644
index 0000000..9b12f9b
--- /dev/null
+++ b/src/libtracker-bus/tracker-bus-fd-cursor.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2010, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __LIBTRACKER_BUS_FD_CURSOR_H__
+#define __LIBTRACKER_BUS_FD_CURSOR_H__
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include <dbus/dbus-glib.h>
+
+#include <libtracker-sparql/tracker-sparql.h>
+
+G_BEGIN_DECLS
+
+void
+tracker_bus_fd_query_async (DBusGConnection     *gconnection,
+                            const gchar         *query,
+                            GAsyncReadyCallback  callback,
+                            gpointer             user_data);
+
+TrackerSparqlCursor *
+tracker_bus_fd_query_finish (GAsyncResult     *res,
+                             GError          **error);
+
+G_END_DECLS
+
+#endif
diff --git a/src/libtracker-bus/tracker-bus-fd-cursor.vapi b/src/libtracker-bus/tracker-bus-fd-cursor.vapi
new file mode 100644
index 0000000..dc6cf8c
--- /dev/null
+++ b/src/libtracker-bus/tracker-bus-fd-cursor.vapi
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2010, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+[CCode (cheader_filename = "libtracker-bus/tracker-bus-fd-cursor.h")]
+public async Tracker.Sparql.Cursor tracker_bus_fd_query_async (DBus.Connection connection, string query) throws GLib.Error;
diff --git a/src/libtracker-bus/tracker-bus.vala b/src/libtracker-bus/tracker-bus.vala
index 68c2a30..7ae90ed 100644
--- a/src/libtracker-bus/tracker-bus.vala
+++ b/src/libtracker-bus/tracker-bus.vala
@@ -99,8 +99,12 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
 	}
 
 	public async override Sparql.Cursor? query_async (string sparql, Cancellable? cancellable = null) throws GLib.Error {
-		string[,] results = yield resources_object.sparql_query_async (sparql);
-		return new Tracker.Bus.ArrayCursor ((owned) results, results.length[0], results.length[1]);
+		if (use_steroids) {
+			return yield tracker_bus_fd_query_async (connection, sparql);
+		} else {
+			string[,] results = yield resources_object.sparql_query_async (sparql);
+			return new Tracker.Bus.ArrayCursor ((owned) results, results.length[0], results.length[1]);
+		}
 	}
 
 	public override void update (string sparql, Cancellable? cancellable = null) throws GLib.Error {



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