renaming stats and cfg
[oweals/gnunet.git] / src / experimentation / gnunet-daemon-experimentation_nodes.c
index 266f82ce38cdd14741b39331337d816ecc4e3242..667c9da578a63bc017b87158759394c47d073b16 100644 (file)
@@ -71,21 +71,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
@@ -121,7 +121,7 @@ cleanup_nodes (void *cls,
                GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
                n->cth = NULL;
        }
-
+       GNUNET_free_non_null (n->issuer_id);
 
        GNUNET_CONTAINER_multihashmap_remove (cur, key, value);
        GNUNET_free (value);
@@ -199,12 +199,15 @@ remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param buf the buffer to copy to
  * @return bytes passed
  */
-size_t send_request_cb (void *cls, size_t bufsize, void *buf)
+size_t send_experimentation_request_cb (void *cls, size_t bufsize, void *buf)
 {
        struct Node *n = cls;
        struct Experimentation_Request msg;
-       size_t size = sizeof (msg);
+       size_t msg_size = sizeof (msg);
+       size_t ri_size = sizeof (struct Experimentation_Issuer) * GSE_my_issuer_count;
+       size_t total_size = msg_size + ri_size;
 
+       memset (buf, '0', bufsize);
        n->cth = NULL;
   if (buf == NULL)
   {
@@ -215,41 +218,46 @@ size_t send_request_cb (void *cls, size_t bufsize, void *buf)
     GNUNET_SCHEDULER_add_now (&remove_request, n);
     return 0;
   }
-  GNUNET_assert (bufsize >= size);
+  GNUNET_assert (bufsize >= total_size);
 
-       msg.msg.size = htons (size);
+       msg.msg.size = htons (total_size);
        msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
        msg.capabilities = htonl (GSE_node_capabilities);
-       memcpy (buf, &msg, size);
+       msg.issuer_count = htonl (GSE_my_issuer_count);
+       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_i2s (&n->id));
-       return size;
+       return total_size;
 }
 
 
 /**
- * Send request
+ * Send request to peer to start add him to to the set of experimentation nodes
  *
  * @param peer the peer to send to
  */
-static void send_request (const struct GNUNET_PeerIdentity *peer)
+static void send_experimentation_request (const struct GNUNET_PeerIdentity *peer)
 {
        struct Node *n;
        size_t size;
+       size_t c_issuers;
 
-       size = sizeof (struct Experimentation_Request);
+       c_issuers = GSE_my_issuer_count;
+
+       size = sizeof (struct Experimentation_Request) +
+                                c_issuers * sizeof (struct Experimentation_Issuer);
        n = GNUNET_malloc (sizeof (struct Node));
        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_request_cb, n);
-       n->capabilities = NONE;
+                                                               peer, size, send_experimentation_request_cb, n);
 
        GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
                        &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
-
        update_stats (nodes_requested);
 }
 
@@ -266,7 +274,9 @@ size_t send_response_cb (void *cls, size_t bufsize, void *buf)
 {
        struct Node *n = cls;
        struct Experimentation_Response msg;
-       size_t size = sizeof (msg);
+       size_t ri_size = GSE_my_issuer_count * sizeof (struct Experimentation_Issuer);
+       size_t msg_size = sizeof (msg);
+       size_t total_size = msg_size + ri_size;
 
        n->cth = NULL;
   if (buf == NULL)
@@ -275,19 +285,41 @@ size_t send_response_cb (void *cls, size_t bufsize, void *buf)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected\n");
     return 0;
   }
-  GNUNET_assert (bufsize >= size);
+  GNUNET_assert (bufsize >= total_size);
 
-       msg.msg.size = htons (size);
+       msg.msg.size = htons (total_size);
        msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
        msg.capabilities = htonl (GSE_node_capabilities);
-       memcpy (buf, &msg, size);
+       msg.issuer_count = htonl (GSE_my_issuer_count);
+       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_i2s (&n->id));
-       return size;
+       return total_size;
 }
 
 
+static void
+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));
+
+       /* Tell the scheduler to add a node with an experiment */
+       GED_scheduler_add (n, e);
+       counter ++;
+}
+
 /**
  * Set a specific node as active
  *
@@ -295,11 +327,19 @@ size_t send_response_cb (void *cls, size_t bufsize, void *buf)
  */
 static void node_make_active (struct Node *n)
 {
+       int c1;
   GNUNET_CONTAINER_multihashmap_put (nodes_active,
                        &n->id.hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
        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++)
+       {
+
+               GED_experiments_get (n, &n->issuer_id[c1], &get_experiments_cb);
+       }
 }
 
 
