-only trigger check config if we actually need it
[oweals/gnunet.git] / src / fs / gnunet-service-fs_pe.c
index beb435c0c224a0aca415f32a5f3af5b6d0331025..b338c1a13127b51666853815fc13b25aa68e4ee4 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2011 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -59,7 +59,7 @@ struct PeerPlan;
  * to a given plan entry.)
  *
  * Similarly head and tail of the "PR" MDLL are stored
- * with the 'struct GSF_PendingRequest'.  (We need
+ * with the `struct GSF_PendingRequest`.  (We need
  * to be able to lookup all plan entries corresponding
  * to a given pending request.)
  */
@@ -87,12 +87,14 @@ struct GSF_PendingRequestPlanBijection
   struct GSF_PendingRequestPlanBijection *prev_PE;
 
   /**
-   * Associated request plan.
+   * Associated request plan (tells us one of the peers that
+   * we plan to forward the request to).
    */
   struct GSF_RequestPlan *rp;
 
   /**
-   * Associated pending request.
+   * Associated pending request (identifies request details
+   * and one of the origins of the request).
    */
   struct GSF_PendingRequest *pr;
 
@@ -101,8 +103,8 @@ struct GSF_PendingRequestPlanBijection
 
 /**
  * Information we keep per request per peer.  This is a doubly-linked
- * list (with head and tail in the 'struct GSF_PendingRequestData')
- * with one entry in each heap of each 'struct PeerPlan'.  Each
+ * list (with head and tail in the `struct GSF_PendingRequestData`)
+ * with one entry in each heap of each `struct PeerPlan`.  Each
  * entry tracks information relevant for this request and this peer.
  */
 struct GSF_RequestPlan
@@ -129,7 +131,9 @@ struct GSF_RequestPlan
   struct PeerPlan *pp;
 
   /**
-   * Head of list of associated pending requests.
+   * Head of list of associated pending requests.  This tells us
+   * which incoming requests from other peers this plan entry
+   * corresponds to.
    */
   struct GSF_PendingRequestPlanBijection *pe_head;
 
@@ -167,19 +171,20 @@ struct GSF_RequestPlan
 struct PeerPlan
 {
   /**
-   * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
+   * Heap with pending queries (`struct GSF_RequestPlan`), higher weights mean higher priority.
    */
   struct GNUNET_CONTAINER_Heap *priority_heap;
 
   /**
-   * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
+   * Heap with pending queries (`struct GSF_RequestPlan`), by transmission time, lowest first.
    */
   struct GNUNET_CONTAINER_Heap *delay_heap;
 
   /**
-   * Map of queries to plan entries.  All entries in the priority_heap or delay_heap
-   * should be in the plan map.  Note that it IS possible for the plan map to have
-   * multiple entries for the same query.
+   * Map of queries to plan entries.  All entries in the @e priority_heap
+   * or @e delay_heap should be in the @e plan_map.  Note that it is
+   * possible for the @e plan_map to have multiple entries for the same
+   * query.
    */
   struct GNUNET_CONTAINER_MultiHashMap *plan_map;
 
@@ -196,7 +201,7 @@ struct PeerPlan
   /**
    * Current task for executing the plan.
    */
-  struct GNUNET_SCHEDULER_Task * task;
+  struct GNUNET_SCHEDULER_Task *task;
 };
 
 
@@ -222,7 +227,7 @@ static unsigned long long plan_count;
  * requests for the same key and we just return _one_ of them; this
  * particular one might complete while another one might still be
  * active, hence the lifetime of the returned hash code is NOT
- * necessarily identical to that of the 'struct GSF_RequestPlan'
+ * necessarily identical to that of the `struct GSF_RequestPlan`
  * given.
  *
  * @param rp a request plan
@@ -239,11 +244,9 @@ get_rp_key (struct GSF_RequestPlan *rp)
  * Figure out when and how to transmit to the given peer.
  *
  * @param cls the `struct GSF_ConnectedPeer` for transmission
- * @param tc scheduler context
  */
 static void
-schedule_peer_transmission (void *cls,
-                            const struct GNUNET_SCHEDULER_TaskContext *tc);
+schedule_peer_transmission (void *cls);
 
 
 /**
@@ -253,7 +256,8 @@ schedule_peer_transmission (void *cls,
  * @param rp request to plan
  */
 static void
-plan (struct PeerPlan *pp, struct GSF_RequestPlan *rp)
+plan (struct PeerPlan *pp,
+      struct GSF_RequestPlan *rp)
 {
 #define N ((double)128.0)
   /**
@@ -355,18 +359,24 @@ get_latest (const struct GSF_RequestPlan *rp)
 {
   struct GSF_PendingRequest *ret;
   struct GSF_PendingRequestPlanBijection *bi;
+  const struct GSF_PendingRequestData *rprd;
+  const struct GSF_PendingRequestData *prd;
 
   bi = rp->pe_head;
   if (NULL == bi)
     return NULL; /* should never happen */
   ret = bi->pr;
-  bi = bi->next_PE;
-  while (NULL != bi)
+  rprd = GSF_pending_request_get_data_ (ret);
+  for (bi = bi->next_PE; NULL != bi; bi = bi->next_PE)
   {
-    if (GSF_pending_request_get_data_ (bi->pr)->ttl.abs_value_us >
-        GSF_pending_request_get_data_ (ret)->ttl.abs_value_us)
+    GNUNET_break (GNUNET_YES ==
+                  GSF_pending_request_test_active_ (bi->pr));
+    prd = GSF_pending_request_get_data_ (bi->pr);
+    if (prd->ttl.abs_value_us > rprd->ttl.abs_value_us)
+    {
       ret = bi->pr;
-    bi = bi->next_PE;
+      rprd = prd;
+    }
   }
   return ret;
 }
@@ -378,10 +388,12 @@ get_latest (const struct GSF_RequestPlan *rp)
  * @param cls closure
  * @param buf_size number of bytes available in @a buf
  * @param buf where to copy the message, NULL on error (peer disconnect)
- * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
+ * @return number of bytes copied to @a buf, can be 0 (without indicating an error)
  */
 static size_t
-transmit_message_callback (void *cls, size_t buf_size, void *buf)
+transmit_message_callback (void *cls,
+                           size_t buf_size,
+                           void *buf)
 {
   struct PeerPlan *pp = cls;
   struct GSF_RequestPlan *rp;
@@ -409,7 +421,9 @@ transmit_message_callback (void *cls, size_t buf_size, void *buf)
     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
     return 0;
   }
-  msize = GSF_pending_request_get_message_ (get_latest (rp), buf_size, buf);
+  msize = GSF_pending_request_get_message_ (get_latest (rp),
+                                            buf_size,
+                                            buf);
   if (msize > buf_size)
   {
     if (NULL != pp->task)
@@ -419,7 +433,8 @@ transmit_message_callback (void *cls, size_t buf_size, void *buf)
     return 0;
   }
   /* remove from root, add again elsewhere... */
-  GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
+  GNUNET_assert (rp ==
+                 GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
   rp->hn = NULL;
   rp->last_transmission = GNUNET_TIME_absolute_get ();
   rp->transmission_counter++;
@@ -429,8 +444,8 @@ transmit_message_callback (void *cls, size_t buf_size, void *buf)
               rp, rp->transmission_counter);
   plan (pp, rp);
   GNUNET_STATISTICS_update (GSF_stats,
-                            gettext_noop
-                            ("# query messages sent to other peers"), 1,
+                            gettext_noop ("# query messages sent to other peers"),
+                            1,
                             GNUNET_NO);
   return msize;
 }
