[glib] Implemented proxy sample code



commit 6b1d851cc5ba8ce4276f2e3236da7554b763cf0c
Author: Nicolas Dufresne <nicolas dufresne collabora co uk>
Date:   Fri May 7 16:23:45 2010 -0400

    Implemented proxy sample code
    
    Reviewed-by: Dan Winship <danw gnome org>

 gio/tests/.gitignore  |    1 +
 gio/tests/Makefile.am |    5 ++
 gio/tests/proxy.c     |  175 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 181 insertions(+), 0 deletions(-)
---
diff --git a/gio/tests/.gitignore b/gio/tests/.gitignore
index 1031dfe..eaf1011 100644
--- a/gio/tests/.gitignore
+++ b/gio/tests/.gitignore
@@ -53,6 +53,7 @@ memory-input-stream
 memory-output-stream
 network-address
 org.gtk.test.enums.xml
+proxy
 readwrite
 resolver
 send-data
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index bb09a86..eac12a8 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -83,6 +83,7 @@ SAMPLE_PROGS = 				\
 	gdbus-connection-flush-helper	\
 	testapp				\
 	appinfo-test			\
+	proxy				\
 	$(NULL)
 
 
@@ -371,6 +372,10 @@ schema_tests = \
 	schema-tests/range.gschema.xml					\
 	schema-tests/wrong-category.gschema.xml
 
+proxy_SOURCES = proxy.c
+proxy_LDADD   = $(progs_ldadd) \
+	$(top_builddir)/gthread/libgthread-2.0.la
+
 EXTRA_DIST += \
 	socket-common.c			\
 	org.gtk.test.gschema		\
diff --git a/gio/tests/proxy.c b/gio/tests/proxy.c
new file mode 100644
index 0000000..e5dedb6
--- /dev/null
+++ b/gio/tests/proxy.c
@@ -0,0 +1,175 @@
+/* GLib testing framework examples and tests
+ *
+ * Copyright (C) 2010 Collabora, Ltd.
+ *
+ * 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 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., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Nicolas Dufresne <nicolas dufresne collabora co uk>
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <gio/gio.h>
+#include <glib.h>
+
+#include "glibintl.h"
+
+static const gchar *uri = NULL;
+static GCancellable *cancellable = NULL;
+static gint return_value = 0;
+
+static void G_GNUC_NORETURN
+usage (void)
+{
+  fprintf (stderr, "Usage: proxy [-s] uri\n");
+  fprintf (stderr, "       Use -s to do synchronous lookups.\n");
+  fprintf (stderr, "       Use -c to cancel operation.\n");
+  exit (1);
+}
+
+static void
+print_and_free_error (GError *error)
+{
+  fprintf (stderr, "Failed to obtain proxies: %s\n", error->message);
+  g_error_free (error);
+  return_value = 1;
+}
+
+static void
+print_proxies (const gchar *uri, gchar **proxies)
+{
+  printf ("Proxies for URI '%s' are:\n", uri);
+
+  if (proxies == NULL || proxies[0] == NULL)
+    printf ("\tnone\n");
+  else
+    for (; proxies[0]; proxies++)
+      printf ("\t%s\n", proxies[0]);
+}
+
+static void
+_proxy_lookup_cb (GObject *source_object,
+		  GAsyncResult *result,
+		  gpointer user_data)
+{
+  GError *error = NULL;
+  gchar **proxies;
+  GMainLoop *loop = user_data;
+
+  proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (source_object),
+					    result,
+					    &error);
+  if (error)
+    {
+      print_and_free_error (error);
+    }
+  else
+    {
+      print_proxies (uri, proxies);
+      g_strfreev (proxies);
+    }
+
+  g_main_loop_quit (loop);
+}
+
+static void
+use_resolver (gboolean synchronous)
+{
+  GProxyResolver *resolver;
+
+  resolver = g_proxy_resolver_get_default ();
+
+  if (synchronous)
+    {
+      GError *error = NULL;
+      gchar **proxies;
+
+      proxies = g_proxy_resolver_lookup (resolver, uri, cancellable, &error);
+
+      if (error)
+	  print_and_free_error (error);
+      else
+	  print_proxies (uri, proxies);
+
+      g_strfreev (proxies);
+    }
+  else
+    {
+      GMainLoop *loop = g_main_loop_new (NULL, FALSE);
+
+      g_proxy_resolver_lookup_async (resolver,
+				     uri,
+				     cancellable,
+				     _proxy_lookup_cb,
+				     loop);
+
+      g_main_loop_run (loop);
+      g_main_loop_unref (loop);
+    }
+}
+
+typedef enum
+{
+  USE_RESOLVER,
+} ProxyTestType;
+
+gint
+main (gint argc, gchar **argv)
+{
+  gboolean synchronous = FALSE;
+  gboolean cancel = FALSE;
+  ProxyTestType type = USE_RESOLVER;
+
+  g_type_init ();
+
+  while (argc >= 2 && argv[1][0] == '-')
+    {
+      if (!strcmp (argv[1], "-s"))
+        synchronous = TRUE;
+      else if (!strcmp (argv[1], "-c"))
+        cancel = TRUE;
+      else
+	usage ();
+
+      argv++;
+      argc--;
+    }
+
+  if (argc != 2)
+    usage ();
+
+  /* Save URI for asyncrhonous callback */
+  uri = argv[1];
+
+  if (cancel)
+    {
+      cancellable = g_cancellable_new ();
+      g_cancellable_cancel (cancellable);
+    }
+
+  switch (type)
+    {
+    case USE_RESOLVER:
+      use_resolver (synchronous);
+      break;
+    }
+
+  return return_value;
+}



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