-removing dead options
[oweals/gnunet.git] / src / experimentation / gnunet-daemon-experimentation_nodes.c
index cb94ddca0a9ef0d7c4255d7e4bff7450bc05694e..ac1d00ce0491c2846fa7828c7fa55bd06285d38b 100644 (file)
 #include "gnunet_statistics_service.h"
 #include "gnunet-daemon-experimentation.h"
 
+
+/**
+ * Core handle
+ */
 static struct GNUNET_CORE_Handle *ch;
 
+
+/**
+ * Peer's own identity
+ */
 static struct GNUNET_PeerIdentity me;
 
+
 /**
  * Nodes with a pending request
  */
-
 struct GNUNET_CONTAINER_MultiHashMap *nodes_requested;
 
+
 /**
  * Active experimentation nodes
  */
 struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
 
+
 /**
  * Inactive experimentation nodes
  * To be excluded from future requests
@@ -53,6 +63,11 @@ struct GNUNET_CONTAINER_MultiHashMap *nodes_active;
 struct GNUNET_CONTAINER_MultiHashMap *nodes_inactive;
 
 
+/**
+ * Update statistics
+ *
+ * @param m hashmap to update values from
+ */
 static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
 {
        GNUNET_assert (NULL != m);
@@ -78,6 +93,15 @@ static void update_stats (struct GNUNET_CONTAINER_MultiHashMap *m)
 
 }
 
+
+/**
+ * Clean up nodes
+ *
+ * @param cls the hashmap to clean up
+ * @param key key of the current node
+ * @param value related node object
+ * @return always GNUNET_OK
+ */
 static int
 cleanup_nodes (void *cls,
                                                         const struct GNUNET_HashCode * key,
@@ -97,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);
@@ -105,6 +129,12 @@ cleanup_nodes (void *cls,
 }
 
 
+/**
+ * Check if id passed is my id
+ *
+ * @param id the id to check
+ * @return GNUNET_YES or GNUNET_NO
+ */
 static int is_me (const struct GNUNET_PeerIdentity *id)
 {
        if (0 == memcmp (&me, id, sizeof (me)))
@@ -113,14 +143,28 @@ static int is_me (const struct GNUNET_PeerIdentity *id)
                return GNUNET_NO;
 }
 
+/**
+ * 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,
+                                                                                       struct GNUNET_CORE_Handle *server,
                       const struct GNUNET_PeerIdentity *my_identity)
 {
        me = *my_identity;
 }
 
+
+/**
+ * Remove experimentation request due to timeout
+ *
+ * @param cls the related node
+ * @param tc scheduler's task context
+ */
 static void
 remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
@@ -146,12 +190,24 @@ remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
        }
 }
 
+
+/**
+ * Core's transmit notify callback to send request
+ *
+ * @param cls the related node
+ * @param bufsize buffer size
+ * @param buf the buffer to copy to
+ * @return bytes passed
+ */
 size_t send_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)
   {
@@ -162,43 +218,65 @@ 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
+ *
+ * @param peer the peer to send to
+ */
 static void send_request (const struct GNUNET_PeerIdentity *peer)
 {
        struct Node *n;
        size_t size;
+       size_t c_issuers;
+
+       c_issuers = GSE_my_issuer_count;
 
-       size = sizeof (struct Experimentation_Request);
+       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;
 
        GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (nodes_requested,
                        &peer->hashPubKey, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
-
        update_stats (nodes_requested);
 }
 
+
+/**
+ * Core's transmit notify callback to send response
+ *
+ * @param cls the related node
+ * @param bufsize buffer size
+ * @param buf the buffer to copy to
+ * @return bytes passed
+ */
 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)
@@ -207,45 +285,102 @@ 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));
+
+       /* Request experiment */
+       GNUNET_EXPERIMENTATION_scheduler_add (n, e);
+
+       counter ++;
 }
 
+/**
+ * Set a specific node as active
+ *
+ * @param n the node
+ */
 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++)
+       {
+
+               GNUNET_EXPERIMENTATION_experiments_get (n, &n->issuer_id[c1], &get_experiments_cb);
+       }
 }
 
 
+/**
+ * Handle a request and send a response
+ *
+ * @param peer the source
+ * @param message the message
+ */
 static void handle_request (const struct GNUNET_PeerIdentity *peer,
                                                                                                                const struct GNUNET_MessageHeader *message)
 {
        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)
                        {
@@ -258,16 +393,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
        {
@@ -275,34 +407,88 @@ 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 == GNUNET_EXPERIMENTATION_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 == 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);
+
+       /* 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);
 }
 
+
+/**
+ * Handle a response
+ *
+ * @param peer the source
+ * @param message the message
+ */
 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)
                        {
@@ -315,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
        {
@@ -332,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);
 }
 
 /**
@@ -381,6 +594,14 @@ void core_disconnect_handler (void *cls,
 }
 
 
+/**
+ * Handle a request and send a response
+ *
+ * @param cls unused
+ * @param other the sender
+ * @param message the message
+ * @return GNUNET_OK to keep connection, GNUNET_SYSERR on error
+ */
 static int
 core_receive_handler (void *cls,
                                                                                        const struct GNUNET_PeerIdentity *other,
@@ -407,11 +628,16 @@ 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
- *
- * @param cfg configuration handle
  */
 void
 GNUNET_EXPERIMENTATION_nodes_start ()
@@ -434,6 +660,7 @@ GNUNET_EXPERIMENTATION_nodes_start ()
        nodes_inactive = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
 }
 
+
 /**
  * Stop the nodes management
  */