1 /* GLib testing framework examples and tests
3 * Copyright (C) 2010 Collabora, Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
34 static const gchar *uri = NULL;
35 static GCancellable *cancellable = NULL;
36 static gint return_value = 0;
38 static void G_GNUC_NORETURN
41 fprintf (stderr, "Usage: proxy [-s] uri\n");
42 fprintf (stderr, " Use -s to do synchronous lookups.\n");
43 fprintf (stderr, " Use -c to cancel operation.\n");
48 print_and_free_error (GError *error)
50 fprintf (stderr, "Failed to obtain proxies: %s\n", error->message);
56 print_proxies (const gchar *uri, gchar **proxies)
58 printf ("Proxies for URI '%s' are:\n", uri);
60 if (proxies == NULL || proxies[0] == NULL)
63 for (; proxies[0]; proxies++)
64 printf ("\t%s\n", proxies[0]);
68 _proxy_lookup_cb (GObject *source_object,
74 GMainLoop *loop = user_data;
76 proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (source_object),
81 print_and_free_error (error);
85 print_proxies (uri, proxies);
89 g_main_loop_quit (loop);
93 use_resolver (gboolean synchronous)
95 GProxyResolver *resolver;
97 resolver = g_proxy_resolver_get_default ();
101 GError *error = NULL;
104 proxies = g_proxy_resolver_lookup (resolver, uri, cancellable, &error);
107 print_and_free_error (error);
109 print_proxies (uri, proxies);
111 g_strfreev (proxies);
115 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
117 g_proxy_resolver_lookup_async (resolver,
123 g_main_loop_run (loop);
124 g_main_loop_unref (loop);
134 main (gint argc, gchar **argv)
136 gboolean synchronous = FALSE;
137 gboolean cancel = FALSE;
138 ProxyTestType type = USE_RESOLVER;
142 while (argc >= 2 && argv[1][0] == '-')
144 if (!strcmp (argv[1], "-s"))
146 else if (!strcmp (argv[1], "-c"))
158 /* Save URI for asyncrhonous callback */
163 cancellable = g_cancellable_new ();
164 g_cancellable_cancel (cancellable);
170 use_resolver (synchronous);