- replace deprecated INCLUDES with AM_CPPFLAGS
[oweals/gnunet.git] / src / experimentation / gnunet-daemon-experimentation_nodes.c
index a77e841b877e6940f3ca3e372806b1011d1ad1bb..a4da92d50e8d7449f4d8c8c477330b70fe26ab45 100644 (file)
@@ -32,6 +32,7 @@
 #include "gnunet-daemon-experimentation.h"
 
 
+#define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
 /**
  * Core handle
  */
@@ -62,6 +63,19 @@ struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
  */
 struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
 
+struct NodeComCtx
+{
+       struct NodeComCtx *prev;
+       struct NodeComCtx *next;
+
+       struct Node *n;
+       struct Experiment *e;
+
+       size_t size;
+       GNUNET_CONNECTION_TransmitReadyNotify notify;
+       void *notify_cls;
+};
+
 
 /**
  * Update statistics
@@ -71,21 +85,21 @@ struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
 static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
 {
        GNUNET_assert (NULL != m);
-       GNUNET_assert (NULL != GSE_stats);
+       GNUNET_assert (NULL != GED_stats);
 
        if (m == nodes_active)
        {
-                       GNUNET_STATISTICS_set (GSE_stats, "# nodes active",
+                       GNUNET_STATISTICS_set (GED_stats, "# nodes active",
                                        GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
        }
        else if (m == nodes_inactive)
        {
-                       GNUNET_STATISTICS_set (GSE_stats, "# nodes inactive",
+                       GNUNET_STATISTICS_set (GED_stats, "# nodes inactive",
                                        GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
        }
        else if (m == nodes_requested)
        {
-                       GNUNET_STATISTICS_set (GSE_stats, "# nodes requested",
+                       GNUNET_STATISTICS_set (GED_stats, "# nodes requested",
                                        GNUNET_CONTAINER_multihashmap_size(m), GNUNET_NO);
        }
        else
@@ -95,7 +109,7 @@ static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
 
 
 /**
- * Clean up nodes
+ * Clean up node
  *
  * @param cls the hashmap to clean up
  * @param key key of the current node
@@ -103,11 +117,13 @@ static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
  * @return always GNUNET_OK
  */
 static int
-cleanup_nodes (void *cls,
+cleanup_node (void *cls,
                                                         const struct GNUNET_HashCode * key,
                                                         void *value)
 {
        struct Node *n;
+       struct NodeComCtx *e_cur;
+       struct NodeComCtx *e_next;
        struct GNUNET_CONTAINER_MultiHashMap *cur = cls;
 
        n = value;
@@ -116,14 +132,23 @@ cleanup_nodes (void *cls,
                GNUNET_SCHEDULER_cancel (n->timeout_task);
                n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
        }
+
        if (NULL != n->cth)
        {
                GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
                n->cth = NULL;
        }
+       e_next = n->e_req_head;
+       while (NULL != (e_cur = e_next))
+       {
+               e_next = e_cur->next;
+               GNUNET_CONTAINER_DLL_remove (n->e_req_head, n->e_req_tail, e_cur);
+               GNUNET_free (e_cur);
+       }
+
        GNUNET_free_non_null (n->issuer_id);
 
-       GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
+       GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_remove (cur, key, value));
        GNUNET_free (value);
        return GNUNET_OK;
 }
@@ -147,15 +172,57 @@ static int is_me (const struct GNUNET_PeerIdentity *id)
  * Core startup callback
  *
  * @param cls unused