@@ -440,11 +455,9 @@ transmit_message_callback (void *cls, size_t buf_size, void *buf)
  * Figure out when and how to transmit to the given peer.
  *
  * @param cls the `struct PeerPlan`
- * @param tc scheduler context
  */
 static void
-schedule_peer_transmission (void *cls,
-                            const struct GNUNET_SCHEDULER_TaskContext *tc)
+schedule_peer_transmission (void *cls)
 {
   struct PeerPlan *pp = cls;
   struct GSF_RequestPlan *rp;
@@ -463,7 +476,9 @@ schedule_peer_transmission (void *cls,
           (rp->earliest_transmission).rel_value_us))
   {
     GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
-    rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
+    rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
+                                           rp,
+                                           rp->priority);
   }
   if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
   {
@@ -471,7 +486,8 @@ schedule_peer_transmission (void *cls,
     rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
     if (NULL == rp)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No active requests for plan %p.\n",
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "No active requests for plan %p.\n",
                   pp);
       return;                   /* both queues empty */
     }
@@ -481,11 +497,14 @@ schedule_peer_transmission (void *cls,
                 GNUNET_STRINGS_relative_time_to_string (delay,
                                                        GNUNET_YES),
                pp);
-    GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# delay heap timeout (ms)"),
+    GNUNET_STATISTICS_set (GSF_stats,
+                           gettext_noop ("# delay heap timeout (ms)"),
                            delay.rel_value_us / 1000LL, GNUNET_NO);
 
     pp->task =
