+ const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
+/**
+ * Insert the given request plan into the heap with the appropriate weight.
+ *
+ * @param pp associated peer's plan
+ * @param rp request to plan
+ */
+static void
+plan (struct PeerPlan *pp, struct GSF_RequestPlan *rp)
+{
+#define N ((double)128.0)
+ /**
+ * Running average delay we currently impose.
+ */
+ static double avg_delay;
+
+ struct GSF_PendingRequestData *prd;
+ struct GNUNET_TIME_Relative delay;
+
+ GNUNET_assert (rp->pp == pp);
+ GNUNET_STATISTICS_set (GSF_stats,
+ gettext_noop ("# average retransmission delay (ms)"),
+ total_delay * 1000LL / plan_count, GNUNET_NO);
+ prd = GSF_pending_request_get_data_ (rp->pe_head->pr);
+
+ if (rp->transmission_counter < 8)
+ delay =
+ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
+ rp->transmission_counter);
+ else if (rp->transmission_counter < 32)
+ delay =
+ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
+ 8 +
+ (1LL << (rp->transmission_counter - 8)));
+ else
+ delay =
+ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
+ 8 + (1LL << 24));
+ delay.rel_value_us =
+ GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+ delay.rel_value_us + 1);
+ /* Add 0.01 to avg_delay to avoid division-by-zero later */
+ avg_delay = (((avg_delay * (N - 1.0)) + delay.rel_value_us) / N) + 0.01;
+
+ /*
+ * For the priority, we need to consider a few basic rules:
+ * 1) if we just started requesting (delay is small), we should
+ * virtually always have a priority of zero.
+ * 2) for requests with average latency, our priority should match
+ * the average priority observed on the network
+ * 3) even the longest-running requests should not be WAY out of
+ * the observed average (thus we bound by a factor of 2)
+ * 4) we add +1 to the observed average priority to avoid everyone
+ * staying put at zero (2 * 0 = 0...).
+ *
+ * Using the specific calculation below, we get:
+ *
+ * delay = 0 => priority = 0;
+ * delay = avg delay => priority = running-average-observed-priority;
+ * delay >> avg_delay => priority = 2 * running-average-observed-priority;
+ *
+ * which satisfies all of the rules above.
+ *
+ * Note: M_PI_4 = PI/4 = arctan(1)
+ */
+ rp->priority =
+ round ((GSF_current_priorities +
+ 1.0) * atan (delay.rel_value_us / avg_delay)) / M_PI_4;
+ /* Note: usage of 'round' and 'atan' requires -lm */
+
+ if (rp->transmission_counter != 0)
+ delay.rel_value_us += TTL_DECREMENT * 1000;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Considering (re)transmission number %u in %s\n",
+ (unsigned int) rp->transmission_counter,
+ GNUNET_STRINGS_relative_time_to_string (delay,
+ GNUNET_YES));
+ rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Earliest (re)transmission for `%s' in %us\n",
+ GNUNET_h2s (&prd->query), rp->transmission_counter);
+ GNUNET_assert (rp->hn == NULL);
+ if (0 == GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value_us)
+ rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
+ else
+ rp->hn =
+ GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
+ rp->earliest_transmission.abs_value_us);
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
+ get_rp_key (rp),
+ rp));
+ if (NULL != pp->task)
+ GNUNET_SCHEDULER_cancel (pp->task);
+ pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
+#undef N
+}
+
+
+/**
+ * Get the pending request with the highest TTL from the given plan.
+ *
+ * @param rp plan to investigate
+ * @return pending request with highest TTL
+ */
+struct GSF_PendingRequest *
+get_latest (const struct GSF_RequestPlan *rp)
+{
+ struct GSF_PendingRequest *ret;
+ struct GSF_PendingRequestPlanBijection *bi;
+
+ bi = rp->pe_head;
+ if (NULL == bi)
+ return NULL; /* should never happen */
+ ret = bi->pr;
+ bi = bi->next_PE;
+ while (NULL != bi)
+ {
+ if (GSF_pending_request_get_data_ (bi->pr)->ttl.abs_value_us >
+ GSF_pending_request_get_data_ (ret)->ttl.abs_value_us)
+ ret = bi->pr;
+ bi = bi->next_PE;
+ }
+ return ret;
+}