Implemented proxy sample code
[platform/upstream/glib.git] / gio / tests / proxy.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2010 Collabora, Ltd.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <gio/gio.h>
30 #include <glib.h>
31
32 #include "glibintl.h"
33
34 static const gchar *uri = NULL;
35 static GCancellable *cancellable = NULL;
36 static gint return_value = 0;
37
38 static void G_GNUC_NORETURN
39 usage (void)
40 {
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");
44   exit (1);
45 }
46
47 static void
48 print_and_free_error (GError *error)
49 {
50   fprintf (stderr, "Failed to obtain proxies: %s\n", error->message);
51   g_error_free (error);
52   return_value = 1;
53 }
54
55 static void
56 print_proxies (const gchar *uri, gchar **proxies)
57 {
58   printf ("Proxies for URI '%s' are:\n", uri);
59
60   if (proxies == NULL || proxies[0] == NULL)
61     printf ("\tnone\n");
62   else
63     for (; proxies[0]; proxies++)
64       printf ("\t%s\n", proxies[0]);
65 }
66
67 static void
68 _proxy_lookup_cb (GObject *source_object,
69                   GAsyncResult *result,
70                   gpointer user_data)
71 {
72   GError *error = NULL;
73   gchar **proxies;
74   GMainLoop *loop = user_data;
75
76   proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (source_object),
77                                             result,
78                                             &error);
79   if (error)
80     {
81       print_and_free_error (error);
82     }
83   else
84     {
85       print_proxies (uri, proxies);
86       g_strfreev (proxies);
87     }
88
89   g_main_loop_quit (loop);
90 }
91
92 static void
93 use_resolver (gboolean synchronous)
94 {
95   GProxyResolver *resolver;
96
97   resolver = g_proxy_resolver_get_default ();
98
99   if (synchronous)
100     {
101       GError *error = NULL;
102       gchar **proxies;
103
104       proxies = g_proxy_resolver_lookup (resolver, uri, cancellable, &error);
105
106       if (error)
107           print_and_free_error (error);
108       else
109           print_proxies (uri, proxies);
110
111       g_strfreev (proxies);
112     }
113   else
114     {
115       GMainLoop *loop = g_main_loop_new (NULL, FALSE);
116
117       g_proxy_resolver_lookup_async (resolver,
118                                      uri,
119                                      cancellable,
120                                      _proxy_lookup_cb,
121                                      loop);
122
123       g_main_loop_run (loop);
124       g_main_loop_unref (loop);
125     }
126 }
127
128 typedef enum
129 {
130   USE_RESOLVER,
131 } ProxyTestType;
132
133 gint
134 main (gint argc, gchar **argv)
135 {
136   gboolean synchronous = FALSE;
137   gboolean cancel = FALSE;
138   ProxyTestType type = USE_RESOLVER;
139
140   g_type_init ();
141
142   while (argc >= 2 && argv[1][0] == '-')
143     {
144       if (!strcmp (argv[1], "-s"))
145         synchronous = TRUE;
146       else if (!strcmp (argv[1], "-c"))
147         cancel = TRUE;
148       else
149         usage ();
150
151       argv++;
152       argc--;
153     }
154
155   if (argc != 2)
156     usage ();
157
158   /* Save URI for asyncrhonous callback */
159   uri = argv[1];
160
161   if (cancel)
162     {
163       cancellable = g_cancellable_new ();
164       g_cancellable_cancel (cancellable);
165     }
166
167   switch (type)
168     {
169     case USE_RESOLVER:
170       use_resolver (synchronous);
171       break;
172     }
173
174   return return_value;
175 }