@@ -314,18 +354,32 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
 {
        struct Node *n;
        struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
+       struct Experimentation_Issuer *rmi = (struct Experimentation_Issuer *) &rm[1];
+       int c1;
+       int c2;
+       uint32_t ic;
+       uint32_t ic_accepted;
+       int make_active;
+
+       if (ntohs (message->size) < sizeof (struct Experimentation_Request))
+       {
+               GNUNET_break (0);
+               return;
+       }
+       ic = ntohl (rm->issuer_count);
+       if (ntohs (message->size) != sizeof (struct Experimentation_Request) + ic * sizeof (struct Experimentation_Issuer))
+       {
+               GNUNET_break (0);
+               return;
+       }
 
+       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"),
-                                       "REQUEST", "active", GNUNET_i2s (peer));
-                       n->capabilities = ntohl (rm->capabilities);
+                       /* Nothing to do */
        }
        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"),
-                                       "REQUEST", "requested", GNUNET_i2s (peer));
-                       n->capabilities = ntohl (rm->capabilities);
                        GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
                        if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
                        {
@@ -338,16 +392,13 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
                                n->cth = NULL;
                        }
                        update_stats (nodes_requested);
-                       node_make_active (n);
+                       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 %s peer `%s'\n"),
-                                       "REQUEST", "inactive", GNUNET_i2s (peer));
-                       n->capabilities = ntohl (rm->capabilities);
                        GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
                        update_stats (nodes_inactive);
-                       node_make_active (n);
+                       make_active = GNUNET_YES;
        }
        else
        {
@@ -355,14 +406,43 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
                        n = GNUNET_malloc (sizeof (struct Node));
                        n->id = *peer;
                        n->capabilities = NONE;
-                       n->capabilities = ntohl (rm->capabilities);
-                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received %s from %s peer `%s'\n"),
-                                       "REQUEST", "new", GNUNET_i2s (peer));
-                       node_make_active (n);
+                       make_active = GNUNET_YES;
        }
+
+       /* Update node */
+       n->capabilities = ntohl (rm->capabilities);
+
+       /* Filter accepted issuer */
+       ic_accepted = 0;
+       for (c1 = 0; c1 < ic; c1++)
+       {
+               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_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 == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
+                       {
+                               n->issuer_id[c2] = rmi[c1].issuer_id;
+                               c2 ++;
+                       }
+       }
+       n->issuer_count = ic_accepted;
+
+       if (GNUNET_YES == make_active)
+               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),
+                                                               peer,
+                                                               sizeof (struct Experimentation_Response) +
+                                                               GSE_my_issuer_count * sizeof (struct Experimentation_Issuer),
                                                                send_response_cb, n);
 }
 
@@ -377,19 +457,37 @@ static void handle_response (const struct GNUNET_PeerIdentity *peer,
                                                                                                                 const struct GNUNET_MessageHeader *message)
 {
        struct Node *n;
-       struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
+       struct Experimentation_Response *rm = (struct Experimentation_Response *) message;
+       struct Experimentation_Issuer *rmi = (struct Experimentation_Issuer *) &rm[1];
+       uint32_t ic;
+       uint32_t ic_accepted;
+       int make_active;
+       unsigned int c1;
+       unsigned int c2;
+
+
+       if (ntohs (message->size) < sizeof (struct Experimentation_Response))
+       {
+               GNUNET_break (0);
+               return;
+       }
+       ic = ntohl (rm->issuer_count);
+       if (ntohs (message->size) != sizeof (struct Experimentation_Response) + ic * sizeof (struct Experimentation_Issuer))
+       {
+               GNUNET_break (0);
+               return;
+       }
 
+       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"),
                                        "RESPONSE", "active", GNUNET_i2s (peer));
-                       n->capabilities = ntohl (rm->capabilities);
        }
        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"),
                                        "RESPONSE", "requested", GNUNET_i2s (peer));
-                       n->capabilities = ntohl (rm->capabilities);
                        GNUNET_CONTAINER_multihashmap_remove (nodes_requested, &peer->hashPubKey, n);
                        if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
                        {
@@ -402,16 +500,15 @@ static void handle_response (const struct GNUNET_PeerIdentity *peer,
                                n->cth = NULL;
                        }
                        update_stats (nodes_requested);
-                       node_make_active (n);
+                       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"),
                                        "RESPONSE", "inactive", GNUNET_i2s (peer));
-                       n->capabilities = ntohl (rm->capabilities);
                        GNUNET_CONTAINER_multihashmap_remove (nodes_inactive, &peer->hashPubKey, n);
                        update_stats (nodes_inactive);
