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 != GED_stats);
76 if (m == nodes_active)
78 GNUNET_STATISTICS_set (GED_stats, "# nodes active",
79 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
81 else if (m == nodes_inactive)
83 GNUNET_STATISTICS_set (GED_stats, "# nodes inactive",
84 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
86 else if (m == nodes_requested)
88 GNUNET_STATISTICS_set (GED_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);
124 GNUNET_free_non_null (n->issuer_id);
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_experimentation_request_cb (void *cls, size_t bufsize, void *buf)
204 struct Node *n = cls;
205 struct Experimentation_Request msg;
206 size_t msg_size = sizeof (msg);
207 size_t ri_size = sizeof (struct Experimentation_Issuer) * GSE_my_issuer_count;
208 size_t total_size = msg_size + ri_size;
210 memset (buf, '0', bufsize);
214 /* client disconnected */
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
216 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
217 GNUNET_SCHEDULER_cancel (n->timeout_task);
218 GNUNET_SCHEDULER_add_now (&remove_request, n);
221 GNUNET_assert (bufsize >= total_size);
223 msg.msg.size = htons (total_size);
224 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
225 msg.capabilities = htonl (GSE_node_capabilities);
226 msg.issuer_count = htonl (GSE_my_issuer_count);
227 memcpy (buf, &msg, msg_size);
228 memcpy (&((char *) buf)[msg_size], GSE_my_issuer, ri_size);
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
231 GNUNET_i2s (&n->id));
237 * Send request to peer to start add him to to the set of experimentation nodes
239 * @param peer the peer to send to
241 static void send_experimentation_request (const struct GNUNET_PeerIdentity *peer)
247 c_issuers = GSE_my_issuer_count;
249 size = sizeof (struct Experimentation_Request) +
250 c_issuers * sizeof (struct Experimentation_Issuer);
251 n = GNUNET_malloc (sizeof (struct Node));
253 n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
254 n->capabilities = NONE;
255 n->cth = GNUNET_CORE_notify_transmit_ready(ch, GNUNET_NO, 0,
256 GNUNET_TIME_relative_get_forever_(),
257 peer, size, send_experimentation_request_cb, n);
259 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
260 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
261 update_stats (nodes_requested);
266 * Core's transmit notify callback to send response
268 * @param cls the related node
269 * @param bufsize buffer size
270 * @param buf the buffer to copy to
271 * @return bytes passed
273 size_t send_response_cb (void *cls, size_t bufsize, void *buf)
275 struct Node *n = cls;
276 struct Experimentation_Response msg;
277 size_t ri_size = GSE_my_issuer_count * sizeof (struct Experimentation_Issuer);
278 size_t msg_size = sizeof (msg);
279 size_t total_size = msg_size + ri_size;
284 /* client disconnected */
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
288 GNUNET_assert (bufsize >= total_size);
290 msg.msg.size = htons (total_size);
291 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
292 msg.capabilities = htonl (GSE_node_capabilities);
293 msg.issuer_count = htonl (GSE_my_issuer_count);
294 memcpy (buf, &msg, msg_size);
295 memcpy (&((char *) buf)[msg_size], GSE_my_issuer, ri_size);
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending response to peer %s\n"),
298 GNUNET_i2s (&n->id));
304 get_experiments_cb (struct Node *n, struct Experiment *e)
306 static int counter = 0;
309 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added %u experiments for peer %s\n"),
310 counter, GNUNET_i2s (&n->id));
314 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting experiment `%s' with peer %s\n"),
316 GNUNET_i2s (&n->id));
318 /* Tell the scheduler to add a node with an experiment */
319 GED_scheduler_add (n, e);
324 * Set a specific node as active
328 static void node_make_active (struct Node *n)
331 GNUNET_CONTAINER_multihashmap_put (nodes_active,
332 &n->id.hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
333 update_stats (nodes_active);
334 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' as active node\n"),
335 GNUNET_i2s (&n->id));
337 /* Request experiments for this node to start them */
338 for (c1 = 0; c1 < n->issuer_count; c1++)
341 GED_experiments_get (n, &n->issuer_id[c1], &get_experiments_cb);
347 * Handle a request and send a response
349 * @param peer the source
350 * @param message the message
352 static void handle_request (const struct GNUNET_PeerIdentity *peer,
353 const struct GNUNET_MessageHeader *message)
356 struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
357 struct Experimentation_Issuer *rmi = (struct Experimentation_Issuer *) &rm[1];
361 uint32_t ic_accepted;
364 if (ntohs (message->size) < sizeof (struct Experimentation_Request))
369 ic = ntohl (rm->issuer_count);
370 if (ntohs (message->size) != sizeof (struct Experimentation_Request) + ic * sizeof (struct Experimentation_Issuer))
376 make_active = GNUNET_NO;
377 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
381 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
383 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
384 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
386 GNUNET_SCHEDULER_cancel (n->timeout_task);
387 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
391 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
394 update_stats (nodes_requested);
395 make_active = GNUNET_YES;
397 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
399 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
400 update_stats (nodes_inactive);
401 make_active = GNUNET_YES;
405 /* Create new node */
406 n = GNUNET_malloc (sizeof (struct Node));
408 n->capabilities = NONE;
409 make_active = GNUNET_YES;
413 n->capabilities = ntohl (rm->capabilities);
415 /* Filter accepted issuer */
417 for (c1 = 0; c1 < ic; c1++)
419 if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
422 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Request from peer `%s' with %u issuers, we accepted %u issuer \n"),
423 GNUNET_i2s (peer), ic, ic_accepted);
424 GNUNET_free_non_null (n->issuer_id);
425 n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
427 for (c1 = 0; c1 < ic; c1++)
429 if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
431 n->issuer_id[c2] = rmi[c1].issuer_id;
435 n->issuer_count = ic_accepted;
437 if (GNUNET_YES == make_active)
438 node_make_active (n);
441 n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0,
442 GNUNET_TIME_relative_get_forever_(),
444 sizeof (struct Experimentation_Response) +
445 GSE_my_issuer_count * sizeof (struct Experimentation_Issuer),
446 send_response_cb, n);
453 * @param peer the source
454 * @param message the message
456 static void handle_response (const struct GNUNET_PeerIdentity *peer,
457 const struct GNUNET_MessageHeader *message)
460 struct Experimentation_Response *rm = (struct Experimentation_Response *) message;
461 struct Experimentation_Issuer *rmi = (struct Experimentation_Issuer *) &rm[1];
463 uint32_t ic_accepted;
469 if (ntohs (message->size) < sizeof (struct Experimentation_Response))
474 ic = ntohl (rm->issuer_count);
475 if (ntohs (message->size) != sizeof (struct Experimentation_Response) + ic * sizeof (struct Experimentation_Issuer))
481 make_active = GNUNET_NO;
482 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
484 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
485 "RESPONSE", "active", GNUNET_i2s (peer));
487 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
489 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
490 "RESPONSE", "requested", GNUNET_i2s (peer));
491 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
492 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
494 GNUNET_SCHEDULER_cancel (n->timeout_task);
495 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
499 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
502 update_stats (nodes_requested);
503 make_active = GNUNET_YES;
505 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
507 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from peer `%s'\n"),
508 "RESPONSE", "inactive", GNUNET_i2s (peer));
509 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
510 update_stats (nodes_inactive);
511 make_active = GNUNET_YES;
515 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
516 "RESPONSE", "unknown", GNUNET_i2s (peer));
521 n->capabilities = ntohl (rm->capabilities);
523 /* Filter accepted issuer */
525 for (c1 = 0; c1 < ic; c1++)
527 if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
530 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Response from peer `%s' with %u issuers, we accepted %u issuer \n"),
531 GNUNET_i2s (peer), ic, ic_accepted);
532 GNUNET_free_non_null (n->issuer_id);
533 n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
535 for (c1 = 0; c1 < ic; c1++)
537 if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
539 n->issuer_id[c2] = rmi[c1].issuer_id;
543 n->issuer_count = ic_accepted;
545 if (GNUNET_YES == make_active)
546 node_make_active (n);
552 * @param peer the source
553 * @param message the message
555 static void handle_start (const struct GNUNET_PeerIdentity *peer,
556 const struct GNUNET_MessageHeader *message)
558 GED_scheduler_handle_start (NULL, NULL);
564 * @param peer the source
565 * @param message the message
567 static void handle_start_ack (const struct GNUNET_PeerIdentity *peer,
568 const struct GNUNET_MessageHeader *message)
570 GED_scheduler_handle_start_ack (NULL, NULL);
576 * @param peer the source
577 * @param message the message
579 static void handle_stop (const struct GNUNET_PeerIdentity *peer,
580 const struct GNUNET_MessageHeader *message)
582 GED_scheduler_handle_stop (NULL, NULL);
586 * Method called whenever a given peer connects.
589 * @param peer peer identity this notification is about
591 void core_connect_handler (void *cls,
592 const struct GNUNET_PeerIdentity *peer)
594 if (GNUNET_YES == is_me(peer))
597 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
600 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &peer->hashPubKey))
601 return; /* We already sent a request */
603 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_active, &peer->hashPubKey))
604 return; /* This peer is known as active */
606 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
607 return; /* This peer is known as inactive */
609 send_experimentation_request (peer);
615 * Method called whenever a given peer disconnects.
618 * @param peer peer identity this notification is about
620 void core_disconnect_handler (void *cls,
621 const struct GNUNET_PeerIdentity * peer)
623 if (GNUNET_YES == is_me(peer))
626 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
633 * Handle a request and send a response
636 * @param other the sender
637 * @param message the message
638 * @return GNUNET_OK to keep connection, GNUNET_SYSERR on error
641 core_receive_handler (void *cls,
642 const struct GNUNET_PeerIdentity *other,
643 const struct GNUNET_MessageHeader *message)
645 if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
648 return GNUNET_SYSERR;
651 switch (ntohs (message->type)) {
652 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
653 handle_request (other, message);
655 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
656 handle_response (other, message);
658 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START:
659 handle_start (other, message);
661 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK:
662 handle_start_ack (other, message);
664 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_STOP:
665 handle_stop (other, message);
674 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
676 struct GNUNET_EXPERIMENTATION_start_message
678 struct GNUNET_MessageHeader header;
681 struct ExperimentStartCtx
683 struct ExperimentStartCtx *prev;
684 struct ExperimentStartCtx *next;
687 struct Experiment *e;
690 size_t node_experiment_start_cb (void *cls, size_t bufsize, void *buf)
692 struct ExperimentStartCtx *e_ctx = cls;
693 struct GNUNET_EXPERIMENTATION_start_message msg;
695 GNUNET_CONTAINER_DLL_remove (e_ctx->n->e_req_head, e_ctx->n->e_req_tail, e_ctx);
696 e_ctx->n->cth = NULL;
703 size_t size = sizeof (struct GNUNET_EXPERIMENTATION_start_message);
704 msg.header.size = htons (size);
705 msg.header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START);
707 memcpy (buf, &msg, size);
713 GED_nodes_rts (struct Node *n)
723 * Request a experiment to start with a node
725 * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
728 GED_nodes_request_start (struct Node *n, struct Experiment *e)
730 struct ExperimentStartCtx *e_ctx;
734 GNUNET_break (0); /* should call rts before */
738 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending experiment start request to peer `%s' for experiment `%s'\n"),
739 GNUNET_i2s(&n->id), e->name);
741 e_ctx = GNUNET_malloc (sizeof (struct ExperimentStartCtx));
744 n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0, FAST_TIMEOUT, &n->id,
745 sizeof (struct GNUNET_EXPERIMENTATION_start_message),
746 &node_experiment_start_cb, e_ctx);
749 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Cannot send experiment start request to peer `%s' for experiment `%s'\n"),
750 GNUNET_i2s(&n->id), e->name);
753 GNUNET_CONTAINER_DLL_insert (n->e_req_head, n->e_req_tail, e_ctx);
760 * Start the nodes management
765 /* Connecting to core service to find partners */
766 ch = GNUNET_CORE_connect (GED_cfg, NULL,
767 &core_startup_handler,
768 &core_connect_handler,
769 &core_disconnect_handler,
770 &core_receive_handler,
771 GNUNET_NO, NULL, GNUNET_NO, NULL);
774 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
778 nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
779 nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
780 nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
785 * Stop the nodes management
792 GNUNET_CORE_disconnect (ch);
796 if (NULL != nodes_requested)
798 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
801 update_stats (nodes_requested);
802 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
803 nodes_requested = NULL;
806 if (NULL != nodes_active)
808 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
811 update_stats (nodes_active);
812 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
816 if (NULL != nodes_inactive)
818 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
821 update_stats (nodes_inactive);
822 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
823 nodes_inactive = NULL;
827 /* end of gnunet-daemon-experimentation_nodes.c */