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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/*
This file is part of GNUnet.
(C) 2010, 2012 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/peerinfo/gnunet-peerinfo-gtk.c
* @brief Main function of gnunet-peerinfo-gtk
* @author Christian Grothoff
*/
#include "gnunet_gtk.h"
#include <gnunet/gnunet_peerinfo_service.h>
#include <gnunet/gnunet_transport_service.h>
#include <gnunet/gnunet_ats_service.h>
#include "gnunet-peerinfo-gtk-flags.h"
/**
* Columns in the peerinfo model.
*/
enum PEERINFO_ModelColumns
{
/**
* A gchararray
*/
PEERINFO_MC_PEER_IDENTITY_STRING = 0,
/**
* A guint
*/
PEERINFO_MC_NUMBER_OF_ADDRESSES = 1,
/**
* A gchararray
*/
PEERINFO_MC_COUNTRY_NAME = 2,
/**
* A GdkPixbuf
*/
PEERINFO_MC_COUNTRY_FLAG = 3,
/**
* A guint64
*/
PEERINFO_MC_BANDWIDTH_IN = 4,
/**
* A guint64
*/
PEERINFO_MC_BANDWIDTH_OUT = 5,
/**
* A gchararray
*/
PEERINFO_MC_ADDRESS_AS_STRING = 6,
/**
* A GdkPixbuf
*/
PEERINFO_MC_CONNECTIVITY_LED = 7,
/**
* A gboolean
*/
PEERINFO_MC_CONNECTED_STATUS = 8
};
/**
* Information we track for each peer outside of the model.
*/
struct PeerInfo
{
/**
* Reference to the peer in the view.
*/
GtkTreeRowReference *rr;
/**
* Handle to an active lookup for addresses of this peer, or NULL.
*/
struct GNUNET_TRANSPORT_PeerIterateContext *palc;
/**
* Handle for address to string conversion.
*/
struct GNUNET_TRANSPORT_AddressToStringContext *tos;
/**
* Did we get any address?
*/
int got_address;
};
/**
* Handle to our main loop.
*/
static struct GNUNET_GTK_MainLoop *ml;
/**
* Handle for our notifications from peerinfo about new peers.
*/
static struct GNUNET_PEERINFO_NotifyContext *pnc;
/**
* Handle to ATS service.
*/
static struct GNUNET_ATS_PerformanceHandle *ats;
/**
* Map of peer identities to the respective PeerInfo for our view.
*/
static struct GNUNET_CONTAINER_MultiHashMap *peer2info;
/**
* Should gnunet-peerinfo-gtk start in tray mode?
*/
static int tray_only;
/**
* Green status led (connected)
*/
static GdkPixbuf *led_green;
/**
* Red status led (disconnected)
*/
static GdkPixbuf *led_red;
/**
* Main window list store.
*/
static GtkListStore *ls;
/**
* Get cfg.
*/
static const struct GNUNET_CONFIGURATION_Handle *
get_configuration ()
{
return GNUNET_GTK_main_loop_get_configuration (ml);
}
/**
* 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);
}
/**
* Function called on each entry in the 'peer2info' map
* to free the associated path.
*
* @param cls unused
* @param key peer identity
* @param value the 'struct PeerInfo'
* @return GNUNET_OK (continue to iterate)
*/
static int
free_paths (void *cls, const struct GNUNET_HashCode * key, void *value)
{
struct PeerInfo *info = value;
if (NULL != info->palc)
{
GNUNET_TRANSPORT_peer_get_active_addresses_cancel (info->palc);
info->palc = NULL;
}
if (NULL != info->tos)
{
GNUNET_TRANSPORT_address_to_string_cancel (info->tos);
info->tos = NULL;
}
gtk_tree_row_reference_free (info->rr);
GNUNET_free (info);
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_GTK_tray_icon_destroy ();
GNUNET_GTK_main_loop_quit (ml);
ml = NULL;
if (NULL != pnc)
{
GNUNET_PEERINFO_notify_cancel (pnc);
pnc = NULL;
}
if (NULL != ats)
{
GNUNET_ATS_performance_done (ats);
ats = NULL;
}
GNUNET_CONTAINER_multihashmap_iterate (peer2info, &free_paths, NULL);
GNUNET_CONTAINER_multihashmap_destroy (peer2info);
peer2info = NULL;
GNUNET_PEERINFO_GTK_flags_shutdown ();
}
/**
* Function to call with the text format of an address
*
* @param cls the 'struct PeerInfo' for which this is a valid address
* @param address address as a string, NULL on error
*/
static void
peer_address_string_cb (void *cls, const char *address)
{
struct PeerInfo *info = cls;
GtkTreeIter iter;
GtkTreePath *path;
char *country;
const char *colon;
const char *dot;
path = gtk_tree_row_reference_get_path (info->rr);
GNUNET_assert (NULL != path);
GNUNET_assert (TRUE == gtk_tree_model_get_iter (GTK_TREE_MODEL (ls), &iter, path));
gtk_tree_path_free (path);
if (NULL == address)
{
/* error */
if (GNUNET_NO == info->got_address)
gtk_list_store_set (ls, &iter,
PEERINFO_MC_NUMBER_OF_ADDRESSES, (guint) 1,
PEERINFO_MC_COUNTRY_NAME, NULL,
PEERINFO_MC_COUNTRY_FLAG, NULL,
PEERINFO_MC_ADDRESS_AS_STRING, "<no address>",
PEERINFO_MC_CONNECTIVITY_LED, led_green,
PEERINFO_MC_CONNECTED_STATUS, TRUE,
-1);
info->tos = NULL;
return;
}
/* last address, store information in model */
country = NULL;
colon = strstr (address, ":");
if (NULL != colon)
{
for (dot = colon - 1; dot != address; dot--)
if ('.' == *dot)
break;
if ('.' == *dot)
country = GNUNET_strndup (&dot[1], (colon - dot) - 1);
}
gtk_list_store_set (ls, &iter,
PEERINFO_MC_NUMBER_OF_ADDRESSES, 1,
PEERINFO_MC_COUNTRY_NAME, country,
PEERINFO_MC_COUNTRY_FLAG, GNUNET_PEERINFO_GTK_get_flag (country),
PEERINFO_MC_ADDRESS_AS_STRING, address,
PEERINFO_MC_CONNECTIVITY_LED, led_green,
PEERINFO_MC_CONNECTED_STATUS, TRUE,
-1);
GNUNET_free_non_null (country);
info->got_address = GNUNET_YES;
}
/**
* Function to call with a binary format of an address
*
* @param cls the 'struct PeerInfo' for which this is a valid address
* @param peer peer the update is about
* @param address NULL on disconnect, otherwise 0-terminated printable UTF-8 string
*/
static void
peer_address_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
const struct GNUNET_HELLO_Address *address)
{
struct PeerInfo *info = cls;
GtkTreeIter iter;
GtkTreePath *path;
path = gtk_tree_row_reference_get_path (info->rr);
GNUNET_assert (NULL != path);
GNUNET_assert (TRUE == gtk_tree_model_get_iter (GTK_TREE_MODEL (ls), &iter, path));
gtk_tree_path_free (path);
if (NULL == address)
{
/* disconnect */
gtk_list_store_set (ls, &iter,
PEERINFO_MC_NUMBER_OF_ADDRESSES, (guint) 0,
PEERINFO_MC_CONNECTIVITY_LED, led_red,
PEERINFO_MC_CONNECTED_STATUS, FALSE,
-1);
return;
}
gtk_list_store_set (ls, &iter,
PEERINFO_MC_NUMBER_OF_ADDRESSES, 1,
PEERINFO_MC_CONNECTIVITY_LED, led_green,
PEERINFO_MC_CONNECTED_STATUS, TRUE,
-1);
if (NULL != info->tos)
GNUNET_TRANSPORT_address_to_string_cancel (info->tos);
info->got_address = GNUNET_NO;
info->tos =
GNUNET_TRANSPORT_address_to_string (get_configuration (), address,
GNUNET_NO,
GNUNET_TIME_UNIT_FOREVER_REL,
&peer_address_string_cb, info);
}
/**
* Function called for peers that we know about.
*
* @param cls closure
* @param peer id of the peer, NULL for last call
* @param hello hello message for the peer (can be NULL)
* @param err_msg NULL if successful, otherwise contains error message
*/
static void
peerinfo_processor (void *cls, const struct GNUNET_PeerIdentity *peer,
const struct GNUNET_HELLO_Message *hello,
const char *err_msg)
{
GtkTreeIter iter;
char *npid;
struct GNUNET_CRYPTO_HashAsciiEncoded enc;
struct PeerInfo *info;
GtkTreePath *path;
info = GNUNET_CONTAINER_multihashmap_get (peer2info, &peer->hashPubKey);
if (NULL == info)
{
GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
npid = (char *) &enc;
npid[4] = '\0';
gtk_list_store_append (ls, &iter);
gtk_list_store_set (ls, &iter,
PEERINFO_MC_PEER_IDENTITY_STRING, npid,
PEERINFO_MC_NUMBER_OF_ADDRESSES, (guint) 0,
PEERINFO_MC_COUNTRY_NAME, "",
PEERINFO_MC_COUNTRY_FLAG, NULL,
PEERINFO_MC_BANDWIDTH_IN, (guint64) 0,
PEERINFO_MC_BANDWIDTH_OUT, (guint64) 0,
PEERINFO_MC_ADDRESS_AS_STRING, "",
PEERINFO_MC_CONNECTIVITY_LED, led_red,
PEERINFO_MC_CONNECTED_STATUS, FALSE,
-1);
path = gtk_tree_model_get_path (GTK_TREE_MODEL (ls), &iter);
info = GNUNET_malloc (sizeof (struct PeerInfo));
info->rr = gtk_tree_row_reference_new (GTK_TREE_MODEL (ls), path);
GNUNET_assert (NULL != info->rr);
gtk_tree_path_free (path);
GNUNET_CONTAINER_multihashmap_put (peer2info, &peer->hashPubKey, info,
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
}
if (NULL == info->palc)
{
info->palc =
GNUNET_TRANSPORT_peer_get_active_addresses (get_configuration (), peer,
GNUNET_NO,
GNUNET_TIME_UNIT_FOREVER_REL,
&peer_address_cb, info);
}
}
/**
* Method called whenever a given peer has a QoS status change.
*
* @param cls closure
* @param address the address
* @param address_active is this address actively used to maintain a connection
* to a peer
* @param bandwidth_in available amount of inbound bandwidth
* @param bandwidth_out available amount of outbound bandwidth
* @param ats performance data for the address (as far as known)
* @param ats_count number of performance records in 'ats'
*/
static void
status_cb (void *cls,
const struct GNUNET_HELLO_Address *address,
int address_active,
struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
const struct GNUNET_ATS_Information *ats,
uint32_t ats_count)
{
struct PeerInfo *info;
GtkTreeIter iter;
GtkTreePath *path;
if (GNUNET_YES != address_active)
return;
info = GNUNET_CONTAINER_multihashmap_get (peer2info, &address->peer.hashPubKey);
if (NULL == info)
return; /* should rarely happen... */
path = gtk_tree_row_reference_get_path (info->rr);
GNUNET_assert (NULL != path);
GNUNET_assert (TRUE == gtk_tree_model_get_iter (GTK_TREE_MODEL (ls), &iter, path));
gtk_tree_path_free (path);
gtk_list_store_set (ls, &iter,
PEERINFO_MC_BANDWIDTH_IN, (guint64) ntohl (bandwidth_in.value__),
PEERINFO_MC_BANDWIDTH_OUT, (guint64) ntohl (bandwidth_out.value__),
-1);
}
/**
* Callback invoked if the application is supposed to exit.
*
* @param object
* @param user_data unused
*/
void
GNUNET_PEERINFO_GTK_quit_cb (GObject * object, gpointer user_data)
{
GNUNET_SCHEDULER_shutdown ();
}
/**
* Load LED image from resource file.
*
* @param color color of the LED to load
* @return the image as a GdkPixbuf
*/
static GdkPixbuf *
load_led (const char *color)
{
GdkPixbuf *pixbuf;
char *dir;
char *fn;
dir = GNUNET_GTK_installation_get_path (GNUNET_OS_IPK_DATADIR);
GNUNET_asprintf (&fn, "%s%s.png", dir,
color);
GNUNET_free (dir);
pixbuf = gdk_pixbuf_new_from_file (fn, NULL);
GNUNET_free (fn);
return pixbuf;
}
/**
* Actual main function run right after GNUnet's scheduler
* is initialized. Initializes up GTK and Glade.
*
* @param cls NULL
* @param tc schedule context
*/
static void
run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
GtkWidget *main_window;
ml = cls;
if (GNUNET_OK != GNUNET_GTK_main_loop_build_window (ml, NULL))
{
return;
}
led_green = load_led ("green");
led_red = load_led ("red");
GNUNET_GTK_set_icon_search_path ();
GNUNET_GTK_setup_nls ();
peer2info = GNUNET_CONTAINER_multihashmap_create (256, GNUNET_NO);
pnc =
GNUNET_PEERINFO_notify (get_configuration (),
GNUNET_NO,
&peerinfo_processor, NULL);
if (NULL == pnc)
{
fprintf (stderr,
_("Failed to initialize communication with peerinfo service!\n"));
GNUNET_SCHEDULER_shutdown ();
return;
}
ats = GNUNET_ATS_performance_init (get_configuration (), &status_cb, NULL);
/* setup main window */
main_window = GTK_WIDGET (get_object ("GNUNET_PEERINFO_GTK_main_window"));
ls = GTK_LIST_STORE (get_object ("GNUNET_PEERINFO_GTK_list_store"));
GNUNET_assert (NULL != ls);
gtk_window_maximize (GTK_WINDOW (main_window));
GNUNET_GTK_tray_icon_create (ml,
GTK_WINDOW (main_window),
"gnunet-gtk" /* FIXME: different icon? */ ,
"gnunet-peerinfo-gtk");
/* make GUI visible */
if (!tray_only)
{
gtk_widget_show (main_window);
gtk_window_present (GTK_WINDOW (main_window));
}
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
&shutdown_task, NULL);
}
/**
* Main function of gnunet-peerinfo-gtk.
*
* @param argc number of arguments
* @param argv arguments
* @return 0 on success
*/
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-peerinfo-gtk",
"GTK GUI for inspecting GNUnet Peers", argc,
argv, options,
"gnunet_peerinfo_gtk_main_window.glade",
&run))
return 1;
return 0;
}
/* end of gnunet-peerinfo-gtk.c */
|