- * @param server core service's server handle
  * @param my_identity my id
  */
 static void
 core_startup_handler (void *cls,
-                                                                                       struct GNUNET_CORE_Handle *server,
                       const struct GNUNET_PeerIdentity *my_identity)
 {
-       me = *my_identity;
+  me = *my_identity;
+}
+
+
+static void
+schedule_transmisson (struct NodeComCtx *e_ctx);
+
+
+static size_t
+transmit_read_wrapper (void *cls, size_t bufsize, void *buf)
+{
+       struct NodeComCtx *e_ctx = cls;
+       struct NodeComCtx *next = NULL;
+
+       size_t res = e_ctx->notify (e_ctx->notify_cls, bufsize, buf);
+       e_ctx->n->cth = NULL;
+
+       GNUNET_CONTAINER_DLL_remove (e_ctx->n->e_req_head, e_ctx->n->e_req_tail, e_ctx);
+       next = e_ctx->n->e_req_head;
+       GNUNET_free (e_ctx);
+
+       if (NULL != next)
+       {
+               /* Schedule next message */
+               schedule_transmisson (next);
+       }
+       return res;
+}
+
+
+static void
+schedule_transmisson (struct NodeComCtx *e_ctx)
+{
+       if (NULL != e_ctx->n->cth)
+               return;
+
+       e_ctx->n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0, FAST_TIMEOUT,
+                       &e_ctx->n->id, e_ctx->size, transmit_read_wrapper, e_ctx);
+       if (NULL == e_ctx->n->cth)
+       {
+               GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Cannot send message to peer `%s' for experiment `%s'\n"),
+                               GNUNET_i2s(&e_ctx->n->id), e_ctx->e->name);
+               GNUNET_free (e_ctx);
+       }
+
 }
 
 
@@ -170,24 +237,18 @@ remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
        struct Node *n = cls;
 
-       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Removing request for peer %s due to timeout\n"),
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing request for peer %s due to timeout\n",
                        GNUNET_i2s (&n->id));
 
        if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_requested, &n->id.hashPubKey))
        {
-                       GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n);
+                       GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &n->id.hashPubKey, n));
                        update_stats (nodes_requested);
                        GNUNET_CONTAINER_multihashmap_put (nodes_inactive, &n->id.hashPubKey, n,
                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
                        update_stats (nodes_inactive);
        }
-
        n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
-       if (NULL != n->cth)
-       {
-               GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
-               n->cth = NULL;
-       }
 }
 
 
@@ -207,7 +268,7 @@ size_t send_experimentation_request_cb (void *cls, size_t bufsize, void *buf)
        size_t ri_size = sizeof (struct Experimentation_Issuer) * GSE_my_issuer_count;
        size_t total_size = msg_size + ri_size;
 
-       memset (buf, '0', bufsize);
+       memset (buf, '\0', bufsize);
        n->cth = NULL;
   if (buf == NULL)
   {
@@ -227,7 +288,7 @@ size_t send_experimentation_request_cb (void *cls, size_t bufsize, void *buf)
        memcpy (buf, &msg, msg_size);
        memcpy (&((char *) buf)[msg_size], GSE_my_issuer, ri_size);
 
-       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending request to peer %s\n"),
+       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending experimentation request to peer %s\n"),
                        GNUNET_i2s (&n->id));
        return total_size;
 }
@@ -241,6 +302,7 @@ size_t send_experimentation_request_cb (void *cls, size_t bufsize, void *buf)
 static void send_experimentation_request (const struct GNUNET_PeerIdentity *peer)
 {
        struct Node *n;
+       struct NodeComCtx *e_ctx;
        size_t size;
        size_t c_issuers;
 
@@ -252,9 +314,15 @@ static void send_experimentation_request (const struct GNUNET_PeerIdentity *peer
        n->id = *peer;
        n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
        n->capabilities = NONE;
-       n->cth = GNUNET_CORE_notify_transmit_ready(ch, GNUNET_NO, 0,
-                                                               GNUNET_TIME_relative_get_forever_(),
-                                                               peer, size, send_experimentation_request_cb, n);
+
+       e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
+       e_ctx->n = n;
+       e_ctx->e = NULL;
+       e_ctx->size = size;
+       e_ctx->notify = &send_experimentation_request_cb;
+       e_ctx->notify_cls = n;
+       GNUNET_CONTAINER_DLL_insert_tail(n->e_req_head, n->e_req_tail, e_ctx);
+       schedule_transmisson (e_ctx);
 
        GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
                        &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
@@ -294,7 +362,7 @@ size_t send_response_cb (void *cls, size_t bufsize, void *buf)
        memcpy (buf, &msg, msg_size);
        memcpy (&((char *) buf)[msg_size], GSE_my_issuer, ri_size);
 
-       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending response to peer %s\n"),
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending response to peer %s\n",
                        GNUNET_i2s (&n->id));
        return total_size;
 }
