diff options
Diffstat (limited to 'src/plugins/peers/peers.c')
-rw-r--r-- | src/plugins/peers/peers.c | 381 |
1 files changed, 381 insertions, 0 deletions
diff --git a/src/plugins/peers/peers.c b/src/plugins/peers/peers.c new file mode 100644 index 00000000..d9c70248 --- /dev/null +++ b/src/plugins/peers/peers.c | |||
@@ -0,0 +1,381 @@ | |||
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_config_impl.h> | ||
33 | #include <GNUnet/gnunet_util_cron.h> | ||
34 | #include <GNUnet/gnunet_util_network_client.h> | ||
35 | #include <gtk/gtk.h> | ||
36 | |||
37 | static struct CronManager * cron; | ||
38 | |||
39 | static struct GE_Context * ectx; | ||
40 | |||
41 | static struct GC_Configuration * cfg; | ||
42 | |||
43 | static GdkPixbuf * green; | ||
44 | |||
45 | static GdkPixbuf * yellow; | ||
46 | |||
47 | static GdkPixbuf * red; | ||
48 | |||
49 | static GdkPixbuf * black; | ||
50 | |||
51 | static int collector(void * data, | ||
52 | const PeerIdentity * identity, | ||
53 | const char * address, | ||
54 | cron_t last_message, | ||
55 | unsigned int trust, | ||
56 | unsigned int bpmFromPeer) { | ||
57 | GtkListStore * model = data; | ||
58 | GtkTreeIter iter; | ||
59 | EncName enc; | ||
60 | GdkPixbuf * ledBuf; | ||
61 | GdkPixbuf * flagBuf; | ||
62 | char * cc; | ||
63 | char * dir; | ||
64 | char * fn; | ||
65 | char * prefix; | ||
66 | char * have; | ||
67 | cron_t now; | ||
68 | |||
69 | hash2enc(&identity->hashPubKey, | ||
70 | &enc); | ||
71 | /* check if same peer is already in model! */ | ||
72 | if (TRUE == gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), | ||
73 | &iter)) { | ||
74 | do { | ||
75 | gtk_tree_model_get(GTK_TREE_MODEL(model), | ||
76 | &iter, | ||
77 | 3, &have, | ||
78 | -1); | ||
79 | if (have != NULL) { | ||
80 | if (0 == strcmp(have, (char*) &enc)) { | ||
81 | FREE(have); | ||
82 | return OK; | ||
83 | } | ||
84 | FREE(have); | ||
85 | } | ||
86 | } while (TRUE == gtk_tree_model_iter_next(GTK_TREE_MODEL(model), | ||
87 | &iter)); | ||
88 | } | ||
89 | |||
90 | |||
91 | /* get flag */ | ||
92 | flagBuf = NULL; | ||
93 | ledBuf = NULL; | ||
94 | cc = NULL; | ||
95 | if (address != NULL) { | ||
96 | prefix = STRDUP(address); | ||
97 | if (strstr(prefix, " ") != NULL) | ||
98 | *strstr(prefix, " ") = '\0'; | ||
99 | cc = prefix; | ||
100 | while (strstr(cc, ".") != NULL) | ||
101 | cc = strstr(cc, ".") + 1; | ||
102 | if (strstr(address, ".") == NULL) | ||
103 | cc = NULL; | ||
104 | else if ( (0 == strcmp(cc, "edu")) || | ||
105 | (0 == strcmp(cc, "com")) || | ||
106 | (0 == strcmp(cc, "net")) || | ||
107 | (0 == strcmp(cc, "org")) || | ||
108 | (0 == strcmp(cc, "gov")) || | ||
109 | (0 == strcmp(cc, "mil")) ) | ||
110 | cc = "us"; | ||
111 | if (strlen(cc) > 2) | ||
112 | cc = NULL; | ||
113 | if (cc != NULL) { | ||
114 | cc = STRDUP(cc); | ||
115 | dir = os_get_installation_path(IPK_DATADIR); | ||
116 | fn = MALLOC(strlen(dir) + 32); | ||
117 | strcpy(fn, dir); | ||
118 | strcat(fn, | ||
119 | DIR_SEPARATOR_STR ".." | ||
120 | DIR_SEPARATOR_STR "gnunet-gtk" | ||
121 | DIR_SEPARATOR_STR "flags" | ||
122 | DIR_SEPARATOR_STR); | ||
123 | strcat(fn, cc); | ||
124 | strcat(fn, ".png"); | ||
125 | FREE(dir); | ||
126 | flagBuf = gdk_pixbuf_new_from_file(fn, NULL); | ||
127 | FREE(fn); | ||
128 | } | ||
129 | FREE(prefix); | ||
130 | } | ||
131 | |||
132 | /* get LED */ | ||
133 | now = get_time(); | ||
134 | if (last_message + 150 * cronSECONDS > now) | ||
135 | ledBuf = green; | ||
136 | else if (last_message + 5 * cronMINUTES > now) | ||
137 | ledBuf = yellow; | ||
138 | else if (bpmFromPeer > 0) | ||
139 | ledBuf = red; | ||
140 | else | ||
141 | ledBuf = black; | ||
142 | if (ledBuf != NULL) | ||
143 | g_object_ref(ledBuf); | ||
144 | |||
145 | /* add to model */ | ||
146 | gtk_list_store_append(model, | ||
147 | &iter); | ||
148 | gtk_list_store_set(model, | ||
149 | &iter, | ||
150 | 0, address, | ||
151 | 1, trust, | ||
152 | 2, bpmFromPeer, | ||
153 | 3, (const char*) &enc, | ||
154 | 4, ledBuf, | ||
155 | 5, flagBuf, | ||
156 | 6, cc, | ||
157 | 7, last_message, | ||
158 | -1); | ||
159 | FREENONNULL(cc); | ||
160 | return OK; | ||
161 | } | ||
162 | |||
163 | |||
164 | |||
165 | /** | ||
166 | * Compute an updated peer model. | ||
167 | */ | ||
168 | static void * getPeerModel() { | ||
169 | struct ClientServerConnection * sock; | ||
170 | GtkListStore * model; | ||
171 | |||
172 | model = gtk_list_store_new(8, | ||
173 | G_TYPE_STRING, /* address */ | ||
174 | G_TYPE_UINT, /* trust */ | ||
175 | G_TYPE_UINT, /* bpm */ | ||
176 | G_TYPE_STRING, /* identity */ | ||
177 | GDK_TYPE_PIXBUF, /* LED */ | ||
178 | GDK_TYPE_PIXBUF, /* flag */ | ||
179 | G_TYPE_STRING, /* country name */ | ||
180 | G_TYPE_UINT64); /* last time seen */ | ||
181 | sock = client_connection_create(ectx, cfg); | ||
182 | if (sock != NULL) { | ||
183 | gnunet_identity_request_peer_infos(sock, | ||
184 | &collector, | ||
185 | model); | ||
186 | connection_destroy(sock); | ||
187 | } | ||
188 | return model; | ||
189 | } | ||
190 | |||
191 | |||
192 | /** | ||
193 | * Update the model for the peers list. | ||
194 | */ | ||
195 | static void * updatePeerInfoSafe(void * m) { | ||
196 | GtkListStore * model = m; | ||
197 | GtkWidget * w; | ||
198 | |||
199 | w = glade_xml_get_widget(getMainXML(), | ||
200 | "peersTreeView"); | ||
201 | gtk_tree_view_set_model(GTK_TREE_VIEW(w), | ||
202 | GTK_TREE_MODEL(model)); | ||
203 | gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(w)), | ||
204 | GTK_SELECTION_NONE); | ||
205 | return NULL; | ||
206 | } | ||
207 | |||
208 | static void updatePeerInfo(void * dummy) { | ||
209 | GtkListStore * model; | ||
210 | |||
211 | model = getPeerModel(); | ||
212 | gtkSaveCall(&updatePeerInfoSafe, model); | ||
213 | } | ||
214 | |||
215 | |||
216 | void init_peers(struct GE_Context * e, | ||
217 | struct GC_Configuration * c) { | ||
218 | GtkWidget * tab; | ||
219 | GtkWidget * peers; | ||
220 | GtkCellRenderer * renderer; | ||
221 | int col; | ||
222 | GtkTreeViewColumn * column; | ||
223 | char * dir; | ||
224 | char * fn; | ||
225 | |||
226 | ectx = e; | ||
227 | cfg = c; | ||
228 | peers | ||
229 | = glade_xml_get_widget(getMainXML(), | ||
230 | "peersTreeView"); | ||
231 | renderer = gtk_cell_renderer_text_new(); | ||
232 | col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(peers), | ||
233 | -1, | ||
234 | _("Address"), | ||
235 | renderer, | ||
236 | "text", 0, | ||
237 | NULL); | ||
238 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(peers), | ||
239 | col - 1); | ||
240 | gtk_tree_view_column_set_resizable(column, TRUE); | ||
241 | gtk_tree_view_column_set_clickable(column, TRUE); | ||
242 | gtk_tree_view_column_set_reorderable(column, TRUE); | ||
243 | gtk_tree_view_column_set_sort_column_id(column, 0); | ||
244 | |||
245 | renderer = gtk_cell_renderer_text_new(); | ||
246 | col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(peers), | ||
247 | -1, | ||
248 | _("Trust"), | ||
249 | renderer, | ||
250 | "text", 1, | ||
251 | NULL); | ||
252 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(peers), | ||
253 | col - 1); | ||
254 | gtk_tree_view_column_set_resizable(column, TRUE); | ||
255 | gtk_tree_view_column_set_clickable(column, TRUE); | ||
256 | gtk_tree_view_column_set_reorderable(column, TRUE); | ||
257 | gtk_tree_view_column_set_sort_column_id(column, 1); | ||
258 | |||
259 | renderer = gtk_cell_renderer_text_new(); | ||
260 | col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(peers), | ||
261 | -1, | ||
262 | _("Bandwidth"), | ||
263 | renderer, | ||
264 | "text", 2, | ||
265 | NULL); | ||
266 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(peers), | ||
267 | col - 1); | ||
268 | gtk_tree_view_column_set_resizable(column, TRUE); | ||
269 | gtk_tree_view_column_set_clickable(column, TRUE); | ||
270 | gtk_tree_view_column_set_reorderable(column, TRUE); | ||
271 | gtk_tree_view_column_set_sort_column_id(column, 2); | ||
272 | |||
273 | |||
274 | renderer = gtk_cell_renderer_pixbuf_new(); | ||
275 | col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(peers), | ||
276 | -1, | ||
277 | _("Country"), | ||
278 | renderer, | ||
279 | "pixbuf", 5, | ||
280 | NULL); | ||
281 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(peers), | ||
282 | col - 1); | ||
283 | gtk_tree_view_column_set_resizable(column, FALSE); | ||
284 | gtk_tree_view_column_set_clickable(column, TRUE); | ||
285 | gtk_tree_view_column_set_reorderable(column, TRUE); | ||
286 | gtk_tree_view_column_set_sort_column_id(column, 6); | ||
287 | |||
288 | renderer = gtk_cell_renderer_pixbuf_new(); | ||
289 | col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(peers), | ||
290 | -1, | ||
291 | _("Status"), | ||
292 | renderer, | ||
293 | "pixbuf", 4, | ||
294 | NULL); | ||
295 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(peers), | ||
296 | col - 1); | ||
297 | gtk_tree_view_column_set_clickable(column, TRUE); | ||
298 | gtk_tree_view_column_set_reorderable(column, TRUE); | ||
299 | gtk_tree_view_column_set_sort_column_id(column, 7); | ||
300 | gtk_tree_view_column_set_resizable(column, FALSE); | ||
301 | |||
302 | renderer = gtk_cell_renderer_text_new(); | ||
303 | col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(peers), | ||
304 | -1, | ||
305 | _("Identity"), | ||
306 | renderer, | ||
307 | "text", 3, | ||
308 | NULL); | ||
309 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(peers), | ||
310 | col - 1); | ||
311 | gtk_tree_view_column_set_resizable(column, TRUE); | ||
312 | gtk_tree_view_column_set_clickable(column, TRUE); | ||
313 | gtk_tree_view_column_set_reorderable(column, TRUE); | ||
314 | gtk_tree_view_column_set_sort_column_id(column, 3); | ||
315 | |||
316 | |||
317 | dir = os_get_installation_path(IPK_DATADIR); | ||
318 | fn = MALLOC(strlen(dir) + 32); | ||
319 | strcpy(fn, dir); | ||
320 | strcat(fn, | ||
321 | DIR_SEPARATOR_STR ".." | ||
322 | DIR_SEPARATOR_STR "gnunet-gtk" | ||
323 | DIR_SEPARATOR_STR "red.png"); | ||
324 | red = gdk_pixbuf_new_from_file(fn, NULL); | ||
325 | |||
326 | strcpy(fn, dir); | ||
327 | strcat(fn, | ||
328 | DIR_SEPARATOR_STR ".." | ||
329 | DIR_SEPARATOR_STR "gnunet-gtk" | ||
330 | DIR_SEPARATOR_STR "yellow.png"); | ||
331 | yellow = gdk_pixbuf_new_from_file(fn, NULL); | ||
332 | |||
333 | strcpy(fn, dir); | ||
334 | strcat(fn, | ||
335 | DIR_SEPARATOR_STR ".." | ||
336 | DIR_SEPARATOR_STR "gnunet-gtk" | ||
337 | DIR_SEPARATOR_STR "green.png"); | ||
338 | green = gdk_pixbuf_new_from_file(fn, NULL); | ||
339 | |||
340 | strcpy(fn, dir); | ||
341 | strcat(fn, | ||
342 | DIR_SEPARATOR_STR ".." | ||
343 | DIR_SEPARATOR_STR "gnunet-gtk" | ||
344 | DIR_SEPARATOR_STR "black.png"); | ||
345 | black = gdk_pixbuf_new_from_file(fn, NULL); | ||
346 | FREE(fn); | ||
347 | FREE(dir); | ||
348 | |||
349 | updatePeerInfo(NULL); | ||
350 | tab | ||
351 | = glade_xml_get_widget(getMainXML(), | ||
352 | "peersScrolledWindow"); | ||
353 | gtk_widget_show(tab); | ||
354 | cron = cron_create(ectx); | ||
355 | cron_add_job(cron, | ||
356 | &updatePeerInfo, | ||
357 | 15 * cronSECONDS, | ||
358 | 15 * cronSECONDS, | ||
359 | NULL); | ||
360 | cron_start(cron); | ||
361 | } | ||
362 | |||
363 | void done_peers() { | ||
364 | cron_stop(cron); | ||
365 | cron_del_job(cron, | ||
366 | &updatePeerInfo, | ||
367 | 15 * cronSECONDS, | ||
368 | NULL); | ||
369 | cron_destroy(cron); | ||
370 | if (red != NULL) | ||
371 | g_object_unref(red); | ||
372 | if (green != NULL) | ||
373 | g_object_unref(green); | ||
374 | if (black != NULL) | ||
375 | g_object_unref(black); | ||
376 | if (yellow != NULL) | ||
377 | g_object_unref(yellow); | ||
378 | } | ||
379 | |||
380 | |||
381 | /* end of peers.c */ | ||