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"
38 static struct GNUNET_CORE_Handle *ch;
44 static struct GNUNET_PeerIdentity me;
48 * Nodes with a pending request
50 struct GNUNET_CONTAINER_MultiHashMap *nodes_requested;
54 * Active experimentation nodes
56 struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
60 * Inactive experimentation nodes
61 * To be excluded from future requests
63 struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
69 * @param m hashmap to update values from
71 static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
73 GNUNET_assert (NULL != m);
74 GNUNET_assert (NULL != GSE_stats);
76 if (m == nodes_active)
78 GNUNET_STATISTICS_set (GSE_stats, "# nodes active",
79 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
81 else if (m == nodes_inactive)
83 GNUNET_STATISTICS_set (GSE_stats, "# nodes inactive",
84 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
86 else if (m == nodes_requested)
88 GNUNET_STATISTICS_set (GSE_stats, "# nodes requested",
89 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
100 * @param cls the hashmap to clean up
101 * @param key key of the current node
102 * @param value related node object
103 * @return always GNUNET_OK
106 cleanup_nodes (void *cls,
107 const struct GNUNET_HashCode * key,
111 struct GNUNET_CONTAINER_MultiHashMap *cur = cls;
114 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
116 GNUNET_SCHEDULER_cancel (n->timeout_task);
117 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
121 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
126 GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
133 * Check if id passed is my id
135 * @param id the id to check
136 * @return GNUNET_YES or GNUNET_NO
138 static int is_me (const struct GNUNET_PeerIdentity *id)
140 if (0 == memcmp (&me, id, sizeof (me)))
147 * Core startup callback
150 * @param server core service's server handle
151 * @param my_identity my id
154 core_startup_handler (void *cls,
155 struct GNUNET_CORE_Handle *server,
156 const struct GNUNET_PeerIdentity *my_identity)
163 * Remove experimentation request due to timeout
165 * @param cls the related node
166 * @param tc scheduler's task context
169 remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 struct Node *n = cls;
173 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Removing request for peer %s due to timeout\n"),
174 GNUNET_i2s (&n->id));
176 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &n->id.hashPubKey))
178 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n);
179 update_stats (nodes_requested);
180 GNUNET_CONTAINER_multihashmap_put (nodes_inactive, &n->id.hashPubKey, n,
181 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
182 update_stats (nodes_inactive);
185 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
188 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
195 * Core's transmit notify callback to send request
197 * @param cls the related node
198 * @param bufsize buffer size
199 * @param buf the buffer to copy to
200 * @return bytes passed
202 size_t send_request_cb (void *cls, size_t bufsize, void *buf)
204 struct Node *n = cls;
205 struct Experimentation_Request msg;
206 size_t size = sizeof (msg);
211 /* client disconnected */
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
213 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
214 GNUNET_SCHEDULER_cancel (n->timeout_task);
215 GNUNET_SCHEDULER_add_now (&remove_request, n);
218 GNUNET_assert (bufsize >= size);
220 msg.msg.size = htons (size);
221 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
222 msg.capabilities = htonl (GSE_node_capabilities);
223 memcpy (buf, &msg, size);
225 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
226 GNUNET_i2s (&n->id));
234 * @param peer the peer to send to
236 static void send_request (const struct GNUNET_PeerIdentity *peer)
241 size = sizeof (struct Experimentation_Request);
242 n = GNUNET_malloc (sizeof (struct Node));
244 n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
245 n->cth = GNUNET_CORE_notify_transmit_ready(ch, GNUNET_NO, 0,
246 GNUNET_TIME_relative_get_forever_(),
247 peer, size, send_request_cb, n);
248 n->capabilities = NONE;
250 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
251 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
253 update_stats (nodes_requested);
258 * Core's transmit notify callback to send response
260 * @param cls the related node
261 * @param bufsize buffer size
262 * @param buf the buffer to copy to
263 * @return bytes passed
265 size_t send_response_cb (void *cls, size_t bufsize, void *buf)
267 struct Node *n = cls;
268 struct Experimentation_Response msg;
269 size_t size = sizeof (msg);
274 /* client disconnected */
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
278 GNUNET_assert (bufsize >= size);
280 msg.msg.size = htons (size);
281 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
282 msg.capabilities = htonl (GSE_node_capabilities);
283 memcpy (buf, &msg, size);
285 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending response to peer %s\n"),
286 GNUNET_i2s (&n->id));
292 * Set a specific node as active
296 static void node_make_active (struct Node *n)
298 GNUNET_CONTAINER_multihashmap_put (nodes_active,
299 &n->id.hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
300 update_stats (nodes_active);
301 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' as active node\n"),
302 GNUNET_i2s (&n->id));
307 * Handle a request and send a response
309 * @param peer the source
310 * @param message the message
312 static void handle_request (const struct GNUNET_PeerIdentity *peer,
313 const struct GNUNET_MessageHeader *message)
316 struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
318 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
320 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
321 "REQUEST", "active", GNUNET_i2s (peer));
322 n->capabilities = ntohl (rm->capabilities);
324 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
326 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
327 "REQUEST", "requested", GNUNET_i2s (peer));
328 n->capabilities = ntohl (rm->capabilities);
329 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
330 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
332 GNUNET_SCHEDULER_cancel (n->timeout_task);
333 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
337 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
340 update_stats (nodes_requested);
341 node_make_active (n);
343 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
345 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
346 "REQUEST", "inactive", GNUNET_i2s (peer));
347 n->capabilities = ntohl (rm->capabilities);
348 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
349 update_stats (nodes_inactive);
350 node_make_active (n);
354 /* Create new node */
355 n = GNUNET_malloc (sizeof (struct Node));
357 n->capabilities = NONE;
358 n->capabilities = ntohl (rm->capabilities);
359 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
360 "REQUEST", "new", GNUNET_i2s (peer));
361 node_make_active (n);
363 n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0,
364 GNUNET_TIME_relative_get_forever_(),
365 peer, sizeof (struct Experimentation_Response),
366 send_response_cb, n);
373 * @param peer the source
374 * @param message the message
376 static void handle_response (const struct GNUNET_PeerIdentity *peer,
377 const struct GNUNET_MessageHeader *message)
380 struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
382 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
384 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
385 "RESPONSE", "active", GNUNET_i2s (peer));
386 n->capabilities = ntohl (rm->capabilities);
388 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
390 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
391 "RESPONSE", "requested", GNUNET_i2s (peer));
392 n->capabilities = ntohl (rm->capabilities);
393 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
394 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
396 GNUNET_SCHEDULER_cancel (n->timeout_task);
397 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
401 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
404 update_stats (nodes_requested);
405 node_make_active (n);
407 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
409 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from peer `%s'\n"),
410 "RESPONSE", "inactive", GNUNET_i2s (peer));
411 n->capabilities = ntohl (rm->capabilities);
412 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
413 update_stats (nodes_inactive);
414 node_make_active (n);
418 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
419 "RESPONSE", "unknown", GNUNET_i2s (peer));
425 * Method called whenever a given peer connects.
428 * @param peer peer identity this notification is about
430 void core_connect_handler (void *cls,
431 const struct GNUNET_PeerIdentity *peer)
433 if (GNUNET_YES == is_me(peer))
436 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
439 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &peer->hashPubKey))
440 return; /* We already sent a request */
442 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_active, &peer->hashPubKey))
443 return; /* This peer is known as active */
445 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
446 return; /* This peer is known as inactive */
454 * Method called whenever a given peer disconnects.
457 * @param peer peer identity this notification is about
459 void core_disconnect_handler (void *cls,
460 const struct GNUNET_PeerIdentity * peer)
462 if (GNUNET_YES == is_me(peer))
465 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
472 * Handle a request and send a response
475 * @param other the sender
476 * @param message the message
477 * @return GNUNET_OK to keep connection, GNUNET_SYSERR on error
480 core_receive_handler (void *cls,
481 const struct GNUNET_PeerIdentity *other,
482 const struct GNUNET_MessageHeader *message)
484 if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
487 return GNUNET_SYSERR;
490 switch (ntohs (message->type)) {
491 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
492 handle_request (other, message);
494 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
495 handle_response (other, message);
507 * Start the nodes management
510 GNUNET_EXPERIMENTATION_nodes_start ()
512 /* Connecting to core service to find partners */
513 ch = GNUNET_CORE_connect (GSE_cfg, NULL,
514 &core_startup_handler,
515 &core_connect_handler,
516 &core_disconnect_handler,
517 &core_receive_handler,
518 GNUNET_NO, NULL, GNUNET_NO, NULL);
521 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
525 nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
526 nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
527 nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
532 * Stop the nodes management
535 GNUNET_EXPERIMENTATION_nodes_stop ()
539 GNUNET_CORE_disconnect (ch);
543 if (NULL != nodes_requested)
545 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
548 update_stats (nodes_requested);
549 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
550 nodes_requested = NULL;
553 if (NULL != nodes_active)
555 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
558 update_stats (nodes_active);
559 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
563 if (NULL != nodes_inactive)
565 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
568 update_stats (nodes_inactive);
569 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
570 nodes_inactive = NULL;
574 /* end of gnunet-daemon-experimentation_nodes.c */