@@ -305,21 +373,41 @@ get_experiments_cb (struct Node *n, struct Experiment *e)
 {
        static int counter = 0;
        if (NULL == e)
-       {
-                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added %u experiments for peer %s\n"),
-                                       counter, GNUNET_i2s (&n->id));
-                       return;
-       }
-
-       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting experiment `%s' with peer %s\n"),
-                       e->name,
-                       GNUNET_i2s (&n->id));
+                       return; /* Done */
 
        /* Tell the scheduler to add a node with an experiment */
-       GNUNET_EXPERIMENTATION_scheduler_add (n, e);
+       GED_scheduler_add (n, e, GNUNET_YES);
        counter ++;
 }
 
+struct Node *
+get_node (const struct GNUNET_PeerIdentity *id)
+{
+       struct Node * res;
+       struct Node * tmp;
+
+       res = NULL;
+       tmp = NULL;
+       tmp = GNUNET_CONTAINER_multihashmap_get (nodes_active, &id->hashPubKey);
+       if (res == NULL)
+               res = tmp;
+
+       tmp = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &id->hashPubKey);
+       if (res == NULL)
+               res = tmp;
+       else
+               GNUNET_break (0); /* Multiple instances */
+
+       tmp = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &id->hashPubKey);
+       if (res == NULL)
+               res = tmp;
+       else
+               GNUNET_break (0); /* Multiple instances */
+
+       return res;
+}
+
+
 /**
  * Set a specific node as active
  *
@@ -333,12 +421,11 @@ static void node_make_active (struct Node *n)
        update_stats (nodes_active);
        GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Added peer `%s' as active node\n"),
                        GNUNET_i2s (&n->id));
-
        /* Request experiments for this node to start them */
        for (c1 = 0; c1 < n->issuer_count; c1++)
        {
 
-               GNUNET_EXPERIMENTATION_experiments_get (n, &n->issuer_id[c1], &get_experiments_cb);
+               GED_experiments_get (n, &n->issuer_id[c1], &get_experiments_cb);
        }
 }
 
@@ -353,6 +440,7 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
                                                                                                                const struct GNUNET_MessageHeader *message)
 {
        struct Node *n;
+       struct NodeComCtx *e_ctx;
        struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
        struct Experimentation_Issuer *rmi = (struct Experimentation_Issuer *) &rm[1];
        int c1;
@@ -386,17 +474,12 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
                                GNUNET_SCHEDULER_cancel (n->timeout_task);
                                n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
                        }
-                       if (NULL != n->cth)
-                       {
-                               GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
-                               n->cth = NULL;
-                       }
                        update_stats (nodes_requested);
                        make_active = GNUNET_YES;
        }
        else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
        {
-                       GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
+                 GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n));
                        update_stats (nodes_inactive);
                        make_active = GNUNET_YES;
        }
@@ -416,17 +499,17 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
        ic_accepted = 0;
        for (c1 = 0; c1 < ic; c1++)
        {
-               if (GNUNET_YES == GNUNET_EXPERIMENTATION_experiments_issuer_accepted(&rmi[c1].issuer_id))
+               if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
                        ic_accepted ++;
        }
