aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/peers/peers.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/peers/peers.c')
-rw-r--r--src/plugins/peers/peers.c432
1 files changed, 0 insertions, 432 deletions
diff --git a/src/plugins/peers/peers.c b/src/plugins/peers/peers.c
deleted file mode 100644
index 42462c54..00000000
--- a/src/plugins/peers/peers.c
+++ /dev/null
@@ -1,432 +0,0 @@
1/*
2 This file is part of GNUnet.
3 (C) 2007 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/plugins/peers/peers.c
23 * @brief code providing peer information
24 * @author Christian Grothoff
25 */
26
27#include "platform.h"
28#include "gnunetgtk_common.h"
29#include <GNUnet/gnunet_directories.h>
30#include <GNUnet/gnunet_getoption_lib.h>
31#include <GNUnet/gnunet_identity_lib.h>
32#include <GNUnet/gnunet_util.h>
33#include <gtk/gtk.h>
34#include <ctype.h>
35
36#define REFRESH_RATE (15 * GNUNET_CRON_SECONDS)
37
38static struct GNUNET_CronManager *cron;
39
40static struct GNUNET_GE_Context *ectx;
41
42static struct GNUNET_GC_Configuration *cfg;
43
44static GdkPixbuf *green;
45
46static GdkPixbuf *yellow;
47
48static GdkPixbuf *red;
49
50static GdkPixbuf *black;
51
52struct Flags
53{
54 struct Flags *next;
55 char *cc;
56 GdkPixbuf *flag;
57};
58
59static struct Flags *flags;
60
61static int
62collector (void *data,
63 const GNUNET_PeerIdentity * identity,
64 const void *address,
65 unsigned int addr_len,
66 GNUNET_CronTime last_message, unsigned int trust,
67 unsigned int bpmFromPeer)
68{
69 GtkListStore *model = data;
70 GtkTreeIter iter;
71 GNUNET_EncName enc;
72 GdkPixbuf *ledBuf;
73 GdkPixbuf *flagBuf;
74 char *cc;
75 char *dir;
76 char *fn;
77 char *prefix;
78 char *have;
79 char *haddress;
80 char *hostname;
81 GNUNET_CronTime now;
82 unsigned int i;
83 int found;
84 struct Flags *pos;
85
86 GNUNET_hash_to_enc (&identity->hashPubKey, &enc);
87 /* check if same peer is already in model! */
88 found = GNUNET_NO;
89 if (TRUE == gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
90 {
91 do
92 {
93 gtk_tree_model_get (GTK_TREE_MODEL (model),
94 &iter, 0, &haddress, 3, &have, -1);
95 if (have != NULL)
96 {
97 if (0 == strcmp (have, (char *) &enc))
98 {
99 if (strlen (haddress) > 0)
100 {
101 GNUNET_free (have);
102 GNUNET_free (haddress);
103 return GNUNET_OK;
104 }
105 found = GNUNET_YES;
106 }
107 GNUNET_free_non_null (haddress);
108 GNUNET_free (have);
109 }
110 }
111 while ((found == GNUNET_NO) &&
112 (TRUE == gtk_tree_model_iter_next (GTK_TREE_MODEL (model),
113 &iter)));
114 }
115
116 hostname = GNUNET_get_ip_as_string (address, addr_len, GNUNET_YES);
117 if (hostname == NULL)
118 hostname = GNUNET_strdup ("NAT");
119 /* get flag */
120 flagBuf = NULL;
121 ledBuf = NULL;
122 cc = NULL;
123 prefix = GNUNET_strdup (hostname);
124 if (strstr (prefix, " ") != NULL)
125 *strstr (prefix, " ") = '\0';
126 cc = prefix;
127 while (strstr (cc, ".") != NULL)
128 cc = strstr (cc, ".") + 1;
129 if (strstr (hostname, ".") == NULL)
130 cc = NULL;
131 else if ((0 == strcasecmp (cc, "edu")) ||
132 (0 == strcasecmp (cc, "com")) ||
133 (0 == strcasecmp (cc, "net")) ||
134 (0 == strcasecmp (cc, "org")) ||
135 (0 == strcasecmp (cc, "gov")) || (0 == strcasecmp (cc, "mil")))
136 cc = "us";
137 else if (0 == strcasecmp (cc, "uk"))
138 cc = "gb";
139 if ((cc != NULL) && (strlen (cc) > 2))
140 cc = NULL;
141 if (cc != NULL)
142 {
143 pos = flags;
144 while (pos != NULL)
145 {
146 if (0 == strcmp (pos->cc, cc))
147 {
148 flagBuf = pos->flag;
149 break;
150 }
151 pos = pos->next;
152 }
153 if (pos == NULL)
154 {
155 cc = GNUNET_strdup (cc);
156 for (i = 0; i < strlen (cc); i++)
157 cc[i] = tolower (cc[i]);
158 dir = GNUNET_get_installation_path (GNUNET_IPK_SELF_PREFIX);
159 fn = GNUNET_malloc (strlen (dir) + 32);
160 strcpy (fn, dir);
161 strcat (fn,
162 "share"
163 DIR_SEPARATOR_STR "gnunet-gtk"
164 DIR_SEPARATOR_STR "flags" DIR_SEPARATOR_STR);
165 strcat (fn, cc);
166 strcat (fn, ".png");
167 GNUNET_free (dir);
168 flagBuf = gdk_pixbuf_new_from_file (fn, NULL);
169 GNUNET_free (fn);
170 pos = GNUNET_malloc (sizeof (struct Flags));
171 pos->cc = cc;
172 pos->flag = flagBuf;
173 pos->next = flags;
174 flags = pos;
175 }
176 }
177
178 /* get LED */
179 now = GNUNET_get_time ();
180 if (last_message + 150 * GNUNET_CRON_SECONDS > now)
181 ledBuf = green;
182 else if (last_message + 5 * GNUNET_CRON_MINUTES > now)
183 ledBuf = yellow;
184 else if (bpmFromPeer > 0)
185 ledBuf = red;
186 else
187 ledBuf = black;
188
189 /* add to model */
190 if (found == GNUNET_NO)
191 gtk_list_store_append (model, &iter);
192 gtk_list_store_set (model,
193 &iter,
194 0, hostname,
195 1, trust,
196 2, bpmFromPeer,
197 3, (const char *) &enc,
198 4, ledBuf, 5, flagBuf, 6, cc, 7, last_message, -1);
199 GNUNET_free (prefix);
200 GNUNET_free (hostname);
201 return GNUNET_OK;
202}
203
204
205
206/**
207 * Compute an updated peer model.
208 */
209static GtkListStore *
210getPeerModel ()
211{
212 struct GNUNET_ClientServerConnection *sock;
213 GtkListStore *model;
214
215 model = gtk_list_store_new (8, G_TYPE_STRING, /* address */
216 G_TYPE_UINT, /* trust */
217 G_TYPE_UINT, /* bpm */
218 G_TYPE_STRING, /* identity */
219 GDK_TYPE_PIXBUF, /* LED */
220 GDK_TYPE_PIXBUF, /* flag */
221 G_TYPE_STRING, /* country name */
222 G_TYPE_UINT64); /* last time seen */
223 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
224 7, GTK_SORT_DESCENDING);
225 sock = GNUNET_client_connection_create (ectx, cfg);
226 if (sock != NULL)
227 {
228 GNUNET_IDENTITY_request_peer_infos (sock, &collector, model);
229 GNUNET_client_connection_destroy (sock);
230 }
231 return model;
232}
233
234
235/**
236 * Update the model for the peers list.
237 */
238static void *
239updatePeerInfoSafe (void *m)
240{
241 GtkListStore *model = m;
242 GtkTreeModel *old_model;
243 GtkWidget *w;
244 GtkSortType order;
245 gint sort_column;
246
247 w =
248 glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "peersTreeView");
249 old_model = gtk_tree_view_get_model (GTK_TREE_VIEW (w));
250 if (old_model != NULL)
251 {
252 if (TRUE ==
253 gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE (old_model),
254 &sort_column, &order))
255 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
256 sort_column, order);
257 }
258 gtk_tree_view_set_model (GTK_TREE_VIEW (w), GTK_TREE_MODEL (model));
259 gtk_tree_selection_set_mode (gtk_tree_view_get_selection
260 (GTK_TREE_VIEW (w)), GTK_SELECTION_NONE);
261 return NULL;
262}
263
264static void
265updatePeerInfo (void *dummy)
266{
267 GtkListStore *model;
268
269 model = getPeerModel ();
270 GNUNET_GTK_save_call (&updatePeerInfoSafe, model);
271 g_object_unref (G_OBJECT (model));
272}
273
274
275void
276init_peers (struct GNUNET_GE_Context *e, struct GNUNET_GC_Configuration *c)
277{
278 GtkWidget *tab;
279 GtkWidget *peers;
280 GtkCellRenderer *renderer;
281 int col;
282 GtkTreeViewColumn *column;
283 char *dir;
284 char *fn;
285
286 ectx = e;
287 cfg = c;
288 peers =
289 glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "peersTreeView");
290 renderer = gtk_cell_renderer_text_new ();
291 col = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (peers),
292 -1,
293 _("Address"),
294 renderer,
295 "text", 0, NULL);
296 column = gtk_tree_view_get_column (GTK_TREE_VIEW (peers), col - 1);
297 gtk_tree_view_column_set_resizable (column, TRUE);
298 gtk_tree_view_column_set_clickable (column, TRUE);
299 gtk_tree_view_column_set_reorderable (column, TRUE);
300 gtk_tree_view_column_set_sort_column_id (column, 0);
301
302 renderer = gtk_cell_renderer_text_new ();
303 col = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (peers),
304 -1,
305 _("Trust"),
306 renderer,
307 "text", 1, NULL);
308 column = gtk_tree_view_get_column (GTK_TREE_VIEW (peers), col - 1);
309 gtk_tree_view_column_set_resizable (column, TRUE);
310 gtk_tree_view_column_set_clickable (column, TRUE);
311 gtk_tree_view_column_set_reorderable (column, TRUE);
312 gtk_tree_view_column_set_sort_column_id (column, 1);
313
314 renderer = gtk_cell_renderer_text_new ();
315 col = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (peers),
316 -1,
317 _("Bandwidth"),
318 renderer,
319 "text", 2, NULL);
320 column = gtk_tree_view_get_column (GTK_TREE_VIEW (peers), col - 1);
321 gtk_tree_view_column_set_resizable (column, TRUE);
322 gtk_tree_view_column_set_clickable (column, TRUE);
323 gtk_tree_view_column_set_reorderable (column, TRUE);
324 gtk_tree_view_column_set_sort_column_id (column, 2);
325
326
327 renderer = gtk_cell_renderer_pixbuf_new ();
328 col = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (peers),
329 -1,
330 _("Country"),
331 renderer,
332 "pixbuf", 5, NULL);
333 column = gtk_tree_view_get_column (GTK_TREE_VIEW (peers), col - 1);
334 gtk_tree_view_column_set_resizable (column, FALSE);
335 gtk_tree_view_column_set_clickable (column, TRUE);
336 gtk_tree_view_column_set_reorderable (column, TRUE);
337 gtk_tree_view_column_set_sort_column_id (column, 6);
338
339 renderer = gtk_cell_renderer_pixbuf_new ();
340 col = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (peers),
341 -1,
342 _("Status"),
343 renderer,
344 "pixbuf", 4, NULL);
345 column = gtk_tree_view_get_column (GTK_TREE_VIEW (peers), col - 1);
346 gtk_tree_view_column_set_clickable (column, TRUE);
347 gtk_tree_view_column_set_reorderable (column, TRUE);
348 gtk_tree_view_column_set_sort_column_id (column, 7);
349 gtk_tree_view_column_set_resizable (column, FALSE);
350
351 renderer = gtk_cell_renderer_text_new ();
352 col = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (peers),
353 -1,
354 _("Identity"),
355 renderer,
356 "text", 3, NULL);
357 column = gtk_tree_view_get_column (GTK_TREE_VIEW (peers), col - 1);
358 gtk_tree_view_column_set_resizable (column, TRUE);
359 gtk_tree_view_column_set_clickable (column, TRUE);
360 gtk_tree_view_column_set_reorderable (column, TRUE);
361 gtk_tree_view_column_set_sort_column_id (column, 3);
362
363
364 dir = GNUNET_get_installation_path (GNUNET_IPK_SELF_PREFIX);
365 fn = GNUNET_malloc (strlen (dir) + 32);
366 strcpy (fn, dir);
367 strcat (fn,
368 "share"
369 DIR_SEPARATOR_STR "gnunet-gtk" DIR_SEPARATOR_STR "red.png");
370 red = gdk_pixbuf_new_from_file (fn, NULL);
371
372 strcpy (fn, dir);
373 strcat (fn,
374 "share"
375 DIR_SEPARATOR_STR "gnunet-gtk" DIR_SEPARATOR_STR "yellow.png");
376 yellow = gdk_pixbuf_new_from_file (fn, NULL);
377
378 strcpy (fn, dir);
379 strcat (fn,
380 "share"
381 DIR_SEPARATOR_STR "gnunet-gtk" DIR_SEPARATOR_STR "green.png");
382 green = gdk_pixbuf_new_from_file (fn, NULL);
383
384 strcpy (fn, dir);
385 strcat (fn,
386 "share"
387 DIR_SEPARATOR_STR "gnunet-gtk" DIR_SEPARATOR_STR "black.png");
388 black = gdk_pixbuf_new_from_file (fn, NULL);
389 GNUNET_free (fn);
390 GNUNET_free (dir);
391
392 tab =
393 glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
394 "peersScrolledWindow");
395 gtk_widget_show (tab);
396 cron = GNUNET_GTK_get_cron_manager ();
397 GNUNET_cron_add_job (cron, &updatePeerInfo, REFRESH_RATE, REFRESH_RATE,
398 NULL);
399}
400
401void
402done_peers ()
403{
404 GtkWidget *w;
405 struct Flags *pos;
406
407 GNUNET_cron_del_job (cron, &updatePeerInfo, REFRESH_RATE, NULL);
408 if (red != NULL)
409 g_object_unref (red);
410 if (green != NULL)
411 g_object_unref (green);
412 if (black != NULL)
413 g_object_unref (black);
414 if (yellow != NULL)
415 g_object_unref (yellow);
416 w =
417 glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "peersTreeView");
418 gtk_tree_view_set_model (GTK_TREE_VIEW (w), NULL);
419
420 while (flags != NULL)
421 {
422 pos = flags->next;
423 if (flags->flag != NULL)
424 g_object_unref (flags->flag);
425 GNUNET_free (flags->cc);
426 GNUNET_free (flags);
427 flags = pos;
428 }
429}
430
431
432/* end of peers.c */