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"
35 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
39 static struct GNUNET_CORE_Handle *ch;
45 static struct GNUNET_PeerIdentity me;
49 * Nodes with a pending request
51 struct GNUNET_CONTAINER_MultiHashMap *nodes_requested;
55 * Active experimentation nodes
57 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 struct NodeComCtx *prev;
69 struct NodeComCtx *next;
75 GNUNET_CONNECTION_TransmitReadyNotify notify;
83 * @param m hashmap to update values from
85 static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
87 GNUNET_assert (NULL != m);
88 GNUNET_assert (NULL != GED_stats);
90 if (m == nodes_active)
92 GNUNET_STATISTICS_set (GED_stats, "# nodes active",
93 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
95 else if (m == nodes_inactive)
97 GNUNET_STATISTICS_set (GED_stats, "# nodes inactive",
98 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
100 else if (m == nodes_requested)
102 GNUNET_STATISTICS_set (GED_stats, "# nodes requested",
103 GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
114 * @param cls the hashmap to clean up
115 * @param key key of the current node
116 * @param value related node object
117 * @return always GNUNET_OK
120 cleanup_node (void *cls,
121 const struct GNUNET_HashCode * key,
125 struct NodeComCtx *e_cur;
126 struct NodeComCtx *e_next;
127 struct GNUNET_CONTAINER_MultiHashMap *cur = cls;
130 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
132 GNUNET_SCHEDULER_cancel (n->timeout_task);
133 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
138 GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
141 e_next = n->e_req_head;
142 while (NULL != (e_cur = e_next))
144 e_next = e_cur->next;
145 GNUNET_CONTAINER_DLL_remove (n->e_req_head, n->e_req_tail, e_cur);
149 GNUNET_free_non_null (n->issuer_id);
151 GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
158 * Check if id passed is my id
160 * @param id the id to check
161 * @return GNUNET_YES or GNUNET_NO
163 static int is_me (const struct GNUNET_PeerIdentity *id)
165 if (0 == memcmp (&me, id, sizeof (me)))
172 * Core startup callback
175 * @param server core service's server handle
176 * @param my_identity my id
179 core_startup_handler (void *cls,
180 struct GNUNET_CORE_Handle *server,
181 const struct GNUNET_PeerIdentity *my_identity)
187 schedule_transmisson (struct NodeComCtx *e_ctx);
190 transmit_read_wrapper (void *cls, size_t bufsize, void *buf)
192 struct NodeComCtx *e_ctx = cls;
193 struct NodeComCtx *next = NULL;
195 size_t res = e_ctx->notify (e_ctx->notify_cls, bufsize, buf);
196 e_ctx->n->cth = NULL;
198 GNUNET_CONTAINER_DLL_remove (e_ctx->n->e_req_head, e_ctx->n->e_req_tail, e_ctx);
199 next = e_ctx->n->e_req_head;
204 /* Schedule next message */
205 schedule_transmisson (next);
211 schedule_transmisson (struct NodeComCtx *e_ctx)
213 if (NULL != e_ctx->n->cth)
216 e_ctx->n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0, FAST_TIMEOUT,
217 &e_ctx->n->id, e_ctx->size, transmit_read_wrapper, e_ctx);
218 if (NULL == e_ctx->n->cth)
220 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Cannot send message to peer `%s' for experiment `%s'\n"),
221 GNUNET_i2s(&e_ctx->n->id), e_ctx->e->name);
229 * Remove experimentation request due to timeout
231 * @param cls the related node
232 * @param tc scheduler's task context
235 remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
237 struct Node *n = cls;
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing request for peer %s due to timeout\n",
240 GNUNET_i2s (&n->id));
242 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &n->id.hashPubKey))
244 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n);
245 update_stats (nodes_requested);
246 GNUNET_CONTAINER_multihashmap_put (nodes_inactive, &n->id.hashPubKey, n,
247 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
248 update_stats (nodes_inactive);
250 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
255 * Core's transmit notify callback to send request
257 * @param cls the related node
258 * @param bufsize buffer size
259 * @param buf the buffer to copy to
260 * @return bytes passed
262 size_t send_experimentation_request_cb (void *cls, size_t bufsize, void *buf)
264 struct Node *n = cls;
265 struct Experimentation_Request msg;
266 size_t msg_size = sizeof (msg);
267 size_t ri_size = sizeof (struct Experimentation_Issuer) * GSE_my_issuer_count;
268 size_t total_size = msg_size + ri_size;
270 memset (buf, '0', bufsize);
274 /* client disconnected */
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
276 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
277 GNUNET_SCHEDULER_cancel (n->timeout_task);
278 GNUNET_SCHEDULER_add_now (&remove_request, n);
281 GNUNET_assert (bufsize >= total_size);
283 msg.msg.size = htons (total_size);
284 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
285 msg.capabilities = htonl (GSE_node_capabilities);
286 msg.issuer_count = htonl (GSE_my_issuer_count);
287 memcpy (buf, &msg, msg_size);
288 memcpy (&((char *) buf)[msg_size], GSE_my_issuer, ri_size);
290 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending experimentation request to peer %s\n"),
291 GNUNET_i2s (&n->id));
297 * Send request to peer to start add him to to the set of experimentation nodes
299 * @param peer the peer to send to
301 static void send_experimentation_request (const struct GNUNET_PeerIdentity *peer)
304 struct NodeComCtx *e_ctx;
308 c_issuers = GSE_my_issuer_count;
310 size = sizeof (struct Experimentation_Request) +
311 c_issuers * sizeof (struct Experimentation_Issuer);
312 n = GNUNET_malloc (sizeof (struct Node));
314 n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
315 n->capabilities = NONE;
317 e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
321 e_ctx->notify = &send_experimentation_request_cb;
322 e_ctx->notify_cls = n;
323 GNUNET_CONTAINER_DLL_insert_tail(n->e_req_head, n->e_req_tail, e_ctx);
324 schedule_transmisson (e_ctx);
326 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
327 &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
328 update_stats (nodes_requested);
333 * Core's transmit notify callback to send response
335 * @param cls the related node
336 * @param bufsize buffer size
337 * @param buf the buffer to copy to
338 * @return bytes passed
340 size_t send_response_cb (void *cls, size_t bufsize, void *buf)
342 struct Node *n = cls;
343 struct Experimentation_Response msg;
344 size_t ri_size = GSE_my_issuer_count * sizeof (struct Experimentation_Issuer);
345 size_t msg_size = sizeof (msg);
346 size_t total_size = msg_size + ri_size;
351 /* client disconnected */
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
355 GNUNET_assert (bufsize >= total_size);
357 msg.msg.size = htons (total_size);
358 msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
359 msg.capabilities = htonl (GSE_node_capabilities);
360 msg.issuer_count = htonl (GSE_my_issuer_count);
361 memcpy (buf, &msg, msg_size);
362 memcpy (&((char *) buf)[msg_size], GSE_my_issuer, ri_size);
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending response to peer %s\n",
365 GNUNET_i2s (&n->id));
371 get_experiments_cb (struct Node *n, struct Experiment *e)
373 static int counter = 0;
377 /* Tell the scheduler to add a node with an experiment */
378 GED_scheduler_add (n, e, GNUNET_YES);
383 get_node (const struct GNUNET_PeerIdentity *id)
390 tmp = GNUNET_CONTAINER_multihashmap_get (nodes_active, &id->hashPubKey);
394 tmp = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &id->hashPubKey);
398 GNUNET_break (0); /* Multiple instances */
400 tmp = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &id->hashPubKey);
404 GNUNET_break (0); /* Multiple instances */
411 * Set a specific node as active
415 static void node_make_active (struct Node *n)
418 GNUNET_CONTAINER_multihashmap_put (nodes_active,
419 &n->id.hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
420 update_stats (nodes_active);
421 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' as active node\n"),
422 GNUNET_i2s (&n->id));
423 /* Request experiments for this node to start them */
424 for (c1 = 0; c1 < n->issuer_count; c1++)
427 GED_experiments_get (n, &n->issuer_id[c1], &get_experiments_cb);
433 * Handle a request and send a response
435 * @param peer the source
436 * @param message the message
438 static void handle_request (const struct GNUNET_PeerIdentity *peer,
439 const struct GNUNET_MessageHeader *message)
442 struct NodeComCtx *e_ctx;
443 struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
444 struct Experimentation_Issuer *rmi = (struct Experimentation_Issuer *) &rm[1];
448 uint32_t ic_accepted;
451 if (ntohs (message->size) < sizeof (struct Experimentation_Request))
456 ic = ntohl (rm->issuer_count);
457 if (ntohs (message->size) != sizeof (struct Experimentation_Request) + ic * sizeof (struct Experimentation_Issuer))
463 make_active = GNUNET_NO;
464 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
468 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
470 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
471 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
473 GNUNET_SCHEDULER_cancel (n->timeout_task);
474 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
476 update_stats (nodes_requested);
477 make_active = GNUNET_YES;
479 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
481 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
482 update_stats (nodes_inactive);
483 make_active = GNUNET_YES;
487 /* Create new node */
488 n = GNUNET_malloc (sizeof (struct Node));
490 n->capabilities = NONE;
491 make_active = GNUNET_YES;
495 n->capabilities = ntohl (rm->capabilities);
497 /* Filter accepted issuer */
499 for (c1 = 0; c1 < ic; c1++)
501 if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Request from peer `%s' with %u issuers, we accepted %u issuer \n",
505 GNUNET_i2s (peer), ic, ic_accepted);
506 GNUNET_free_non_null (n->issuer_id);
507 n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
509 for (c1 = 0; c1 < ic; c1++)
511 if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
513 n->issuer_id[c2] = rmi[c1].issuer_id;
517 n->issuer_count = ic_accepted;
519 if (GNUNET_YES == make_active)
520 node_make_active (n);
523 e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
526 e_ctx->size = sizeof (struct Experimentation_Response) + GSE_my_issuer_count * sizeof (struct Experimentation_Issuer);
527 e_ctx->notify = &send_response_cb;
528 e_ctx->notify_cls = n;
530 GNUNET_CONTAINER_DLL_insert_tail(n->e_req_head, n->e_req_tail, e_ctx);
531 schedule_transmisson (e_ctx);
538 * @param peer the source
539 * @param message the message
541 static void handle_response (const struct GNUNET_PeerIdentity *peer,
542 const struct GNUNET_MessageHeader *message)
545 struct Experimentation_Response *rm = (struct Experimentation_Response *) message;
546 struct Experimentation_Issuer *rmi = (struct Experimentation_Issuer *) &rm[1];
548 uint32_t ic_accepted;
553 if (ntohs (message->size) < sizeof (struct Experimentation_Response))
558 ic = ntohl (rm->issuer_count);
559 if (ntohs (message->size) != sizeof (struct Experimentation_Response) + ic * sizeof (struct Experimentation_Issuer))
565 make_active = GNUNET_NO;
566 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from %s peer `%s'\n",
569 "RESPONSE", "active", GNUNET_i2s (peer));
571 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from %s peer `%s'\n",
574 "RESPONSE", "requested", GNUNET_i2s (peer));
575 GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
576 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
578 GNUNET_SCHEDULER_cancel (n->timeout_task);
579 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
581 update_stats (nodes_requested);
582 make_active = GNUNET_YES;
584 else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from peer `%s'\n",
587 "RESPONSE", "inactive", GNUNET_i2s (peer));
588 GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
589 update_stats (nodes_inactive);
590 make_active = GNUNET_YES;
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from %s peer `%s'\n",
595 "RESPONSE", "unknown", GNUNET_i2s (peer));
600 n->capabilities = ntohl (rm->capabilities);
602 /* Filter accepted issuer */
604 for (c1 = 0; c1 < ic; c1++)
606 if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Response from peer `%s' with %u issuers, we accepted %u issuer \n",
610 GNUNET_i2s (peer), ic, ic_accepted);
611 GNUNET_free_non_null (n->issuer_id);
612 n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
614 for (c1 = 0; c1 < ic; c1++)
616 if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
618 n->issuer_id[c2] = rmi[c1].issuer_id;
622 n->issuer_count = ic_accepted;
624 if (GNUNET_YES == make_active)
625 node_make_active (n);
631 * @param peer the source
632 * @param message the message
634 static void handle_start (const struct GNUNET_PeerIdentity *peer,
635 const struct GNUNET_MessageHeader *message)
639 const struct GED_start_message *msg;
642 struct Experiment *e;
655 size = ntohs (message->size);
656 if (size < sizeof (struct GED_start_message))
661 msg = (const struct GED_start_message *) message;
662 name_len = ntohl (msg->len_name);
663 if (size != sizeof (struct GED_start_message) + name_len)
675 name = (const char *) &msg[1];
676 if (name[name_len-1] != '\0')
682 if (name_len != strlen (name) + 1)
688 e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
695 GED_scheduler_handle_start (n, e);
701 * @param peer the source
702 * @param message the message
704 static void handle_start_ack (const struct GNUNET_PeerIdentity *peer,
705 const struct GNUNET_MessageHeader *message)
709 const struct GED_start_ack_message *msg;
712 struct Experiment *e;
725 size = ntohs (message->size);
726 if (size < sizeof (struct GED_start_ack_message))
731 msg = (const struct GED_start_ack_message *) message;
732 name_len = ntohl (msg->len_name);
733 if (size != sizeof (struct GED_start_message) + name_len)
745 name = (const char *) &msg[1];
746 if (name[name_len-1] != '\0')
752 if (name_len != strlen (name) + 1)
758 e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
764 GED_scheduler_handle_start_ack (n, e);
770 * @param peer the source
771 * @param message the message
773 static void handle_stop (const struct GNUNET_PeerIdentity *peer,
774 const struct GNUNET_MessageHeader *message)
778 const struct GED_stop_message *msg;
781 struct Experiment *e;
794 size = ntohs (message->size);
795 if (size < sizeof (struct GED_stop_message))
800 msg = (const struct GED_stop_message *) message;
801 name_len = ntohl (msg->len_name);
802 if (size != sizeof (struct GED_start_message) + name_len)
814 name = (const char *) &msg[1];
815 if (name[name_len-1] != '\0')
821 if (name_len != strlen (name) + 1)
827 e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
833 GED_scheduler_handle_stop (n, e);
837 * Method called whenever a given peer connects.
840 * @param peer peer identity this notification is about
842 void core_connect_handler (void *cls,
843 const struct GNUNET_PeerIdentity *peer)
845 if (GNUNET_YES == is_me(peer))
848 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
851 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &peer->hashPubKey))
852 return; /* We already sent a request */
854 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_active, &peer->hashPubKey))
855 return; /* This peer is known as active */
857 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
858 return; /* This peer is known as inactive */
860 send_experimentation_request (peer);
865 * Method called whenever a given peer disconnects.
868 * @param peer peer identity this notification is about
870 void core_disconnect_handler (void *cls,
871 const struct GNUNET_PeerIdentity * peer)
874 if (GNUNET_YES == is_me(peer))
877 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
880 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
881 cleanup_node (nodes_requested, &peer->hashPubKey, n);
883 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
884 cleanup_node (nodes_active, &peer->hashPubKey, n);
886 if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
887 cleanup_node (nodes_inactive, &peer->hashPubKey, n);
892 * Handle a request and send a response
895 * @param other the sender
896 * @param message the message
897 * @return GNUNET_OK to keep connection, GNUNET_SYSERR on error
900 core_receive_handler (void *cls,
901 const struct GNUNET_PeerIdentity *other,
902 const struct GNUNET_MessageHeader *message)
904 if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
907 return GNUNET_SYSERR;
910 switch (ntohs (message->type)) {
911 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
912 handle_request (other, message);
914 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
915 handle_response (other, message);
917 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START:
918 handle_start (other, message);
920 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK:
921 handle_start_ack (other, message);
923 case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_STOP:
924 handle_stop (other, message);
934 size_t node_experiment_start_cb (void *cls, size_t bufsize, void *buf)
936 struct NodeComCtx *e_ctx = cls;
937 struct GED_start_message *msg;
944 name_len = strlen(e_ctx->e->name) + 1;
945 size = sizeof (struct GED_start_message) + name_len;
947 msg = GNUNET_malloc (size);
948 msg->header.size = htons (size);
949 msg->header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START);
950 msg->issuer = e_ctx->e->issuer;
951 msg->version_nbo = GNUNET_TIME_absolute_hton(e_ctx->e->version);
952 msg->len_name = htonl (name_len);
953 memcpy (&msg[1], e_ctx->e->name, name_len);
955 memcpy (buf, msg, size);
960 size_t node_experiment_start_ack_cb (void *cls, size_t bufsize, void *buf)
962 struct NodeComCtx *e_ctx = cls;
963 struct GED_start_ack_message *msg;
969 name_len = strlen(e_ctx->e->name) + 1;
970 size = sizeof (struct GED_start_ack_message) + name_len;
972 msg = GNUNET_malloc (size);
973 msg->header.size = htons (size);
974 msg->header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK);
975 msg->issuer = e_ctx->e->issuer;
976 msg->version_nbo = GNUNET_TIME_absolute_hton(e_ctx->e->version);
977 msg->len_name = htonl (name_len);
978 memcpy (&msg[1], e_ctx->e->name, name_len);
980 memcpy (buf, msg, size);
989 * Confirm a experiment START with a node
991 * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
994 GED_nodes_send_start_ack (struct Node *n, struct Experiment *e)
996 struct NodeComCtx *e_ctx;
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Sending %s for experiment request to peer `%s' for experiment `%s'\n",
1000 "START_ACK" ,GNUNET_i2s(&n->id), e->name);
1002 e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
1005 e_ctx->size = sizeof (struct GED_start_ack_message) + strlen (e->name) + 1;
1006 e_ctx->notify = &node_experiment_start_ack_cb;
1007 e_ctx->notify_cls = e_ctx;
1009 GNUNET_CONTAINER_DLL_insert_tail (n->e_req_head, n->e_req_tail, e_ctx);
1010 schedule_transmisson (e_ctx);
1016 * Request a experiment to start with a node
1018 * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
1021 GED_nodes_send_start (struct Node *n, struct Experiment *e)
1023 struct NodeComCtx *e_ctx;
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026 "Sending %s for experiment request to peer `%s' for experiment `%s'\n",
1027 "START", GNUNET_i2s(&n->id), e->name);
1029 e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
1032 e_ctx->size = sizeof (struct GED_start_message) + strlen (e->name) + 1;
1033 e_ctx->notify = &node_experiment_start_cb;
1034 e_ctx->notify_cls = e_ctx;
1036 GNUNET_CONTAINER_DLL_insert_tail (n->e_req_head, n->e_req_tail, e_ctx);
1037 schedule_transmisson (e_ctx);
1043 * Start the nodes management
1048 /* Connecting to core service to find partners */
1049 ch = GNUNET_CORE_connect (GED_cfg, NULL,
1050 &core_startup_handler,
1051 &core_connect_handler,
1052 &core_disconnect_handler,
1053 &core_receive_handler,
1054 GNUNET_NO, NULL, GNUNET_NO, NULL);
1057 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
1061 nodes_requested = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1062 nodes_active = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1063 nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1068 * Stop the nodes management
1075 GNUNET_CORE_disconnect (ch);
1079 if (NULL != nodes_requested)
1081 GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
1084 update_stats (nodes_requested);
1085 GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
1086 nodes_requested = NULL;
1089 if (NULL != nodes_active)
1091 GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
1094 update_stats (nodes_active);
1095 GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
1096 nodes_active = NULL;
1099 if (NULL != nodes_inactive)
1101 GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
1104 update_stats (nodes_inactive);
1105 GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);
1106 nodes_inactive = NULL;
1110 /* end of gnunet-daemon-experimentation_nodes.c */