-        GNUNET_SCHEDULER_add_delayed (delay, &schedule_peer_transmission, pp);
+        GNUNET_SCHEDULER_add_delayed (delay,
+                                      &schedule_peer_transmission,
+                                      pp);
     return;
   }
 #if INSANE_STATISTICS
@@ -494,12 +513,16 @@ schedule_peer_transmission (void *cls,
 #endif
   /* process from priority heap */
   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing query plan %p\n", rp);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Executing query plan %p\n",
+              rp);
   GNUNET_assert (NULL != rp);
   msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
   pp->pth =
-      GSF_peer_transmit_ (pp->cp, GNUNET_YES, rp->priority,
-                          GNUNET_TIME_UNIT_FOREVER_REL, msize,
+      GSF_peer_transmit_ (pp->cp, GNUNET_YES,
+                          rp->priority,
+                          GNUNET_TIME_UNIT_FOREVER_REL,
+                          msize,
                           &transmit_message_callback, pp);
   GNUNET_assert (NULL != pp->pth);
 }
@@ -511,8 +534,14 @@ schedule_peer_transmission (void *cls,
 struct MergeContext
 {
 
+  /**
+   * Request we are trying to merge.
+   */
   struct GSF_PendingRequest *pr;
 
+  /**
+   * Set to #GNUNET_YES if we succeeded to merge.
+   */
   int merged;
 
 };
@@ -539,6 +568,8 @@ merge_pr (void *cls,
   struct GSF_PendingRequestPlanBijection *bi;
   struct GSF_PendingRequest *latest;
 
+  GNUNET_break (GNUNET_YES ==
+                GSF_pending_request_test_active_ (mpr->pr));
   if (GNUNET_OK !=
       GSF_pending_request_is_compatible_ (mpr->pr,
                                           rp->pe_head->pr))
@@ -559,7 +590,8 @@ merge_pr (void *cls,
   mpr->merged = GNUNET_YES;
 #if INSANE_STATISTICS
   GNUNET_STATISTICS_update (GSF_stats,
-                            gettext_noop ("# requests merged"), 1,
+                            gettext_noop ("# requests merged"),
+                            1,
                             GNUNET_NO);
 #endif
   latest = get_latest (rp);
@@ -569,7 +601,8 @@ merge_pr (void *cls,
 #if INSANE_STATISTICS
     GNUNET_STATISTICS_update (GSF_stats,
                               gettext_noop ("# requests refreshed"),
-                              1, GNUNET_NO);
+                              1,
+                              GNUNET_NO);
 #endif
     rp->transmission_counter = 0;       /* reset */
   }
@@ -594,6 +627,8 @@ GSF_plan_add_ (struct GSF_ConnectedPeer *cp,
   struct GSF_PendingRequestPlanBijection *bi;
   struct MergeContext mpc;
 
+  GNUNET_assert (GNUNET_YES ==
+                 GSF_pending_request_test_active_ (pr));
   GNUNET_assert (NULL != cp);
   id = GSF_connected_peer_get_identity2_ (cp);
   pp = GNUNET_CONTAINER_multipeermap_get (plans, id);
@@ -689,12 +724,19 @@ GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
   {
     GNUNET_break (GNUNET_YES ==
                   GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
-                                                        get_rp_key (rp), rp));
+                                                        get_rp_key (rp),
+                                                        rp));
     while (NULL != (bi = rp->pe_head))
     {
-      GNUNET_CONTAINER_MDLL_remove (PE, rp->pe_head, rp->pe_tail, bi);
+      GNUNET_CONTAINER_MDLL_remove (PE,
+                                    rp->pe_head,
+                                    rp->pe_tail,
+                                    bi);
       prd = GSF_pending_request_get_data_ (bi->pr);
-      GNUNET_CONTAINER_MDLL_remove (PR, prd->pr_head, prd->pr_tail, bi);
+      GNUNET_CONTAINER_MDLL_remove (PR,
+                                    prd->pr_head,
+                                    prd->pr_tail,
+                                    bi);
       GNUNET_free (bi);
     }
     plan_count--;
@@ -705,19 +747,28 @@ GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
   {
     GNUNET_break (GNUNET_YES ==
                   GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
-                                                        get_rp_key (rp), rp));
+                                                        get_rp_key (rp),
+                                                        rp));
     while (NULL != (bi = rp->pe_head))
     {
       prd = GSF_pending_request_get_data_ (bi->pr);
-      GNUNET_CONTAINER_MDLL_remove (PE, rp->pe_head, rp->pe_tail, bi);
-      GNUNET_CONTAINER_MDLL_remove (PR, prd->pr_head, prd->pr_tail, bi);
+      GNUNET_CONTAINER_MDLL_remove (PE,
+                                    rp->pe_head,
+                                    rp->pe_tail,
+                                    bi);
+      GNUNET_CONTAINER_MDLL_remove (PR,
+                                    prd->pr_head,
+                                    prd->pr_tail,
+                                    bi);
       GNUNET_free (bi);
     }
     plan_count--;
     GNUNET_free (rp);
   }