-                       node_make_active (n);
+                       make_active = GNUNET_YES;
        }
        else
        {
@@ -419,6 +516,70 @@ static void handle_response (const struct GNUNET_PeerIdentity *peer,
                                        "RESPONSE", "unknown", GNUNET_i2s (peer));
                        return;
        }
+
+       /* Update */
+       n->capabilities = ntohl (rm->capabilities);
+
+       /* Filter accepted issuer */
+       ic_accepted = 0;
+       for (c1 = 0; c1 < ic; c1++)
+       {
+               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_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 == GED_experiments_issuer_accepted(&rmi[c1].issuer_id))
+                       {
+                               n->issuer_id[c2] = rmi[c1].issuer_id;
+                               c2 ++;
+                       }
+       }
+       n->issuer_count = ic_accepted;
+
+       if (GNUNET_YES == make_active)
+               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)
+{
+       GED_scheduler_handle_start (NULL, NULL);
+}
+
+/**
+ * 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)
+{
+       GED_scheduler_handle_start_ack (NULL, NULL);
+}
+
+/**
+ * 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)
+{
+       GED_scheduler_handle_stop (NULL, NULL);
 }
 
 /**
@@ -445,7 +606,7 @@ void core_connect_handler (void *cls,
        if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (nodes_inactive, &peer->hashPubKey))
                return; /* This peer is known as inactive  */
 
-       send_request (peer);
+       send_experimentation_request (peer);
 
 }
 
@@ -494,6 +655,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;
        }
@@ -501,16 +671,99 @@ core_receive_handler (void *cls,
        return GNUNET_OK;
 }
 
+#define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
+
+struct GNUNET_EXPERIMENTATION_start_message
+{
+       struct GNUNET_MessageHeader header;
+};
+
+struct ExperimentStartCtx
+{
+       struct ExperimentStartCtx *prev;
+       struct ExperimentStartCtx *next;
+
+       struct Node *n;
+       struct Experiment *e;
+};
+
+size_t node_experiment_start_cb (void *cls, size_t bufsize, void *buf)
+{
+       struct ExperimentStartCtx *e_ctx = cls;
+       struct GNUNET_EXPERIMENTATION_start_message msg;
+
+       GNUNET_CONTAINER_DLL_remove (e_ctx->n->e_req_head, e_ctx->n->e_req_tail, e_ctx);
+       e_ctx->n->cth = NULL;
+       if (NULL == buf)
+       {
+               GNUNET_free (e_ctx);
+               return 0;
+       }
+
+       size_t size = sizeof (struct GNUNET_EXPERIMENTATION_start_message);
+       msg.header.size = htons (size);
+       msg.header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START);
+
+       memcpy (buf, &msg, size);
+       GNUNET_free (e_ctx);
+       return size;
+}
+
+int
+GED_nodes_rts (struct Node *n)
+{
+       if (NULL == n->cth)
+               return GNUNET_YES;
+       else
+               return GNUNET_NO;
+
+}
+
+/**
+ * Request a experiment to start with a node
+ *
+ * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
+ */
+int
+GED_nodes_request_start (struct Node *n, struct Experiment *e)
+{
+       struct ExperimentStartCtx *e_ctx;
+
+       if (NULL != n->cth)
+       {
+               GNUNET_break (0); /* should call rts before */
+               return GNUNET_NO;
+       }
+
+       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending experiment start request to peer `%s' for experiment `%s'\n"),
+                       GNUNET_i2s(&n->id), e->name);
+
+       e_ctx = GNUNET_malloc (sizeof (struct ExperimentStartCtx));
+       e_ctx->n = n;
+       e_ctx->e = e;
+       n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0, FAST_TIMEOUT, &n->id,
+                       sizeof (struct GNUNET_EXPERIMENTATION_start_message),
+                       &node_experiment_start_cb, e_ctx);
+       if (NULL == n->cth)
+       {
+               GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Cannot send experiment start request to peer `%s' for experiment `%s'\n"),
+                               GNUNET_i2s(&n->id), e->name);
+               GNUNET_free (e_ctx);
+       }
+       GNUNET_CONTAINER_DLL_insert (n->e_req_head, n->e_req_tail, e_ctx);
+
+       return GNUNET_OK;
+}
 
 
 /**
  * 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,
@@ -532,7 +785,7 @@ GNUNET_EXPERIMENTATION_nodes_start ()
  * Stop the nodes management
  */
 void
-GNUNET_EXPERIMENTATION_nodes_stop ()
+GED_nodes_stop ()
 {
   if (NULL != ch)
   {