diff options
Diffstat (limited to 'src/setup/gnunet-setup-hostlist-server.c')
-rw-r--r-- | src/setup/gnunet-setup-hostlist-server.c | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/src/setup/gnunet-setup-hostlist-server.c b/src/setup/gnunet-setup-hostlist-server.c new file mode 100644 index 00000000..4cad17fa --- /dev/null +++ b/src/setup/gnunet-setup-hostlist-server.c | |||
@@ -0,0 +1,205 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet 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 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/gnunet-setup-hostlist-server.c | ||
23 | * @brief (de)sensitize UI elements related to hostlist server configuration | ||
24 | * based on the build configuration of the hostlist daemon | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | #include "gnunet-setup.h" | ||
28 | #include <gnunet/gnunet_util_lib.h> | ||
29 | |||
30 | |||
31 | /** | ||
32 | * Timeout for hostlist daemon to print help information | ||
33 | */ | ||
34 | #define CMD_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3) | ||
35 | |||
36 | |||
37 | /** | ||
38 | * Function called to report the result of testing the hostlist daemon. | ||
39 | * | ||
40 | * @param cls closure | ||
41 | * @param result GNUNET_YES if the hostlist daemon has an integrated hostlist | ||
42 | * server, GNUNET_NO otherwise | ||
43 | */ | ||
44 | typedef void (*TestHostlistDaemonCallback) (void *cls, | ||
45 | int result); | ||
46 | |||
47 | |||
48 | /** | ||
49 | * Context for running hostlist daemon and processing its output. | ||
50 | */ | ||
51 | struct CommandContext | ||
52 | { | ||
53 | /** | ||
54 | * Handle to the command. | ||
55 | */ | ||
56 | struct GNUNET_OS_CommandHandle *cmd; | ||
57 | |||
58 | /** | ||
59 | * Where to pass the result. | ||
60 | */ | ||
61 | TestHostlistDaemonCallback callback; | ||
62 | |||
63 | /** | ||
64 | * Closure for the callback. | ||
65 | */ | ||
66 | void *callback_cls; | ||
67 | |||
68 | /** | ||
69 | * GNUNET_YES if the target argument was found, GNUNET_NO otherwise. | ||
70 | */ | ||
71 | int found; | ||
72 | |||
73 | }; | ||
74 | |||
75 | |||
76 | static void | ||
77 | set_checkbutton_status (void *cls, | ||
78 | int result) | ||
79 | { | ||
80 | GtkWidget * widget = cls; | ||
81 | GtkToggleButton *button; | ||
82 | |||
83 | if (GNUNET_YES != result) | ||
84 | { | ||
85 | gtk_widget_set_sensitive (widget, FALSE); | ||
86 | button = GTK_TOGGLE_BUTTON (widget); | ||
87 | if (button == NULL) | ||
88 | { | ||
89 | GNUNET_break (0); | ||
90 | return; | ||
91 | } | ||
92 | /* also deactivate the checkbutton since this option could be enabled | ||
93 | by editing the config file manually */ | ||
94 | gtk_toggle_button_set_active (button, FALSE); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | |||
99 | static void | ||
100 | set_spinbutton_status (void *cls, | ||
101 | int result) | ||
102 | { | ||
103 | GtkWidget * widget = cls; | ||
104 | |||
105 | if (GNUNET_YES != result) | ||
106 | gtk_widget_set_sensitive (widget, FALSE); | ||
107 | } | ||
108 | |||
109 | |||
110 | /** | ||
111 | * Process the output from the 'gnunet-daemon-hostlist -h' command | ||
112 | * to find out whether the hostlist daemon can provide a hostlist server. | ||
113 | * | ||
114 | * @param cls the 'struct CommandContext' | ||
115 | * @param line line of output, NULL at the end | ||
116 | */ | ||
117 | static void | ||
118 | process_hostlist_daemon_output (void *cls, const char *line) | ||
119 | { | ||
120 | struct CommandContext *ctx = cls; | ||
121 | char *t; | ||
122 | char *w; | ||
123 | |||
124 | if (NULL == line) | ||
125 | { | ||
126 | ctx->callback (ctx->callback_cls, ctx->found); | ||
127 | GNUNET_OS_command_stop (ctx->cmd); | ||
128 | GNUNET_free (ctx); | ||
129 | return; | ||
130 | } | ||
131 | |||
132 | t = GNUNET_strdup (line); | ||
133 | w = strtok (t, " "); | ||
134 | while (w != NULL) | ||
135 | { | ||
136 | if (0 == strcmp (w, "--provide-hostlist")) | ||
137 | { | ||
138 | ctx->found = GNUNET_YES; | ||
139 | break; | ||
140 | } | ||
141 | w = strtok (NULL, " "); | ||
142 | } | ||
143 | GNUNET_free (t); | ||
144 | } | ||
145 | |||
146 | |||
147 | /** | ||
148 | * Run 'gnunet-daemon-hostlist -h'. Output of this command will let us | ||
149 | * know whether the hostlist daemon can provide a hostlist server. | ||
150 | * | ||
151 | * @param callback function to call with the result | ||
152 | * @param cls closure for the callback | ||
153 | */ | ||
154 | static void | ||
155 | test_hostlist_daemon (TestHostlistDaemonCallback callback, | ||
156 | void *cls) | ||
157 | { | ||
158 | struct CommandContext *ctx; | ||
159 | |||
160 | ctx = GNUNET_malloc (sizeof (struct CommandContext)); | ||
161 | ctx->callback = callback; | ||
162 | ctx->callback_cls = cls; | ||
163 | ctx->found = GNUNET_NO; | ||
164 | ctx->cmd = GNUNET_OS_command_run (&process_hostlist_daemon_output, | ||
165 | ctx, | ||
166 | CMD_TIMEOUT, | ||
167 | "gnunet-daemon-hostlist", | ||
168 | "gnunet-daemon-hostlist", | ||
169 | "-h", | ||
170 | NULL); | ||
171 | if (NULL == ctx->cmd) | ||
172 | { | ||
173 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
174 | _("Could not determine whether the hostlist daemon has" | ||
175 | " an integrated hostlist server!\n")); | ||
176 | GNUNET_free (ctx); | ||
177 | } | ||
178 | } | ||
179 | |||
180 | |||
181 | void | ||
182 | GNUNET_setup_hostlist_offer_hostlist_checkbutton_realize_cb (GtkWidget * widget, | ||
183 | gpointer user_data) | ||
184 | { | ||
185 | test_hostlist_daemon (&set_checkbutton_status, widget); | ||
186 | } | ||
187 | |||
188 | |||
189 | void | ||
190 | GNUNET_setup_hostlist_advertise_checkbutton_realize_cb (GtkWidget * widget, | ||
191 | gpointer user_data) | ||
192 | { | ||
193 | test_hostlist_daemon (&set_checkbutton_status, widget); | ||
194 | } | ||
195 | |||
196 | |||
197 | void | ||
198 | GNUNET_setup_hostlist_server_port_spin_button_realize_cb (GtkWidget * widget, | ||
199 | gpointer user_data) | ||
200 | { | ||
201 | test_hostlist_daemon (&set_spinbutton_status, widget); | ||
202 | } | ||
203 | |||
204 | |||
205 | /* end of gnunet-setup-hostlist-server.c */ | ||