Debian Codegraph @

examples/host.c

raw symbols SCIP
1 /* host.c
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * 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 General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include <libdex.h>
24
25 static DexFuture *
26 resolve_address (gpointer user_data)
27 {
28 GResolver *resolver = g_resolver_get_default ();
29 const char *addr = user_data;
30 g_autoptr(GError) error = NULL;
31 g_autolist(GInetAddress) addrs = NULL;
32
33 addrs = dex_await_boxed (dex_resolver_lookup_by_name (resolver, addr), &error);
34
35 if (error != NULL)
36 {
37 g_printerr ("%s: %s\n", addr, error->message);
38 return NULL;
39 }
40
41 for (const GList *iter = addrs; iter; iter = iter->next)
42 {
43 GInetAddress *inet_address = iter->data;
44 g_autofree char *str = g_inet_address_to_string (inet_address);
45
46 g_print ("%s has address %s\n", addr, str);
47 }
48
49 return NULL;
50 }
51
52 static DexFuture *
53 quit_cb (DexFuture *completed,
54 gpointer user_data)
55 {
56 GMainLoop *main_loop = user_data;
57 g_main_loop_quit (main_loop);
58 return NULL;
59 }
60
61 int
62 main (int argc,
63 char *argv[])
64 {
65 g_autoptr(GMainLoop) main_loop = g_main_loop_new (NULL, FALSE);
66 g_autoptr(GPtrArray) futures = g_ptr_array_new ();
67 g_autoptr(DexFuture) future = NULL;
68
69 dex_init ();
70
71 if (argc < 2)
72 {
73 g_printerr ("usage: %s HOSTNAME...\n", argv[0]);
74 return 1;
75 }
76
77 for (guint i = 1; i < argc; i++)
78 {
79 const char *addr = argv[i];
80
81 g_ptr_array_add (futures,
82 dex_scheduler_spawn (NULL, 0, resolve_address, g_strdup (addr), g_free));
83 }
84
85 future = dex_future_allv ((DexFuture **)futures->pdata, futures->len);
86 future = dex_future_finally (future, quit_cb, main_loop, NULL);
87
88 g_main_loop_run (main_loop);
89
90 return 0;
91 }