1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
This file is part of GNUnet
(C) 2012, 2013 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file src/main/gnunet-gtk.c
* @author Christian Grothoff
* @brief Gtk user interface for GNUnet
*/
#include "gnunet_gtk.h"
#if HAVE_GTK_GTKX_H
#include <gtk/gtkx.h>
#endif
/**
* Handle for a plugged in process.
*/
struct Plug
{
/**
* Kept in a DLL.
*/
struct Plug *prev;
/**
* Kept in a DLL.
*/
struct Plug *next;
/**
* The socket.
*/
GtkWidget *s;
/**
* Name of the binary.
*/
const char *binary;
/**
* Environment variable we set.
*/
const char *env_var;
/**
* Handle to the child process.
*/
struct GNUNET_OS_Process *proc;
/**
* How long do we wait on restarts?
*/
struct GNUNET_TIME_Relative backoff;
/**
* Task to restart process after crash.
*/
GNUNET_SCHEDULER_TaskIdentifier restart_task;
};
/**
* Main loop handle.
*/
static struct GNUNET_GTK_MainLoop *ml;
/**
* Our configuration.
*/
static const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* Global return value (for success/failure of gnunet-gtk).
*/
static int gret;
/**
* Head of plugs.
*/
static struct Plug *p_head;
/**
* Tail of plugs.
*/
static struct Plug *p_tail;
/**
* Get an object from the main window.
*
* @param name name of the object
* @return NULL on error, otherwise the object
*/
static GObject *
get_object (const char *name)
{
if (NULL == ml)
return NULL;
return GNUNET_GTK_main_loop_get_object (ml, name);
}
/**
* Actual main to tear down the system.
*
* @param cls the main loop handle
* @param tc scheduler context
*/
static void
cleanup_task (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct Plug *p;
while (NULL != (p = p_head))
{
if (NULL != p->proc)
{
(void) GNUNET_OS_process_kill (p->proc, SIGTERM);
GNUNET_break (GNUNET_OK ==
GNUNET_OS_process_wait (p->proc));
GNUNET_OS_process_destroy (p->proc);
p->proc = NULL;
}
if (GNUNET_SCHEDULER_NO_TASK != p->restart_task)
{
GNUNET_SCHEDULER_cancel (p->restart_task);
p->restart_task = GNUNET_SCHEDULER_NO_TASK;
}
GNUNET_CONTAINER_DLL_remove (p_head,
p_tail,
p);
GNUNET_free (p);
}
if (NULL == ml)
{
GNUNET_break (0);
return;
}
GNUNET_GTK_main_loop_quit (ml);
}
/**
* Callback invoked if the application is supposed to exit.
*/
void
gnunet_gtk_quit_cb (GObject * object,
gpointer user_data)
{
GNUNET_SCHEDULER_shutdown ();
}
#if HAVE_GTK_GTKX_H
/**
* Start the child process for the plug.
*
* @param p plug identification
*/
static void
start_process (struct Plug *p)
{
char window_id[128];
Window w;
w = gtk_socket_get_id (GTK_SOCKET (p->s));
GNUNET_snprintf (window_id,
sizeof (window_id),
"%llu",
(unsigned long long) w);
setenv (p->env_var, window_id, 1);
p->proc = GNUNET_OS_start_process (GNUNET_NO,
GNUNET_OS_INHERIT_STD_ALL,
NULL, NULL,
p->binary,
p->binary,
NULL);
}
/**
* Restart crashed plugin process.
*
* @param cls the `struct Plug` of the plugin
* @param tc scheduler context
*/
static void
restart_process (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct Plug *p = cls;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Restarting crashed plugin `%s'\n"),
p->binary);
p->restart_task = GNUNET_SCHEDULER_NO_TASK;
start_process (p);
}
/**
* The window got detached, restart the child.
*
* @param sock socket the plug got detached from
* @param userdata our `struct Plug *`
* @return TRUE (keep socket open)
*/
static gboolean
handle_remove (GtkSocket *sock,
gpointer userdata)
{
struct Plug *p = userdata;
(void) GNUNET_OS_process_kill (p->proc, SIGTERM);
GNUNET_OS_process_destroy (p->proc);
p->proc = NULL;
p->backoff = GNUNET_TIME_STD_BACKOFF (p->backoff);
p->restart_task = GNUNET_SCHEDULER_add_delayed (p->backoff,
&restart_process,
p);
return TRUE;
}
/**
* Embed process in our GUI.
*
* @param container where to embed
* @param binary name of the binary to embed
* @param env_var name of the environment variable to set
*/
static void
plug (const char *container,
const char *binary,
const char *env_var)
{
GtkContainer *v;
struct Plug *p;
p = GNUNET_new (struct Plug);
p->s = gtk_socket_new ();
gtk_widget_set_events (p->s,
GDK_ALL_EVENTS_MASK);
v = GTK_CONTAINER (get_object (container));
gtk_container_add (v, p->s);
p->binary = binary;
p->env_var = env_var;
g_signal_connect (p->s,
"plug-removed",
G_CALLBACK (handle_remove),
p);
start_process (p);
gtk_widget_show (p->s);
GNUNET_CONTAINER_DLL_insert (p_head,
p_tail,
p);
}
#endif
/**
* Actual main method that sets up the configuration window.
*
* @param cls the main loop handle
* @param tc scheduler context
*/
static void
run (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
GtkWidget *main_window;
ml = cls;
cfg = GNUNET_GTK_main_loop_get_configuration (ml);
if (GNUNET_OK != GNUNET_GTK_main_loop_build_window (ml, NULL))
return;
GNUNET_GTK_set_icon_search_path ();
GNUNET_GTK_setup_nls ();
main_window = GTK_WIDGET (get_object ("gnunet_gtk_window"));
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
&cleanup_task, NULL);
#if HAVE_GTK_GTKX_H
plug ("gnunet_statistics_hbox",
"gnunet-statistics-gtk",
"GNUNET_STATISTICS_GTK_PLUG");
plug ("gnunet_peerinfo_hbox",
"gnunet-peerinfo-gtk",
"GNUNET_PEERINFO_GTK_PLUG");
plug ("gnunet_namestore_hbox",
"gnunet-namestore-gtk",
"GNUNET_NAMESTORE_GTK_PLUG");
plug ("gnunet_fs_hbox",
"gnunet-fs-gtk",
"GNUNET_FS_GTK_PLUG");
plug ("gnunet_identity_hbox",
"gnunet-identity-gtk",
"GNUNET_IDENTITY_GTK_PLUG");
#if 0
plug ("gnunet_setup_hbox",
"gnunet-setup",
"GNUNET_SETUP_PLUG");
#endif
#endif
GNUNET_GTK_tray_icon_create (ml,
GTK_WINDOW (main_window),
"gnunet-fs-gtk",
"gnunet-gtk");
gtk_widget_show (main_window);
gtk_window_present (GTK_WINDOW (main_window));
}
/**
* Main function for gnunet-gtk.
*
* @param argc number of arguments
* @param argv arguments
* @return 0 on success
*/
int
main (int argc, char *const *argv)
{
struct GNUNET_GETOPT_CommandLineOption options[] = {
GNUNET_GETOPT_OPTION_END
};
int ret;
if (GNUNET_OK ==
GNUNET_GTK_main_loop_start ("gnunet-gtk",
"gnunet-gtk", argc, argv,
options, "gnunet_gtk.glade",
&run))
ret = gret;
else
ret = 1;
return ret;
}
/* end of gnunet-gtk.c */
|