GResolver wrappers: GNetworkAddress, GNetworkService, GSocketConnectable
[platform/upstream/glib.git] / gio / gsocketaddressenumerator.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2008 Red Hat, Inc.
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
21 #include "config.h"
22 #include "gsocketaddressenumerator.h"
23 #include "glibintl.h"
24
25 #include "gsimpleasyncresult.h"
26
27 #include "gioalias.h"
28
29 G_DEFINE_ABSTRACT_TYPE (GSocketAddressEnumerator, g_socket_address_enumerator, G_TYPE_OBJECT);
30
31 static void            g_socket_address_enumerator_real_next_async  (GSocketAddressEnumerator  *enumerator,
32                                                                      GCancellable              *cancellable,
33                                                                      GAsyncReadyCallback        callback,
34                                                                      gpointer                   user_data);
35 static GSocketAddress *g_socket_address_enumerator_real_next_finish (GSocketAddressEnumerator  *enumerator,
36                                                                      GAsyncResult              *result,
37                                                                      GError                   **error);
38
39 static void
40 g_socket_address_enumerator_init (GSocketAddressEnumerator *enumerator)
41 {
42 }
43
44 static void
45 g_socket_address_enumerator_class_init (GSocketAddressEnumeratorClass *enumerator_class)
46 {
47   enumerator_class->next_async = g_socket_address_enumerator_real_next_async;
48   enumerator_class->next_finish = g_socket_address_enumerator_real_next_finish;
49 }
50
51 /**
52  * g_socket_address_enumerator_next:
53  * @enumerator: a #GSocketAddressEnumerator
54  * @cancellable: optional #GCancellable object, %NULL to ignore.
55  * @error: a #GError.
56  *
57  * Retrieves the next #GSocketAddress from @enumerator. Note that this
58  * may block for some amount of time. (Eg, a #GNetworkAddress may need
59  * to do a DNS lookup before it can return an address.) Use
60  * g_socket_address_enumerator_next_async() if you need to avoid
61  * blocking.
62  *
63  * If @enumerator is expected to yield addresses, but for some reason
64  * is unable to (eg, because of a DNS error), then the first call to
65  * g_socket_address_enumerator_next() will return an appropriate error
66  * in *@error. However, if the first call to
67  * g_socket_address_enumerator_next() succeeds, then any further
68  * internal errors (other than @cancellable being triggered) will be
69  * ignored.
70  *
71  * Return value: a #GSocketAddress (owned by the caller), or %NULL on
72  * error (in which case *@error will be set) or if there are no more
73  * addresses.
74  **/
75 GSocketAddress *
76 g_socket_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
77                                   GCancellable              *cancellable,
78                                   GError                   **error)
79 {
80   GSocketAddressEnumeratorClass *klass;
81
82   g_return_val_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator), NULL);
83
84   klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator);
85
86   return (* klass->next) (enumerator, cancellable, error);
87 }
88
89 /* Default implementation just calls the synchronous method; this can
90  * be used if the implementation already knows all of its addresses,
91  * and so the synchronous method will never block.
92  */
93 static void
94 g_socket_address_enumerator_real_next_async (GSocketAddressEnumerator  *enumerator,
95                                              GCancellable              *cancellable,
96                                              GAsyncReadyCallback        callback,
97                                              gpointer                   user_data)
98 {
99   GSimpleAsyncResult *result;
100   GSocketAddress *address;
101   GError *error = NULL;
102
103   result = g_simple_async_result_new (G_OBJECT (enumerator),
104                                       callback, user_data,
105                                       g_socket_address_enumerator_real_next_async);
106   address = g_socket_address_enumerator_next (enumerator, cancellable, &error);
107   if (address)
108     g_simple_async_result_set_op_res_gpointer (result, address, NULL);
109   else if (error)
110     {
111       g_simple_async_result_set_from_error (result, error);
112       g_error_free (error);
113     }
114   g_simple_async_result_complete_in_idle (result);
115   g_object_unref (result);
116 }
117
118 /**
119  * g_socket_address_enumerator_next_async:
120  * @enumerator: a #GSocketAddressEnumerator
121  * @cancellable: optional #GCancellable object, %NULL to ignore.
122  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
123  * @user_data: the data to pass to callback function
124  *
125  * Asynchronously retrieves the next #GSocketAddress from @enumerator
126  * and then calls @callback, which must call
127  * g_socket_address_enumerator_next_finish() to get the result.
128  **/
129 void
130 g_socket_address_enumerator_next_async (GSocketAddressEnumerator  *enumerator,
131                                         GCancellable              *cancellable,
132                                         GAsyncReadyCallback        callback,
133                                         gpointer                   user_data)
134 {
135   GSocketAddressEnumeratorClass *klass;
136
137   g_return_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator));
138
139   klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator);
140
141   (* klass->next_async) (enumerator, cancellable, callback, user_data);
142 }
143
144 static GSocketAddress *
145 g_socket_address_enumerator_real_next_finish (GSocketAddressEnumerator  *enumerator,
146                                               GAsyncResult              *result,
147                                               GError                   **error)
148 {
149   GSimpleAsyncResult *simple;
150
151   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
152   simple = G_SIMPLE_ASYNC_RESULT (result);
153   g_return_val_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_address_enumerator_real_next_async, NULL);
154
155   if (g_simple_async_result_propagate_error (simple, error))
156     return NULL;
157   else
158     return g_simple_async_result_get_op_res_gpointer (simple);
159 }
160
161 /**
162  * g_socket_address_enumerator_next_finish:
163  * @enumerator: a #GSocketAddressEnumerator
164  * @result: a #GAsyncResult.
165  * @error: a #GError.
166  *
167  * Retrieves the result of a completed call to
168  * g_socket_address_enumerator_next_async(). See
169  * g_socket_address_enumerator_next() for more information about
170  * error handling.
171  *
172  * Return value: a #GSocketAddress (owned by the caller), or %NULL on
173  * error (in which case *@error will be set) or if there are no more
174  * addresses.
175  **/
176 GSocketAddress *
177 g_socket_address_enumerator_next_finish (GSocketAddressEnumerator  *enumerator,
178                                          GAsyncResult              *result,
179                                          GError                   **error)
180 {
181   GSocketAddressEnumeratorClass *klass;
182
183   g_return_val_if_fail (G_IS_SOCKET_ADDRESS_ENUMERATOR (enumerator), NULL);
184
185   klass = G_SOCKET_ADDRESS_ENUMERATOR_GET_CLASS (enumerator);
186
187   return (* klass->next_finish) (enumerator, result, error);
188 }
189
190 #define __G_SOCKET_ADDRESS_ENUMERATOR_C__
191 #include "gioaliasdef.c"