-       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Request from peer `%s' with %u issuers, we accepted %u issuer \n"),
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Request from peer `%s' with %u issuers, we accepted %u issuer \n",
                        GNUNET_i2s (peer), ic, ic_accepted);
        GNUNET_free_non_null (n->issuer_id);
        n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
        c2 = 0;
        for (c1 = 0; c1 < ic; c1++)
        {
-                       if (GNUNET_YES == GNUNET_EXPERIMENTATION_experiments_issuer_accepted(&rmi[c1].issuer_id))
+                       if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
                        {
                                n->issuer_id[c2] = rmi[c1].issuer_id;
                                c2 ++;
@@ -438,12 +521,15 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
                node_make_active (n);
 
        /* Send response */
-       n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0,
-                                                               GNUNET_TIME_relative_get_forever_(),
-                                                               peer,
-                                                               sizeof (struct Experimentation_Response) +
-                                                               GSE_my_issuer_count * sizeof (struct Experimentation_Issuer),
-                                                               send_response_cb, n);
+       e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
+       e_ctx->n = n;
+       e_ctx->e = NULL;
+       e_ctx->size = sizeof (struct Experimentation_Response) + GSE_my_issuer_count * sizeof (struct Experimentation_Issuer);
+       e_ctx->notify = &send_response_cb;
+       e_ctx->notify_cls = n;
+
+       GNUNET_CONTAINER_DLL_insert_tail(n->e_req_head, n->e_req_tail, e_ctx);
+       schedule_transmisson (e_ctx);
 }
 
 
