-removing dead options
[oweals/gnunet.git] / src / experimentation / gnunet-daemon-experimentation_nodes.c
index 9d96bfa2df15a3d0539af5a87cb412fa84249601..ac1d00ce0491c2846fa7828c7fa55bd06285d38b 100644 (file)
@@ -204,7 +204,7 @@ size_t send_request_cb (void *cls, size_t bufsize, void *buf)
        struct Node *n = cls;
        struct Experimentation_Request msg;
        size_t msg_size = sizeof (msg);
-       size_t ri_size = sizeof (struct Experimentation_Request_Issuer) * GSE_my_issuer_count;
+       size_t ri_size = sizeof (struct Experimentation_Issuer) * GSE_my_issuer_count;
        size_t total_size = msg_size + ri_size;
 
        memset (buf, '0', bufsize);
@@ -225,7 +225,7 @@ size_t send_request_cb (void *cls, size_t bufsize, void *buf)
        msg.capabilities = htonl (GSE_node_capabilities);
        msg.issuer_count = htonl (GSE_my_issuer_count);
        memcpy (buf, &msg, msg_size);
-       memcpy (&buf[msg_size], GSE_my_issuer, ri_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));
@@ -247,7 +247,7 @@ static void send_request (const struct GNUNET_PeerIdentity *peer)
        c_issuers = GSE_my_issuer_count;
 
        size = sizeof (struct Experimentation_Request) +
-                                c_issuers * sizeof (struct Experimentation_Request_Issuer);
+                                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);
@@ -274,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)
@@ -283,16 +285,18 @@ 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;
 }
 
 
@@ -307,9 +311,13 @@ get_experiments_cb (struct Node *n, struct Experiment *e)
                        return;
        }
 
-       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Scheduling experiment `%s' for peer %s\n"),
+       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting experiment `%s' with peer %s\n"),
+                       e->name,
                        GNUNET_i2s (&n->id));
-       GNUNET_EXPERIMENTATION_scheduler_add (e);
+
+       /* Request experiment */
+       GNUNET_EXPERIMENTATION_scheduler_add (n, e);
+
        counter ++;
 }
 
@@ -329,7 +337,10 @@ static void node_make_active (struct Node *n)
 
        /* 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);
+       }
 }
 
 
@@ -344,7 +355,7 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
 {
        struct Node *n;
        struct Experimentation_Request *rm = (struct Experimentation_Request *) message;
-       struct Experimentation_Request_Issuer *rmi = (struct Experimentation_Request_Issuer *) &rm[1];
+       struct Experimentation_Issuer *rmi = (struct Experimentation_Issuer *) &rm[1];
        int c1;
        int c2;
        uint32_t ic;
@@ -357,7 +368,7 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
                return;
        }
        ic = ntohl (rm->issuer_count);
-       if (ntohs (message->size) != sizeof (struct Experimentation_Request) + ic * sizeof (struct Experimentation_Request_Issuer))
+       if (ntohs (message->size) != sizeof (struct Experimentation_Request) + ic * sizeof (struct Experimentation_Issuer))
        {
                GNUNET_break (0);
                return;
@@ -382,7 +393,7 @@ 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)))
        {
@@ -396,7 +407,7 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
                        n = GNUNET_malloc (sizeof (struct Node));
                        n->id = *peer;
                        n->capabilities = NONE;
-                       node_make_active (n);
+                       make_active = GNUNET_YES;
        }
 
        /* Update node */
@@ -430,7 +441,9 @@ static void handle_request (const struct GNUNET_PeerIdentity *peer,
        /* 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);
 }
 
@@ -445,19 +458,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)
                        {
@@ -470,16 +501,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
        {
@@ -487,6 +517,34 @@ 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 == GNUNET_EXPERIMENTATION_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 == GNUNET_EXPERIMENTATION_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);
 }
 
 /**
@@ -570,6 +628,13 @@ core_receive_handler (void *cls,
 }
 
 
+void
+GNUNET_EXPERIMENT_nodes_request_start (struct Node *n, struct Experiment *e)
+{
+       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Sending start request to peer `%s' for experiment `%s'\n"),
+                       GNUNET_i2s(&n->id), e->name);
+}
+
 
 /**
  * Start the nodes management