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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
/*
This file is part of GNUnet.
(C) 2010, 2011 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 2, 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/statistics/gnunet-statistics-gtk.c
* @brief Main function of gnunet-statistics-gtk
* @author Christian Grothoff
*/
#include "gnunet_gtk.h"
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_statistics_service.h>
#include <cairo.h>
#define MAX_HISTORY 1280
/**
* Information about a value we received.
*/
struct HistoricValue
{
/**
* A value we recorded.
*/
uint64_t value;
/**
* Time when the value was recorded.
*/
struct GNUNET_TIME_Absolute timestamp;
};
/**
* Historic information we recorded for a value.
*/
struct ValueHistory
{
/**
* Name of the subsystem.
*/
char *subsystem;
/**
* Name of the statistic.
*/
char *name;
/**
* Recent values for this number.
*/
struct HistoricValue history[MAX_HISTORY];
/**
* Last offset we wrote to in history.
*/
unsigned int last_history_offset;
/**
* Number of valid entries in the history.
*/
unsigned int history_size;
};
/**
* Information about how to plot certain values.
*/
struct PlotInfo
{
/**
* Subsystem originating the value to plot.
*/
const char *subsystem;
/**
* Name of the value to plot.
*/
const char *name;
/**
* Name of color to use when plotting.
*/
const char *color_name;
};
/**
* Handle to our main loop.
*/
static struct GNUNET_GTK_MainLoop *ml;
/**
* Should gnunet-peerinfo-gtk start in tray mode?
*/
static int tray_only;
/**
* Handle to statistics subsystem.
*/
static struct GNUNET_STATISTICS_Handle *statistics;
/**
* Map from keys (subsystem+value) to 'struct ValueHistory'.
*/
static struct GNUNET_CONTAINER_MultiHashMap *stat_map;
/**
* Task that refreshes connection graphic.
*/
static GNUNET_SCHEDULER_TaskIdentifier connection_task;
/**
* Pixmap where we have the connection image.
*/
static GdkPixmap *connection_pixmap;
/**
* How often do we refresh?
*/
static struct GNUNET_TIME_Relative refresh_delay;
/**
* Obtain key for 'stat_map' for a given subsystem and name.
*
* @param subsystem subsystem
* @param name name
* @param key set to the hash map key
*/
static void
get_key (const char *subsystem,
const char *name,
GNUNET_HashCode *key)
{
GNUNET_HashCode h1;
GNUNET_HashCode h2;
GNUNET_CRYPTO_hash (subsystem, strlen(subsystem), &h1);
GNUNET_CRYPTO_hash (name, strlen(name), &h2);
GNUNET_CRYPTO_hash_xor (&h1, &h2, key);
}
/**
* Callback function to process statistic values.
*
* @param cls the 'struct ValueHistory' to update
* @param subsystem name of subsystem that created the statistic
* @param name the name of the datum
* @param value the current value
* @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
* @return GNUNET_OK to continue
*/
static int
process_value_update (void *cls, const char *subsystem,
const char *name, uint64_t value,
int is_persistent)
{
struct ValueHistory *vh = cls;
GNUNET_HashCode key;
get_key (subsystem, name, &key);
if (++vh->last_history_offset == MAX_HISTORY)
vh->last_history_offset = 0;
if (vh->history_size < MAX_HISTORY)
vh->history_size++;
vh->history[vh->last_history_offset].value = value;
vh->history[vh->last_history_offset].timestamp = GNUNET_TIME_absolute_get ();
return GNUNET_OK;
}
/**
* Begin monitoring a particular value.
*
* @param subsystem name of subsystem that created the statistic
* @param name the name of the datum
*/
static void
monitor (const char *subsystem,
const char *name)
{
GNUNET_HashCode key;
struct ValueHistory *vh;
get_key (subsystem, name, &key);
vh = GNUNET_CONTAINER_multihashmap_get (stat_map, &key);
if (NULL != vh)
return;
vh = GNUNET_malloc (sizeof (struct ValueHistory));
vh->subsystem = GNUNET_strdup (subsystem);
vh->name = GNUNET_strdup (name);
GNUNET_assert (GNUNET_OK ==
GNUNET_CONTAINER_multihashmap_put (stat_map,
&key,
vh,
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
GNUNET_STATISTICS_watch (statistics,
subsystem,
name,
&process_value_update,
vh);
}
/**
* Get an object from the main window.
*
* @param name name of the object
* @return NULL on error
*/
static GObject *
get_object (const char *name)
{
return GNUNET_GTK_main_loop_get_object (ml, name);
}
/**
* Actually draw a plot based on collected data.
*
* @param widget size and style information for the plot
* @param info what to draw
* @return pixmap with the drawing
*/
static GdkPixmap *
create_plot (GtkWidget *widget,
const struct PlotInfo *info)
{
GdkPixmap *ret;
GdkGC *gc;
unsigned int i;
GNUNET_HashCode key;
struct ValueHistory *vh;
GdkColor color;
GdkColormap *colormap;
ret = gdk_pixmap_new (widget->window,
widget->allocation.width,
widget->allocation.height,
gtk_widget_get_visual (widget)->depth);
colormap = gdk_window_get_colormap (widget->window);
gc = gdk_gc_new (widget->window);
gdk_gc_copy (gc, widget->style->white_gc);
gdk_color_parse ("black", &color);
gdk_colormap_alloc_color (colormap, &color, TRUE, TRUE);
gdk_gc_set_foreground (gc, &color);
gdk_draw_rectangle (GDK_DRAWABLE (ret),
widget->style->black_gc,
TRUE, 0, 0,
widget->allocation.width,
widget->allocation.height);
for (i=0; NULL != info[i].subsystem; i++)
{
get_key (info[i].subsystem, info[i].name, &key);
vh = GNUNET_CONTAINER_multihashmap_get (stat_map, &key);
if (NULL == vh)
continue;
gdk_color_parse (info[i].color_name, &color);
gdk_colormap_alloc_color (colormap, &color, TRUE, TRUE);
gdk_gc_set_foreground (gc, &color);
}
// FIXME
return ret;
}
/**
* Redraw the 'connection_pixmap'.
*
* @param widget where to render it to in the end (also for size to use)
*/
static void
plot_connection_graph (GtkWidget *widget)
{
static const struct PlotInfo connection_data[] =
{
{ "core", "entries in session map", "white" },
{ NULL, NULL, NULL}
};
if (NULL != connection_pixmap)
{
gdk_pixmap_unref (connection_pixmap);
connection_pixmap = NULL;
}
connection_pixmap = create_plot (widget,
connection_data);
}
/**
* Part of the connection graph got re-exposed; refresh the area.
*
* @param widget the drawing area of the connection graph
* @param event expose event
* @param data_ptr unused
* @return FALSE
*/
gint
GNUNET_STATISTICS_connection_graph_expose (GtkWidget * widget,
GdkEventExpose * event,
gpointer data_ptr)
{
gdk_draw_pixmap (widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
connection_pixmap,
event->area.x, event->area.y,
event->area.x, event->area.y,
event->area.width, event->area.height);
return FALSE;
}
/**
* The window size was changed, forcing us to re-draw the connection
* graph.
*
* @param widget the drawing area of the connection graph
* @param event configure event
* @param data_ptr unused
* @return TRUE
*/
gint
GNUNET_STATISTICS_connection_graph_configure (GtkWidget * widget,
GdkEventConfigure * event,
gpointer data_ptr)
{
plot_connection_graph (widget);
gdk_draw_pixmap (widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
connection_pixmap,
0, 0,
0, 0,
widget->allocation.width, widget->allocation.height);
return TRUE;
}
/**
* Refresh the 'connections' graphic.
*
* @param cls closure
* @param tc scheduler context
*/
static void
refresh_connections (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
GtkWidget *area;
connection_task = GNUNET_SCHEDULER_add_delayed (refresh_delay,
&refresh_connections,
NULL);
area = GTK_WIDGET (get_object ("GNUNET_STATISTICS_GTK_main_notebook_connectivity_drawingarea"));
plot_connection_graph (area);
gdk_draw_pixmap (area->window,
area->style->fg_gc[GTK_WIDGET_STATE (area)],
connection_pixmap,
0, 0,
0, 0,
area->allocation.width,
area->allocation.height);
}
/**
* Free entries in the value history.
*
* @param cls unused
* @param key unused
* @param value 'struct ValueHistory' to free
* @return GNUNET_OK (continue iteration)
*/
static int
free_history (void *cls,
const GNUNET_HashCode *key,
void *value)
{
struct ValueHistory *vh = value;
GNUNET_free (vh->subsystem);
GNUNET_free (vh->name);
GNUNET_free (vh);
return GNUNET_OK;
}
/**
* Task run on shutdown.
*
* @param cls unused
* @param tc scheduler context, unused
*/
static void
shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
statistics = NULL;
GNUNET_CONTAINER_multihashmap_iterate (stat_map, &free_history, NULL);
GNUNET_CONTAINER_multihashmap_destroy (stat_map);
stat_map = NULL;
}
/**
* Callback invoked if the application is supposed to exit.
*/
void
GNUNET_STATISTICS_GTK_quit_cb (GObject * object, gpointer user_data)
{
GNUNET_GTK_tray_icon_destroy ();
GNUNET_GTK_main_loop_quit (ml);
if (connection_task != GNUNET_SCHEDULER_NO_TASK)
{
GNUNET_SCHEDULER_cancel (connection_task);
connection_task = GNUNET_SCHEDULER_NO_TASK;
}
if (NULL != connection_pixmap)
{
gdk_pixmap_unref (connection_pixmap);
connection_pixmap = NULL;
}
GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
}
/**
* Actual main function run right after GNUnet's scheduler
* is initialized. Initializes up GTK and Glade.
*/
static void
run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
static const struct
{
const char *subsystem;
const char *name;
} moma[] =
{
{ "core", "entries in session map" },
{ NULL, NULL }
};
GtkWidget *main_window;
unsigned int i;
ml = cls;
statistics = GNUNET_STATISTICS_create ("gnunet-statistics-gtk",
GNUNET_GTK_main_loop_get_configuration (ml));
if (NULL == statistics)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to initiate connection with statistics service\n"));
return;
}
stat_map = GNUNET_CONTAINER_multihashmap_create (128);
GNUNET_GTK_set_icon_search_path ();
GNUNET_GTK_setup_nls ();
refresh_delay = GNUNET_TIME_UNIT_SECONDS; /* fixme: make option / cmd-line option */
i = 0;
while (moma[i].subsystem != NULL)
{
monitor (moma[i].subsystem,
moma[i].name);
i++;
}
/* setup main window */
main_window = GTK_WIDGET (get_object ("GNUNET_STATISTICS_GTK_main_window"));
gtk_window_maximize (GTK_WINDOW (main_window));
GNUNET_GTK_tray_icon_create (GTK_WINDOW (main_window),
"gnunet-gtk" /* FIXME: different icon? */ ,
"gnunet-statistics-gtk");
/* FIXME: only schedule this task if the respective tab is open!? */
connection_task = GNUNET_SCHEDULER_add_now (&refresh_connections, NULL);
/* make GUI visible */
if (!tray_only)
{
gtk_widget_show (main_window);
gtk_window_present (GTK_WINDOW (main_window));
}
}
int
main (int argc, char *const *argv)
{
static struct GNUNET_GETOPT_CommandLineOption options[] = {
{'t', "tray", NULL,
gettext_noop ("start in tray mode"), 0,
&GNUNET_GETOPT_set_one, &tray_only},
GNUNET_GETOPT_OPTION_END
};
if (GNUNET_OK !=
GNUNET_GTK_main_loop_start ("gnunet-statistics-gtk",
"GTK GUI for viewing GNUnet statistics", argc,
argv, options,
"gnunet_statistics_gtk_main_window.glade",
&run))
return 1;
return 0;
}
/* end of gnunet-statistics-gtk.c */
|