@@ -465,7 +551,6 @@ static void handle_response (const struct GNUNET_PeerIdentity *peer,
        unsigned int c1;
        unsigned int c2;
 
-
        if (ntohs (message->size) < sizeof (struct Experimentation_Response))
        {
                GNUNET_break (0);
@@ -481,12 +566,12 @@ static void handle_response (const struct GNUNET_PeerIdentity *peer,
        make_active = GNUNET_NO;
        if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
        {
-                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
+                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from %s peer `%s'\n",
                                        "RESPONSE", "active", GNUNET_i2s (peer));
        }
        else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
        {
-                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
+                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from %s peer `%s'\n",
                                        "RESPONSE", "requested", GNUNET_i2s (peer));
                        GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
                        if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
@@ -494,25 +579,20 @@ static void handle_response (const struct GNUNET_PeerIdentity *peer,
                                GNUNET_SCHEDULER_cancel (n->timeout_task);
                                n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
                        }
-                       if (NULL != n->cth)
-                       {
-                               GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
-                               n->cth = NULL;
-                       }
                        update_stats (nodes_requested);
                        make_active = GNUNET_YES;
        }
        else if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
        {
-                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from peer `%s'\n"),
+                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from peer `%s'\n",
                                        "RESPONSE", "inactive", GNUNET_i2s (peer));
-                       GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
+                       GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n));
                        update_stats (nodes_inactive);
                        make_active = GNUNET_YES;
        }
        else
        {
-                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
+                       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from %s peer `%s'\n",
                                        "RESPONSE", "unknown", GNUNET_i2s (peer));
                        return;
        }
@@ -524,17 +604,17 @@ static void handle_response (const struct GNUNET_PeerIdentity *peer,
        ic_accepted = 0;
        for (c1 = 0; c1 < ic; c1++)
        {
-               if (GNUNET_YES == GNUNET_EXPERIMENTATION_experiments_issuer_accepted(&rmi[c1].issuer_id))
+               if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
                        ic_accepted ++;
        }
-       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Response from peer `%s' with %u issuers, we accepted %u issuer \n"),
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Response from peer `%s' with %u issuers, we accepted %u issuer \n",
                        GNUNET_i2s (peer), ic, ic_accepted);
        GNUNET_free_non_null (n->issuer_id);
        n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
        c2 = 0;
        for (c1 = 0; c1 < ic; c1++)
        {
-                       if (GNUNET_YES == GNUNET_EXPERIMENTATION_experiments_issuer_accepted(&rmi[c1].issuer_id))
+                       if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
                        {
                                n->issuer_id[c2] = rmi[c1].issuer_id;
                                c2 ++;
@@ -546,6 +626,214 @@ static void handle_response (const struct GNUNET_PeerIdentity *peer,
                node_make_active (n);
 }
 
+/**
+ * Handle a response
+ *
+ * @param peer the source
+ * @param message the message
+ */
+static void handle_start (const struct GNUNET_PeerIdentity *peer,
+                                                                                                                const struct GNUNET_MessageHeader *message)
+{
+       uint16_t size;
+       uint32_t name_len;
+       const struct GED_start_message *msg;
+       const char *name;
+       struct Node *n;
+       struct Experiment *e;
+
+       if (NULL == peer)
+       {
+               GNUNET_break (0);
+               return;
+       }
+       if (NULL == message)
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       size = ntohs (message->size);
+       if (size < sizeof (struct GED_start_message))
+       {
+               GNUNET_break (0);
+               return;
+       }
+       msg = (const struct GED_start_message *) message;
+       name_len = ntohl (msg->len_name);
+       if (size != sizeof (struct GED_start_message) + name_len)
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       n = get_node (peer);
+       if (NULL == n)
+       {
+               GNUNET_break (0);
+               return;
+       }
+       name = (const char *) &msg[1];
+       if (name[name_len-1] != '\0')
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       if (name_len != strlen (name) + 1)
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
+       if (NULL == e)
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       GED_scheduler_handle_start (n, e);
+}
+
+/**
+ * Handle a response
+ *
+ * @param peer the source
+ * @param message the message
+ */
+static void handle_start_ack (const struct GNUNET_PeerIdentity *peer,
+                                                                                                                const struct GNUNET_MessageHeader *message)
+{
+       uint16_t size;
+       uint32_t name_len;
+       const struct GED_start_ack_message *msg;
+       const char *name;
+       struct Node *n;
+       struct Experiment *e;
+
+       if (NULL == peer)
+       {
+               GNUNET_break (0);
+               return;
+       }
+       if (NULL == message)
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       size = ntohs (message->size);
+       if (size < sizeof (struct GED_start_ack_message))
+       {
+               GNUNET_break (0);
+               return;
+       }
+       msg = (const struct GED_start_ack_message *) message;
+       name_len = ntohl (msg->len_name);
+       if (size != sizeof (struct GED_start_message) + name_len)
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       n = get_node (peer);
+       if (NULL == n)
+       {
+               GNUNET_break (0);
+               return;
+       }
+       name = (const char *) &msg[1];
+       if (name[name_len-1] != '\0')
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       if (name_len != strlen (name) + 1)
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
+       if (NULL == e)
+       {
+               GNUNET_break (0);
+               return;
+       }
+       GED_scheduler_handle_start_ack (n, e);
+}
+
+/**
+ * Handle a response
+ *
+ * @param peer the source
+ * @param message the message
+ */
+static void handle_stop (const struct GNUNET_PeerIdentity *peer,
+                                                                                                const struct GNUNET_MessageHeader *message)
+{
+       uint16_t size;
+       uint32_t name_len;
+       const struct GED_stop_message *msg;
+       const char *name;
+       struct Node *n;
+       struct Experiment *e;
+
+       if (NULL == peer)
+       {
+               GNUNET_break (0);
+               return;
+       }
+       if (NULL == message)
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       size = ntohs (message->size);
+       if (size < sizeof (struct GED_stop_message))
+       {
+               GNUNET_break (0);
+               return;
+       }
+       msg = (const struct GED_stop_message *) message;
+       name_len = ntohl (msg->len_name);
+       if (size != sizeof (struct GED_start_message) + name_len)
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       n = get_node (peer);
+       if (NULL == n)
+       {
+               GNUNET_break (0);
+               return;
+       }
+       name = (const char *) &msg[1];
+       if (name[name_len-1] != '\0')
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       if (name_len != strlen (name) + 1)
+       {
+               GNUNET_break (0);
+               return;
+       }
+
+       e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
+       if (NULL == e)
+       {
+               GNUNET_break (0);
+               return;
+       }
+       GED_scheduler_handle_stop (n, e);
+}
+
 /**
  * Method called whenever a given peer connects.
  *
@@ -571,7 +859,6 @@ void core_connect_handler (void *cls,
                return; /* This peer is known as inactive  */
 
        send_experimentation_request (peer);
-
 }
 
 
@@ -584,12 +871,21 @@ void core_connect_handler (void *cls,
 void core_disconnect_handler (void *cls,
                            const struct GNUNET_PeerIdentity * peer)
 {
+       struct Node *n;
        if (GNUNET_YES == is_me(peer))
                return;
 
        GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
                        GNUNET_i2s (peer));
 
+       if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_requested, &peer->hashPubKey)))
+               cleanup_node (nodes_requested, &peer->hashPubKey, n);
+
+       if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_active, &peer->hashPubKey)))
+               cleanup_node (nodes_active, &peer->hashPubKey, n);
+
+       if (NULL != (n = GNUNET_CONTAINER_multihashmap_get (nodes_inactive, &peer->hashPubKey)))
+               cleanup_node (nodes_inactive, &peer->hashPubKey, n);
 }
 
 
@@ -619,6 +915,15 @@ core_receive_handler (void *cls,
                case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
                        handle_response (other, message);
                        break;
+               case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START:
+                       handle_start (other, message);
+                       break;
+               case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK:
+                       handle_start_ack (other, message);
+                       break;
+               case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_STOP:
+                       handle_stop (other, message);
+                       break;
                default:
                        break;
        }
@@ -627,11 +932,111 @@ core_receive_handler (void *cls,
 }
 
 
-void
-GNUNET_EXPERIMENT_nodes_request_start (struct Node *n, struct Experiment *e)
+size_t node_experiment_start_cb (void *cls, size_t bufsize, void *buf)
 {
-       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending start request to peer `%s' for experiment `%s'\n"),
-                       GNUNET_i2s(&n->id), e->name);
+       struct NodeComCtx *e_ctx = cls;
+       struct GED_start_message *msg;
+       size_t name_len;
+       size_t size;
+
+       if (NULL == buf)
+               return 0;
+
+       name_len = strlen(e_ctx->e->name) + 1;
+       size = sizeof (struct GED_start_message) + name_len;
+
+       msg = GNUNET_malloc (size);
+       msg->header.size = htons (size);
+       msg->header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START);
+       msg->issuer = e_ctx->e->issuer;
+       msg->version_nbo = GNUNET_TIME_absolute_hton(e_ctx->e->version);
+       msg->len_name = htonl (name_len);
+       memcpy (&msg[1], e_ctx->e->name, name_len);
+
+       memcpy (buf, msg, size);
+       GNUNET_free (msg);
+       return size;
+}
+
+size_t node_experiment_start_ack_cb (void *cls, size_t bufsize, void *buf)
+{
+       struct NodeComCtx *e_ctx = cls;
+       struct GED_start_ack_message *msg;
+       size_t name_len;
+       size_t size;
+       if (NULL == buf)
+               return 0;
+
+       name_len = strlen(e_ctx->e->name) + 1;
+       size = sizeof (struct GED_start_ack_message) + name_len;
+
+       msg = GNUNET_malloc (size);
+       msg->header.size = htons (size);
+       msg->header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK);
+       msg->issuer = e_ctx->e->issuer;
+       msg->version_nbo = GNUNET_TIME_absolute_hton(e_ctx->e->version);
+       msg->len_name = htonl (name_len);
+       memcpy (&msg[1], e_ctx->e->name, name_len);
+
+       memcpy (buf, msg, size);
+       GNUNET_free (msg);
+       return size;
+}
+
+
+
+
+/**
+ * Confirm a experiment START with a node
+ *
+ * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
+ */
+int
+GED_nodes_send_start_ack (struct Node *n, struct Experiment *e)
+{
+       struct NodeComCtx *e_ctx;
+
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                       "Sending %s for experiment request to peer `%s' for experiment `%s'\n",
+                       "START_ACK" ,GNUNET_i2s(&n->id), e->name);
+
+       e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
+       e_ctx->n = n;
+       e_ctx->e = e;
+       e_ctx->size = sizeof (struct GED_start_ack_message) + strlen (e->name) + 1;
+       e_ctx->notify = &node_experiment_start_ack_cb;
+       e_ctx->notify_cls = e_ctx;
+
+       GNUNET_CONTAINER_DLL_insert_tail (n->e_req_head, n->e_req_tail, e_ctx);
+       schedule_transmisson (e_ctx);
+       return GNUNET_OK;
+}
+
+
+/**
+ * Request a experiment to start with a node
+ *
+ * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
+ */
+int
+GED_nodes_send_start (struct Node *n, struct Experiment *e)
+{
+       struct NodeComCtx *e_ctx;
+
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                       "Sending %s for experiment request to peer `%s' for experiment `%s'\n",
+                       "START", GNUNET_i2s(&n->id), e->name);
+
+       e_ctx = GNUNET_malloc (sizeof (struct NodeComCtx));
+       e_ctx->n = n;
+       e_ctx->e = e;
+       e_ctx->size = sizeof (struct GED_start_message) + strlen (e->name) + 1;
+       e_ctx->notify = &node_experiment_start_cb;
+       e_ctx->notify_cls = e_ctx;
+
+       GNUNET_CONTAINER_DLL_insert_tail (n->e_req_head, n->e_req_tail, e_ctx);
+       schedule_transmisson (e_ctx);
+       return GNUNET_OK;
 }
 
 
