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_container_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_core_service.h"
33 #define EXP_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
35 static struct GNUNET_CORE_Handle *ch;
37 static struct GNUNET_PeerIdentity me;
40 * A experimentation node
44 struct GNUNET_PeerIdentity id;
46 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
50 * Nodes with a pending request
53 struct GNUNET_CONTAINER_MultiHashMap *nodes_requested;
56 * Active experimentation nodes
58 struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
61 * Inactive experimentation nodes
62 * To be excluded from future requests
64 struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
68 cleanup_nodes (void *cls,
69 const struct GNUNET_HashCode * key,
73 struct GNUNET_CONTAINER_MultiHashMap *cur = cls;
76 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
78 GNUNET_SCHEDULER_cancel (n->timeout_task);
79 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
82 GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
88 * Task run during shutdown.
94 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Experimentation daemon shutting down ...\n"));
99 GNUNET_CORE_disconnect (ch);
103 if (NULL != nodes_requested)
105 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
108 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
109 nodes_requested = NULL;
112 if (NULL != nodes_active)
114 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
117 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
121 if (NULL != nodes_inactive)
123 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
126 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
127 nodes_inactive = NULL;
131 static int is_me (const struct GNUNET_PeerIdentity *id)
133 if (0 == memcmp (&me, id, sizeof (me)))
140 core_startup_handler (void *cls,
141 struct GNUNET_CORE_Handle * server,
142 const struct GNUNET_PeerIdentity *my_identity)
148 remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
150 struct Node *n = cls;
152 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &n->id.hashPubKey))
156 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n);
157 GNUNET_CONTAINER_multihashmap_put (nodes_inactive, &n->id.hashPubKey, n,
158 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
159 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
165 * Method called whenever a given peer connects.
168 * @param peer peer identity this notification is about
170 void core_connect_handler (void *cls,
171 const struct GNUNET_PeerIdentity * peer)
175 if (GNUNET_YES == is_me(peer))
178 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
181 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &peer->hashPubKey))
182 return; /* We already sent a request */
184 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_active, &peer->hashPubKey))
185 return; /*This peer is known as active */
187 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
188 return; /*This peer is known as inactive */
191 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
194 n = GNUNET_malloc (sizeof (struct Node));
196 n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
198 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
199 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
207 * Method called whenever a given peer disconnects.
210 * @param peer peer identity this notification is about
212 void core_disconnect_handler (void *cls,
213 const struct GNUNET_PeerIdentity * peer)
215 if (GNUNET_YES == is_me(peer))
218 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
224 * The main function for the experimentation daemon.
226 * @param argc number of arguments from the command line
227 * @param argv command line arguments
230 run (void *cls, char *const *args, const char *cfgfile,
231 const struct GNUNET_CONFIGURATION_Handle *cfg)
233 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Experimentation daemon starting ...\n"));
235 nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
236 nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
237 nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
239 /* Connecting to core service to find partners */
240 ch = GNUNET_CORE_connect (cfg, NULL,
241 &core_startup_handler,
242 &core_connect_handler,
243 &core_disconnect_handler,
244 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
247 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
251 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
258 * The main function for the experimentation daemon.
260 * @param argc number of arguments from the command line
261 * @param argv command line arguments
262 * @return 0 ok, 1 on error
265 main (int argc, char *const *argv)
267 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
268 GNUNET_GETOPT_OPTION_END
272 GNUNET_PROGRAM_run (argc, argv, "experimentation",
273 _("GNUnet hostlist server and client"), options,
274 &run, NULL)) ? 0 : 1;
277 /* end of gnunet-daemon-experimentation.c */