-  GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
-                         plan_count, GNUNET_NO);
+  GNUNET_STATISTICS_set (GSF_stats,
+                         gettext_noop ("# query plan entries"),
+                         plan_count,
+                         GNUNET_NO);
   GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
   GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
   GNUNET_free (pp);
@@ -772,23 +823,31 @@ GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
   while (NULL != (bi = prd->pr_head))
   {
     rp = bi->rp;
-    GNUNET_CONTAINER_MDLL_remove (PR, prd->pr_head, prd->pr_tail, bi);
-    GNUNET_CONTAINER_MDLL_remove (PE, rp->pe_head, rp->pe_tail, bi);
+    GNUNET_CONTAINER_MDLL_remove (PR,
+                                  prd->pr_head,
+                                  prd->pr_tail,
+                                  bi);
+    GNUNET_CONTAINER_MDLL_remove (PE,
+                                  rp->pe_head,
+                                  rp->pe_tail,
+                                  bi);
+    GNUNET_assert (bi->pr == pr);
     if (NULL == rp->pe_head)
     {
       GNUNET_CONTAINER_heap_remove_node (rp->hn);
       plan_count--;
       GNUNET_break (GNUNET_YES ==
                     GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
-                                                         &GSF_pending_request_get_data_
-                                                         (bi->pr)->query,
+                                                         &prd->query,
                                                           rp));
       GNUNET_free (rp);
     }
     GNUNET_free (bi);
   }
-  GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
-                         plan_count, GNUNET_NO);
+  GNUNET_STATISTICS_set (GSF_stats,
+                         gettext_noop ("# query plan entries"),
+                         plan_count,
+                         GNUNET_NO);
 }
 
 
@@ -798,7 +857,8 @@ GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
 void
 GSF_plan_init ()
 {
-  plans = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_YES);
+  plans = GNUNET_CONTAINER_multipeermap_create (256,
+                                                GNUNET_YES);
 }