@@ -639,10 +1044,10 @@ GNUNET_EXPERIMENT_nodes_request_start (struct Node *n, struct Experiment *e)
  * Start the nodes management
  */
 void
-GNUNET_EXPERIMENTATION_nodes_start ()
+GED_nodes_start ()
 {
        /* Connecting to core service to find partners */
-       ch = GNUNET_CORE_connect (GSE_cfg, NULL,
+       ch = GNUNET_CORE_connect (GED_cfg, NULL,
                                                                                                                &core_startup_handler,
                                                                                                                &core_connect_handler,
                                                                                                                &core_disconnect_handler,
@@ -664,7 +1069,7 @@ GNUNET_EXPERIMENTATION_nodes_start ()
  * Stop the nodes management
  */
 void
-GNUNET_EXPERIMENTATION_nodes_stop ()
+GED_nodes_stop ()
 {
   if (NULL != ch)
   {
@@ -675,7 +1080,7 @@ GNUNET_EXPERIMENTATION_nodes_stop ()
   if (NULL != nodes_requested)
   {
                GNUNET_CONTAINER_multihashmap_iterate (nodes_requested,
-                                                                                                                                                                        &cleanup_nodes,
+                                                                                                                                                                        &cleanup_node,
                                                                                                                                                                         nodes_requested);
                update_stats (nodes_requested);
                GNUNET_CONTAINER_multihashmap_destroy (nodes_requested);
@@ -685,7 +1090,7 @@ GNUNET_EXPERIMENTATION_nodes_stop ()
   if (NULL != nodes_active)
   {
                GNUNET_CONTAINER_multihashmap_iterate (nodes_active,
-                                                                                                                                                                        &cleanup_nodes,
+                                                                                                                                                                        &cleanup_node,
                                                                                                                                                                         nodes_active);
                update_stats (nodes_active);
                GNUNET_CONTAINER_multihashmap_destroy (nodes_active);
@@ -695,7 +1100,7 @@ GNUNET_EXPERIMENTATION_nodes_stop ()
   if (NULL != nodes_inactive)
   {
                GNUNET_CONTAINER_multihashmap_iterate (nodes_inactive,
-                                                                                                                                                                        &cleanup_nodes,
+                                                                                                                                                                        &cleanup_node,
                                                                                                                                                                         nodes_inactive);
                update_stats (nodes_inactive);
                GNUNET_CONTAINER_multihashmap_destroy (nodes_inactive);