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"
33 #define EXP_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
35 static struct GNUNET_CORE_Handle *ch;
37 static struct GNUNET_PeerIdentity me;
39 static struct GNUNET_STATISTICS_Handle *stats;
42 * A experimentation node
46 struct GNUNET_PeerIdentity id;
48 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
50 struct GNUNET_CORE_TransmitHandle *cth;
54 struct Experimentation_Request
56 struct GNUNET_MessageHeader msg;
60 * Nodes with a pending request
63 struct GNUNET_CONTAINER_MultiHashMap *nodes_requested;
66 * Active experimentation nodes
68 struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
71 * Inactive experimentation nodes
72 * To be excluded from future requests
74 struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
77 static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
79 GNUNET_assert (NULL != m);
80 GNUNET_assert (NULL != stats);
82 if (m == nodes_active)
84 GNUNET_STATISTICS_set (stats, "# nodes active",
85 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
87 else if (m == nodes_inactive)
89 GNUNET_STATISTICS_set (stats, "# nodes inactive",
90 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
92 else if (m == nodes_requested)
94 GNUNET_STATISTICS_set (stats, "# nodes requested",
95 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
103 cleanup_nodes (void *cls,
104 const struct GNUNET_HashCode * key,
108 struct GNUNET_CONTAINER_MultiHashMap *cur = cls;
111 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
113 GNUNET_SCHEDULER_cancel (n->timeout_task);
114 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
118 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
123 GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
129 * Task run during shutdown.
135 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
137 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Experimentation daemon shutting down ...\n"));
140 GNUNET_CORE_disconnect (ch);
144 if (NULL != nodes_requested)
146 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
149 update_stats (nodes_requested);
150 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
151 nodes_requested = NULL;
154 if (NULL != nodes_active)
156 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
159 update_stats (nodes_active);
160 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
164 if (NULL != nodes_inactive)
166 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
169 update_stats (nodes_inactive);
170 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
171 nodes_inactive = NULL;
176 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
181 static int is_me (const struct GNUNET_PeerIdentity *id)
183 if (0 == memcmp (&me, id, sizeof (me)))
190 core_startup_handler (void *cls,
191 struct GNUNET_CORE_Handle * server,
192 const struct GNUNET_PeerIdentity *my_identity)
198 remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
200 struct Node *n = cls;
202 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Removing request for peer %s due to timeout\n"),
203 GNUNET_i2s (&n->id));
205 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &n->id.hashPubKey))
209 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n);
210 update_stats (nodes_requested);
211 GNUNET_CONTAINER_multihashmap_put (nodes_inactive, &n->id.hashPubKey, n,
212 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
213 update_stats (nodes_inactive);
214 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
217 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
224 size_t send_request_cb (void *cls, size_t bufsize, void *buf)
226 struct Node *n = cls;
227 struct Experimentation_Request msg;
228 size_t size = sizeof (msg);
233 /* client disconnected */
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
235 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
236 GNUNET_SCHEDULER_cancel (n->timeout_task);
237 GNUNET_SCHEDULER_add_now (&remove_request, n);
240 GNUNET_assert (bufsize >= size);
242 msg.msg.size = htons (size);
243 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
244 memcpy (buf, &msg, size);
246 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
247 GNUNET_i2s (&n->id));
251 static void send_request (const struct GNUNET_PeerIdentity *peer)
256 size = sizeof (struct Experimentation_Request);
257 n = GNUNET_malloc (sizeof (struct Node));
259 n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
260 n->cth = GNUNET_CORE_notify_transmit_ready(ch, GNUNET_NO, 0,
261 GNUNET_TIME_relative_get_forever_(),
262 peer, size, send_request_cb, n);
264 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
265 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
267 update_stats (nodes_requested);
271 * Method called whenever a given peer connects.
274 * @param peer peer identity this notification is about
276 void core_connect_handler (void *cls,
277 const struct GNUNET_PeerIdentity *peer)
279 if (GNUNET_YES == is_me(peer))
282 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
285 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &peer->hashPubKey))
286 return; /* We already sent a request */
288 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_active, &peer->hashPubKey))
289 return; /* This peer is known as active */
291 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
292 return; /* This peer is known as inactive */
300 * Method called whenever a given peer disconnects.
303 * @param peer peer identity this notification is about
305 void core_disconnect_handler (void *cls,
306 const struct GNUNET_PeerIdentity * peer)
308 if (GNUNET_YES == is_me(peer))
311 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
317 * The main function for the experimentation daemon.
319 * @param argc number of arguments from the command line
320 * @param argv command line arguments
323 run (void *cls, char *const *args, const char *cfgfile,
324 const struct GNUNET_CONFIGURATION_Handle *cfg)
326 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Experimentation daemon starting ...\n"));
328 stats = GNUNET_STATISTICS_create ("experimentation", cfg);
331 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to create statistics!\n"));
335 /* Connecting to core service to find partners */
336 ch = GNUNET_CORE_connect (cfg, NULL,
337 &core_startup_handler,
338 &core_connect_handler,
339 &core_disconnect_handler,
340 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
343 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
347 nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
348 nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
349 nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
351 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
358 * The main function for the experimentation daemon.
360 * @param argc number of arguments from the command line
361 * @param argv command line arguments
362 * @return 0 ok, 1 on error
365 main (int argc, char *const *argv)
367 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
368 GNUNET_GETOPT_OPTION_END
372 GNUNET_PROGRAM_run (argc, argv, "experimentation",
373 _("GNUnet hostlist server and client"), options,
374 &run, NULL)) ? 0 : 1;
377 /* end of gnunet-daemon-experimentation.c */