2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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 3, or (at your
8 option) any later version.
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.
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.
22 * @file experimentation/gnunet-daemon-experimentation_nodes.c
23 * @brief experimentation daemon: node management
24 * @author Christian Grothoff
25 * @author Matthias Wachs
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet-daemon-experimentation.h"
34 static struct GNUNET_CORE_Handle *ch;
36 static struct GNUNET_PeerIdentity me;
39 * Nodes with a pending request
42 struct GNUNET_CONTAINER_MultiHashMap *nodes_requested;
45 * Active experimentation nodes
47 struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
50 * Inactive experimentation nodes
51 * To be excluded from future requests
53 struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
56 static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
58 GNUNET_assert (NULL != m);
59 GNUNET_assert (NULL != GSE_stats);
61 if (m == nodes_active)
63 GNUNET_STATISTICS_set (GSE_stats, "# nodes active",
64 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
66 else if (m == nodes_inactive)
68 GNUNET_STATISTICS_set (GSE_stats, "# nodes inactive",
69 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
71 else if (m == nodes_requested)
73 GNUNET_STATISTICS_set (GSE_stats, "# nodes requested",
74 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
82 cleanup_nodes (void *cls,
83 const struct GNUNET_HashCode * key,
87 struct GNUNET_CONTAINER_MultiHashMap *cur = cls;
90 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
92 GNUNET_SCHEDULER_cancel (n->timeout_task);
93 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
97 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
102 GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
108 static int is_me (const struct GNUNET_PeerIdentity *id)
110 if (0 == memcmp (&me, id, sizeof (me)))
117 core_startup_handler (void *cls,
118 struct GNUNET_CORE_Handle * server,
119 const struct GNUNET_PeerIdentity *my_identity)
125 remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
127 struct Node *n = cls;
129 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Removing request for peer %s due to timeout\n"),
130 GNUNET_i2s (&n->id));
132 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &n->id.hashPubKey))
134 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n);
135 update_stats (nodes_requested);
136 GNUNET_CONTAINER_multihashmap_put (nodes_inactive, &n->id.hashPubKey, n,
137 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
138 update_stats (nodes_inactive);
141 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
144 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
149 size_t send_request_cb (void *cls, size_t bufsize, void *buf)
151 struct Node *n = cls;
152 struct Experimentation_Request msg;
153 size_t size = sizeof (msg);
158 /* client disconnected */
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
160 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
161 GNUNET_SCHEDULER_cancel (n->timeout_task);
162 GNUNET_SCHEDULER_add_now (&remove_request, n);
165 GNUNET_assert (bufsize >= size);
167 msg.msg.size = htons (size);
168 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
169 memcpy (buf, &msg, size);
171 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
172 GNUNET_i2s (&n->id));
176 static void send_request (const struct GNUNET_PeerIdentity *peer)
181 size = sizeof (struct Experimentation_Request);
182 n = GNUNET_malloc (sizeof (struct Node));
184 n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
185 n->cth = GNUNET_CORE_notify_transmit_ready(ch, GNUNET_NO, 0,
186 GNUNET_TIME_relative_get_forever_(),
187 peer, size, send_request_cb, n);
189 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
190 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
192 update_stats (nodes_requested);
195 size_t send_response_cb (void *cls, size_t bufsize, void *buf)
197 struct Node *n = cls;
198 struct Experimentation_Response msg;
199 size_t size = sizeof (msg);
204 /* client disconnected */
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
208 GNUNET_assert (bufsize >= size);
210 msg.msg.size = htons (size);
211 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
212 memcpy (buf, &msg, size);
214 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending response to peer %s\n"),
215 GNUNET_i2s (&n->id));
220 static void handle_request (const struct GNUNET_PeerIdentity *peer)
224 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
226 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
227 "REQUEST", "active", GNUNET_i2s (peer));
229 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
231 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
232 "REQUEST", "requested", GNUNET_i2s (peer));
233 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
234 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
236 GNUNET_SCHEDULER_cancel (n->timeout_task);
237 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
241 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
244 update_stats (nodes_requested);
245 GNUNET_CONTAINER_multihashmap_put (nodes_active,
246 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
247 update_stats (nodes_active);
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
251 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
254 "REQUEST", "inactive", GNUNET_i2s (peer));
255 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
256 update_stats (nodes_inactive);
257 GNUNET_CONTAINER_multihashmap_put (nodes_active,
258 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
259 update_stats (nodes_active);
260 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
265 /* Create new node */
266 n = GNUNET_malloc (sizeof (struct Node));
268 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
269 "REQUEST", "new", GNUNET_i2s (peer));
270 GNUNET_CONTAINER_multihashmap_put (nodes_active,
271 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
272 update_stats (nodes_active);
273 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
277 n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0,
278 GNUNET_TIME_relative_get_forever_(),
279 peer, sizeof (struct Experimentation_Response),
280 send_response_cb, n);
283 static void handle_response (const struct GNUNET_PeerIdentity *peer)
287 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
289 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
290 "RESPONSE", "active", GNUNET_i2s (peer));
292 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
294 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
295 "RESPONSE", "requested", GNUNET_i2s (peer));
296 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
297 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
299 GNUNET_SCHEDULER_cancel (n->timeout_task);
300 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
304 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
307 update_stats (nodes_requested);
308 GNUNET_CONTAINER_multihashmap_put (nodes_active,
309 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
310 update_stats (nodes_active);
311 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
314 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
316 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from peer `%s'\n"),
317 "RESPONSE", "inactive", GNUNET_i2s (peer));
318 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
319 update_stats (nodes_inactive);
320 GNUNET_CONTAINER_multihashmap_put (nodes_active,
321 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
322 update_stats (nodes_active);
323 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
328 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
329 "RESPONSE", "unknown", GNUNET_i2s (peer));
334 * Method called whenever a given peer connects.
337 * @param peer peer identity this notification is about
339 void core_connect_handler (void *cls,
340 const struct GNUNET_PeerIdentity *peer)
342 if (GNUNET_YES == is_me(peer))
345 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
348 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &peer->hashPubKey))
349 return; /* We already sent a request */
351 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_active, &peer->hashPubKey))
352 return; /* This peer is known as active */
354 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
355 return; /* This peer is known as inactive */
363 * Method called whenever a given peer disconnects.
366 * @param peer peer identity this notification is about
368 void core_disconnect_handler (void *cls,
369 const struct GNUNET_PeerIdentity * peer)
371 if (GNUNET_YES == is_me(peer))
374 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
381 core_receive_handler (void *cls,
382 const struct GNUNET_PeerIdentity *other,
383 const struct GNUNET_MessageHeader *message)
385 if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
388 return GNUNET_SYSERR;
391 switch (ntohs (message->type)) {
392 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
393 handle_request (other);
395 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
396 handle_response (other);
408 * Start the nodes management
410 * @param cfg configuration handle
413 GNUNET_EXPERIMENTATION_nodes_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
415 /* Connecting to core service to find partners */
416 ch = GNUNET_CORE_connect (cfg, NULL,
417 &core_startup_handler,
418 &core_connect_handler,
419 &core_disconnect_handler,
420 &core_receive_handler,
421 GNUNET_NO, NULL, GNUNET_NO, NULL);
424 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
428 nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
429 nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
430 nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
434 * Stop the nodes management
437 GNUNET_EXPERIMENTATION_nodes_stop ()
441 GNUNET_CORE_disconnect (ch);
445 if (NULL != nodes_requested)
447 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
450 update_stats (nodes_requested);
451 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
452 nodes_requested = NULL;
455 if (NULL != nodes_active)
457 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
460 update_stats (nodes_active);
461 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
465 if (NULL != nodes_inactive)
467 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
470 update_stats (nodes_inactive);
471 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
472 nodes_inactive = NULL;
476 /* end of gnunet-daemon-experimentation_nodes.c */