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.c
23 * @brief experimentation daemon
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 #define EXP_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
36 static struct GNUNET_CORE_Handle *ch;
38 static struct GNUNET_PeerIdentity me;
40 static struct GNUNET_STATISTICS_Handle *stats;
43 * Nodes with a pending request
46 struct GNUNET_CONTAINER_MultiHashMap *nodes_requested;
49 * Active experimentation nodes
51 struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
54 * Inactive experimentation nodes
55 * To be excluded from future requests
57 struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
60 static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
62 GNUNET_assert (NULL != m);
63 GNUNET_assert (NULL != stats);
65 if (m == nodes_active)
67 GNUNET_STATISTICS_set (stats, "# nodes active",
68 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
70 else if (m == nodes_inactive)
72 GNUNET_STATISTICS_set (stats, "# nodes inactive",
73 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
75 else if (m == nodes_requested)
77 GNUNET_STATISTICS_set (stats, "# nodes requested",
78 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
86 cleanup_nodes (void *cls,
87 const struct GNUNET_HashCode * key,
91 struct GNUNET_CONTAINER_MultiHashMap *cur = cls;
94 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
96 GNUNET_SCHEDULER_cancel (n->timeout_task);
97 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
101 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
106 GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
112 static int is_me (const struct GNUNET_PeerIdentity *id)
114 if (0 == memcmp (&me, id, sizeof (me)))
121 core_startup_handler (void *cls,
122 struct GNUNET_CORE_Handle * server,
123 const struct GNUNET_PeerIdentity *my_identity)
129 remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
131 struct Node *n = cls;
133 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Removing request for peer %s due to timeout\n"),
134 GNUNET_i2s (&n->id));
136 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &n->id.hashPubKey))
138 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n);
139 update_stats (nodes_requested);
140 GNUNET_CONTAINER_multihashmap_put (nodes_inactive, &n->id.hashPubKey, n,
141 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
142 update_stats (nodes_inactive);
145 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
148 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
153 size_t send_request_cb (void *cls, size_t bufsize, void *buf)
155 struct Node *n = cls;
156 struct Experimentation_Request msg;
157 size_t size = sizeof (msg);
162 /* client disconnected */
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
164 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
165 GNUNET_SCHEDULER_cancel (n->timeout_task);
166 GNUNET_SCHEDULER_add_now (&remove_request, n);
169 GNUNET_assert (bufsize >= size);
171 msg.msg.size = htons (size);
172 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
173 memcpy (buf, &msg, size);
175 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
176 GNUNET_i2s (&n->id));
180 static void send_request (const struct GNUNET_PeerIdentity *peer)
185 size = sizeof (struct Experimentation_Request);
186 n = GNUNET_malloc (sizeof (struct Node));
188 n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
189 n->cth = GNUNET_CORE_notify_transmit_ready(ch, GNUNET_NO, 0,
190 GNUNET_TIME_relative_get_forever_(),
191 peer, size, send_request_cb, n);
193 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
194 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
196 update_stats (nodes_requested);
199 size_t send_response_cb (void *cls, size_t bufsize, void *buf)
201 struct Node *n = cls;
202 struct Experimentation_Response msg;
203 size_t size = sizeof (msg);
208 /* client disconnected */
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
212 GNUNET_assert (bufsize >= size);
214 msg.msg.size = htons (size);
215 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
216 memcpy (buf, &msg, size);
218 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending response to peer %s\n"),
219 GNUNET_i2s (&n->id));
224 static void handle_request (const struct GNUNET_PeerIdentity *peer)
228 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
231 "REQUEST", "active", GNUNET_i2s (peer));
233 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
236 "REQUEST", "requested", GNUNET_i2s (peer));
237 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
238 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
240 GNUNET_SCHEDULER_cancel (n->timeout_task);
241 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
245 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
248 update_stats (nodes_requested);
249 GNUNET_CONTAINER_multihashmap_put (nodes_active,
250 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
251 update_stats (nodes_active);
252 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
255 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
257 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
258 "REQUEST", "inactive", GNUNET_i2s (peer));
259 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
260 update_stats (nodes_inactive);
261 GNUNET_CONTAINER_multihashmap_put (nodes_active,
262 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
263 update_stats (nodes_active);
264 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
269 /* Create new node */
270 n = GNUNET_malloc (sizeof (struct Node));
272 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
273 "REQUEST", "new", GNUNET_i2s (peer));
274 GNUNET_CONTAINER_multihashmap_put (nodes_active,
275 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
276 update_stats (nodes_active);
277 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
281 n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0,
282 GNUNET_TIME_relative_get_forever_(),
283 peer, sizeof (struct Experimentation_Response),
284 send_response_cb, n);
287 static void handle_response (const struct GNUNET_PeerIdentity *peer)
291 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
293 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
294 "RESPONSE", "active", GNUNET_i2s (peer));
296 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
298 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
299 "RESPONSE", "requested", GNUNET_i2s (peer));
300 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
301 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
303 GNUNET_SCHEDULER_cancel (n->timeout_task);
304 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
308 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
311 update_stats (nodes_requested);
312 GNUNET_CONTAINER_multihashmap_put (nodes_active,
313 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
314 update_stats (nodes_active);
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
318 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
320 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from peer `%s'\n"),
321 "RESPONSE", "inactive", GNUNET_i2s (peer));
322 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
323 update_stats (nodes_inactive);
324 GNUNET_CONTAINER_multihashmap_put (nodes_active,
325 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
326 update_stats (nodes_active);
327 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' active node \n"),
332 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
333 "RESPONSE", "unknown", GNUNET_i2s (peer));
338 * Method called whenever a given peer connects.
341 * @param peer peer identity this notification is about
343 void core_connect_handler (void *cls,
344 const struct GNUNET_PeerIdentity *peer)
346 if (GNUNET_YES == is_me(peer))
349 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
352 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &peer->hashPubKey))
353 return; /* We already sent a request */
355 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_active, &peer->hashPubKey))
356 return; /* This peer is known as active */
358 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
359 return; /* This peer is known as inactive */
367 * Method called whenever a given peer disconnects.
370 * @param peer peer identity this notification is about
372 void core_disconnect_handler (void *cls,
373 const struct GNUNET_PeerIdentity * peer)
375 if (GNUNET_YES == is_me(peer))
378 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
385 core_receive_handler (void *cls,
386 const struct GNUNET_PeerIdentity *other,
387 const struct GNUNET_MessageHeader *message)
389 if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
392 return GNUNET_SYSERR;
395 switch (ntohs (message->type)) {
396 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
397 handle_request (other);
399 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
400 handle_response (other);
412 * The main function for the experimentation daemon.
414 * @param argc number of arguments from the command line
415 * @param argv command line arguments
418 GNUNET_EXPERIMENTATION_nodes_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
420 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Experimentation daemon starting ...\n"));
422 stats = GNUNET_STATISTICS_create ("experimentation", cfg);
425 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to create statistics!\n"));
429 /* Connecting to core service to find partners */
430 ch = GNUNET_CORE_connect (cfg, NULL,
431 &core_startup_handler,
432 &core_connect_handler,
433 &core_disconnect_handler,
434 &core_receive_handler,
435 GNUNET_NO, NULL, GNUNET_NO, NULL);
438 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
442 nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
443 nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
444 nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
449 GNUNET_EXPERIMENTATION_nodes_stop ()
451 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Experimentation daemon shutting down ...\n"));
454 GNUNET_CORE_disconnect (ch);
458 if (NULL != nodes_requested)
460 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
463 update_stats (nodes_requested);
464 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
465 nodes_requested = NULL;
468 if (NULL != nodes_active)
470 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
473 update_stats (nodes_active);
474 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
478 if (NULL != nodes_inactive)
480 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
483 update_stats (nodes_inactive);
484 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
485 nodes_inactive = NULL;
490 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
495 /* end of gnunet-daemon-experimentation.c */