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 msg.capabilities = htonl (GSE_node_capabilities);
170 memcpy (buf, &msg, size);
172 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
173 GNUNET_i2s (&n->id));
177 static void send_request (const struct GNUNET_PeerIdentity *peer)
182 size = sizeof (struct Experimentation_Request);
183 n = GNUNET_malloc (sizeof (struct Node));
185 n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
186 n->cth = GNUNET_CORE_notify_transmit_ready(ch, GNUNET_NO, 0,
187 GNUNET_TIME_relative_get_forever_(),
188 peer, size, send_request_cb, n);
189 n->capabilities = NONE;
191 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
192 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
194 update_stats (nodes_requested);
197 size_t send_response_cb (void *cls, size_t bufsize, void *buf)
199 struct Node *n = cls;
200 struct Experimentation_Response msg;
201 size_t size = sizeof (msg);
206 /* client disconnected */
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
210 GNUNET_assert (bufsize >= size);
212 msg.msg.size = htons (size);
213 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
214 msg.capabilities = htonl (GSE_node_capabilities);
215 memcpy (buf, &msg, size);
217 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending response to peer %s\n"),
218 GNUNET_i2s (&n->id));
222 static void node_make_active (struct Node *n)
224 GNUNET_CONTAINER_multihashmap_put (nodes_active,
225 &n->id.hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
226 update_stats (nodes_active);
227 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' as active node\n"),
228 GNUNET_i2s (&n->id));
232 static void handle_request (const struct GNUNET_PeerIdentity *peer,
233 const struct GNUNET_MessageHeader *message)
236 struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
238 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
240 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
241 "REQUEST", "active", GNUNET_i2s (peer));
242 n->capabilities = ntohl (rm->capabilities);
244 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
246 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
247 "REQUEST", "requested", GNUNET_i2s (peer));
248 n->capabilities = ntohl (rm->capabilities);
249 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
250 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
252 GNUNET_SCHEDULER_cancel (n->timeout_task);
253 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
257 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
260 update_stats (nodes_requested);
261 node_make_active (n);
263 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
265 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
266 "REQUEST", "inactive", GNUNET_i2s (peer));
267 n->capabilities = ntohl (rm->capabilities);
268 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
269 update_stats (nodes_inactive);
270 node_make_active (n);
274 /* Create new node */
275 n = GNUNET_malloc (sizeof (struct Node));
277 n->capabilities = NONE;
278 n->capabilities = ntohl (rm->capabilities);
279 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
280 "REQUEST", "new", GNUNET_i2s (peer));
281 node_make_active (n);
283 n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0,
284 GNUNET_TIME_relative_get_forever_(),
285 peer, sizeof (struct Experimentation_Response),
286 send_response_cb, n);
289 static void handle_response (const struct GNUNET_PeerIdentity *peer,
290 const struct GNUNET_MessageHeader *message)
293 struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
295 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
298 "RESPONSE", "active", GNUNET_i2s (peer));
299 n->capabilities = ntohl (rm->capabilities);
301 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
303 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
304 "RESPONSE", "requested", GNUNET_i2s (peer));
305 n->capabilities = ntohl (rm->capabilities);
306 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
307 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
309 GNUNET_SCHEDULER_cancel (n->timeout_task);
310 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
314 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
317 update_stats (nodes_requested);
318 node_make_active (n);
320 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
322 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from peer `%s'\n"),
323 "RESPONSE", "inactive", GNUNET_i2s (peer));
324 n->capabilities = ntohl (rm->capabilities);
325 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
326 update_stats (nodes_inactive);
327 node_make_active (n);
331 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
332 "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, message);
399 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
400 handle_response (other, message);
412 * Start the nodes management
414 * @param cfg configuration handle
417 GNUNET_EXPERIMENTATION_nodes_start ()
419 /* Connecting to core service to find partners */
420 ch = GNUNET_CORE_connect (GSE_cfg, NULL,
421 &core_startup_handler,
422 &core_connect_handler,
423 &core_disconnect_handler,
424 &core_receive_handler,
425 GNUNET_NO, NULL, GNUNET_NO, NULL);
428 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
432 nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
433 nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
434 nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
438 * Stop the nodes management
441 GNUNET_EXPERIMENTATION_nodes_stop ()
445 GNUNET_CORE_disconnect (ch);
449 if (NULL != nodes_requested)
451 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
454 update_stats (nodes_requested);
455 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
456 nodes_requested = NULL;
459 if (NULL != nodes_active)
461 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
464 update_stats (nodes_active);
465 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
469 if (NULL != nodes_inactive)
471 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
474 update_stats (nodes_inactive);
475 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
476 nodes_inactive = NULL;
480 /* end of gnunet-daemon-experimentation_nodes.c */