- clean up the receive switch case
[oweals/gnunet.git] / src / fs / gnunet-service-fs_pe.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file fs/gnunet-service-fs_pe.c
23  * @brief API to manage query plan
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet-service-fs.h"
28 #include "gnunet-service-fs_cp.h"
29 #include "gnunet-service-fs_pe.h"
30 #include "gnunet-service-fs_pr.h"
31
32
33 /**
34  * List of GSF_PendingRequests this request plan
35  * participates with.
36  */
37 struct PendingRequestList;
38
39 /**
40  * Transmission plan for a peer.
41  */
42 struct PeerPlan;
43
44
45 /**
46  * DLL of request plans a particular pending request is
47  * involved with.
48  */
49 struct GSF_RequestPlanReference
50 {
51
52   /**
53    * This is a doubly-linked list.
54    */
55   struct GSF_RequestPlanReference *next;
56
57   /**
58    * This is a doubly-linked list.
59    */
60   struct GSF_RequestPlanReference *prev;
61
62   /**
63    * Associated request plan.
64    */
65   struct GSF_RequestPlan *rp;
66
67   /**
68    * Corresponding PendingRequestList.
69    */
70   struct PendingRequestList *prl;
71 };
72
73
74 /**
75  * List of GSF_PendingRequests this request plan
76  * participates with.
77  */
78 struct PendingRequestList
79 {
80
81   /**
82    * This is a doubly-linked list.
83    */
84   struct PendingRequestList *next;
85
86   /**
87    * This is a doubly-linked list.
88    */
89   struct PendingRequestList *prev;
90
91   /**
92    * Associated pending request.
93    */
94   struct GSF_PendingRequest *pr;
95
96   /**
97    * Corresponding GSF_RequestPlanReference.
98    */
99   struct GSF_RequestPlanReference *rpr;
100
101 };
102
103
104 /**
105  * Information we keep per request per peer.  This is a doubly-linked
106  * list (with head and tail in the 'struct GSF_PendingRequestData')
107  * with one entry in each heap of each 'struct PeerPlan'.  Each
108  * entry tracks information relevant for this request and this peer.
109  */
110 struct GSF_RequestPlan
111 {
112
113   /**
114    * This is a doubly-linked list.
115    */
116   struct GSF_RequestPlan *next;
117
118   /**
119    * This is a doubly-linked list.
120    */
121   struct GSF_RequestPlan *prev;
122
123   /**
124    * Heap node associated with this request and this peer.
125    */
126   struct GNUNET_CONTAINER_HeapNode *hn;
127
128   /**
129    * The transmission plan for a peer that this request is associated with.
130    */
131   struct PeerPlan *pp;
132
133   /**
134    * Head of list of associated pending requests.
135    */
136   struct PendingRequestList *prl_head;
137
138   /**
139    * Tail of list of associated pending requests.
140    */
141   struct PendingRequestList *prl_tail;
142
143   /**
144    * Earliest time we'd be happy to (re)transmit this request.
145    */
146   struct GNUNET_TIME_Absolute earliest_transmission;
147
148   /**
149    * When was the last time we transmitted this request to this peer? 0 for never.
150    */
151   struct GNUNET_TIME_Absolute last_transmission;
152
153   /**
154    * Current priority for this request for this target.
155    */
156   uint64_t priority;
157
158   /**
159    * How often did we transmit this request to this peer?
160    */
161   unsigned int transmission_counter;
162
163 };
164
165
166 /**
167  * Transmission plan for a peer.
168  */
169 struct PeerPlan
170 {
171   /**
172    * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
173    */
174   struct GNUNET_CONTAINER_Heap *priority_heap;
175
176   /**
177    * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
178    */
179   struct GNUNET_CONTAINER_Heap *delay_heap;
180
181   /**
182    * Map of queries to plan entries.  All entries in the priority_heap or delay_heap
183    * should be in the plan map.  Note that it IS possible for the plan map to have
184    * multiple entries for the same query.
185    */
186   struct GNUNET_CONTAINER_MultiHashMap *plan_map;
187
188   /**
189    * Current transmission request handle.
190    */
191   struct GSF_PeerTransmitHandle *pth;
192
193   /**
194    * Peer for which this is the plan.
195    */
196   struct GSF_ConnectedPeer *cp;
197
198   /**
199    * Current task for executing the plan.
200    */
201   GNUNET_SCHEDULER_TaskIdentifier task;
202 };
203
204
205 /**
206  * Hash map from peer identities to PeerPlans.
207  */
208 static struct GNUNET_CONTAINER_MultiHashMap *plans;
209
210 /**
211  * Sum of all transmission counters (equals total delay for all plan entries).
212  */
213 static unsigned long long total_delay;
214
215 /**
216  * Number of plan entries.
217  */
218 static unsigned long long plan_count;
219
220
221 /**
222  * Return the query (key in the plan_map) for the given request plan.
223  *
224  * @param rp a request plan
225  * @return the associated query
226  */
227 static const GNUNET_HashCode *
228 get_rp_key (struct GSF_RequestPlan *rp)
229 {
230   return &GSF_pending_request_get_data_ (rp->prl_head->pr)->query;
231 }
232
233
234 /**
235  * Figure out when and how to transmit to the given peer.
236  *
237  * @param cls the 'struct GSF_ConnectedPeer' for transmission
238  * @param tc scheduler context
239  */
240 static void
241 schedule_peer_transmission (void *cls,
242                             const struct GNUNET_SCHEDULER_TaskContext *tc);
243
244
245 /**
246  * Insert the given request plan into the heap with the appropriate weight.
247  *
248  * @param pp associated peer's plan
249  * @param rp request to plan
250  */
251 static void
252 plan (struct PeerPlan *pp, struct GSF_RequestPlan *rp)
253 {
254 #define N ((double)128.0)
255   /**
256    * Running average delay we currently impose.
257    */
258   static double avg_delay;
259
260   struct GSF_PendingRequestData *prd;
261   struct GNUNET_TIME_Relative delay;
262
263   GNUNET_assert (rp->pp == pp);
264   GNUNET_STATISTICS_set (GSF_stats,
265                          gettext_noop ("# average retransmission delay (ms)"),
266                          total_delay * 1000LL / plan_count, GNUNET_NO);
267   prd = GSF_pending_request_get_data_ (rp->prl_head->pr);
268
269   if (rp->transmission_counter < 8)
270     delay =
271         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
272                                        rp->transmission_counter);
273   else if (rp->transmission_counter < 32)
274     delay =
275         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
276                                        8 +
277                                        (1LL << (rp->transmission_counter - 8)));
278   else
279     delay =
280         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
281                                        8 + (1LL << 24));
282   delay.rel_value =
283       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
284                                 delay.rel_value + 1);
285   /* Add 0.01 to avg_delay to avoid division-by-zero later */
286   avg_delay = (((avg_delay * (N - 1.0)) + delay.rel_value) / N) + 0.01;
287
288   /*
289    * For the priority, we need to consider a few basic rules:
290    * 1) if we just started requesting (delay is small), we should
291    * virtually always have a priority of zero.
292    * 2) for requests with average latency, our priority should match
293    * the average priority observed on the network
294    * 3) even the longest-running requests should not be WAY out of
295    * the observed average (thus we bound by a factor of 2)
296    * 4) we add +1 to the observed average priority to avoid everyone
297    * staying put at zero (2 * 0 = 0...).
298    *
299    * Using the specific calculation below, we get:
300    *
301    * delay = 0 => priority = 0;
302    * delay = avg delay => priority = running-average-observed-priority;
303    * delay >> avg_delay => priority = 2 * running-average-observed-priority;
304    *
305    * which satisfies all of the rules above.
306    *
307    * Note: M_PI_4 = PI/4 = arctan(1)
308    */
309   rp->priority =
310       round ((GSF_current_priorities +
311               1.0) * atan (delay.rel_value / avg_delay)) / M_PI_4;
312   /* Note: usage of 'round' and 'atan' requires -lm */
313
314   if (rp->transmission_counter != 0)
315     delay.rel_value += TTL_DECREMENT;
316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317               "Considering (re)transmission number %u in %llu ms\n",
318               (unsigned int) rp->transmission_counter,
319               (unsigned long long) delay.rel_value);
320   rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
321 #if DEBUG_FS
322   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323               "Earliest (re)transmission for `%s' in %us\n",
324               GNUNET_h2s (&prd->query), rp->transmission_counter);
325 #endif
326
327   GNUNET_assert (rp->hn == NULL);
328   if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value
329       == 0)
330     rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
331   else
332     rp->hn =
333         GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
334                                       rp->earliest_transmission.abs_value);
335   GNUNET_assert (GNUNET_YES ==
336                  GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
337                                                                get_rp_key (rp),
338                                                                rp));
339   if (GNUNET_SCHEDULER_NO_TASK != pp->task)
340     GNUNET_SCHEDULER_cancel (pp->task);
341   pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
342 #undef N
343 }
344
345
346 /**
347  * Get the pending request with the highest TTL from the given plan.
348  *
349  * @param rp plan to investigate
350  * @return pending request with highest TTL
351  */
352 struct GSF_PendingRequest *
353 get_latest (const struct GSF_RequestPlan *rp)
354 {
355   struct GSF_PendingRequest *ret;
356   struct PendingRequestList *prl;
357
358   prl = rp->prl_head;
359   ret = prl->pr;
360   prl = prl->next;
361   while (NULL != prl)
362   {
363     if (GSF_pending_request_get_data_ (prl->pr)->ttl.abs_value >
364         GSF_pending_request_get_data_ (ret)->ttl.abs_value)
365       ret = prl->pr;
366     prl = prl->next;
367   }
368   return ret;
369 }
370
371
372 /**
373  * Function called to get a message for transmission.
374  *
375  * @param cls closure
376  * @param buf_size number of bytes available in buf
377  * @param buf where to copy the message, NULL on error (peer disconnect)
378  * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
379  */
380 static size_t
381 transmit_message_callback (void *cls, size_t buf_size, void *buf)
382 {
383   struct PeerPlan *pp = cls;
384   struct GSF_RequestPlan *rp;
385   size_t msize;
386
387   pp->pth = NULL;
388   if (NULL == buf)
389   {
390     /* failed, try again... */
391     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
392     GNUNET_STATISTICS_update (GSF_stats,
393                               gettext_noop
394                               ("# transmission failed (core has no bandwidth)"),
395                               1, GNUNET_NO);
396     return 0;
397   }
398   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
399   if (NULL == rp)
400   {
401     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
402     return 0;
403   }
404   msize = GSF_pending_request_get_message_ (get_latest (rp), buf_size, buf);
405   if (msize > buf_size)
406   {
407     /* buffer to small (message changed), try again */
408     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
409     return 0;
410   }
411   /* remove from root, add again elsewhere... */
412   GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
413   rp->hn = NULL;
414   rp->last_transmission = GNUNET_TIME_absolute_get ();
415   rp->transmission_counter++;
416   total_delay++;
417 #if DEBUG_FS
418   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419               "Executing plan %p executed %u times, planning retransmission\n",
420               rp, rp->transmission_counter);
421 #endif
422   plan (pp, rp);
423   GNUNET_STATISTICS_update (GSF_stats,
424                             gettext_noop
425                             ("# query messages sent to other peers"), 1,
426                             GNUNET_NO);
427   return msize;
428 }
429
430
431 /**
432  * Figure out when and how to transmit to the given peer.
433  *
434  * @param cls the 'struct PeerPlan'
435  * @param tc scheduler context
436  */
437 static void
438 schedule_peer_transmission (void *cls,
439                             const struct GNUNET_SCHEDULER_TaskContext *tc)
440 {
441   struct PeerPlan *pp = cls;
442   struct GSF_RequestPlan *rp;
443   size_t msize;
444   struct GNUNET_TIME_Relative delay;
445
446   pp->task = GNUNET_SCHEDULER_NO_TASK;
447   if (pp->pth != NULL)
448   {
449     GSF_peer_transmit_cancel_ (pp->pth);
450     pp->pth = NULL;
451   }
452   /* move ready requests to priority queue */
453   while ((NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
454          (GNUNET_TIME_absolute_get_remaining
455           (rp->earliest_transmission).rel_value == 0))
456   {
457     GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
458     rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
459   }
460   if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
461   {
462     /* priority heap (still) empty, check for delay... */
463     rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
464     if (NULL == rp)
465     {
466 #if DEBUG_FS
467       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No active requests for plan %p.\n",
468                   pp);
469 #endif
470       return;                   /* both queues empty */
471     }
472     delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
473 #if DEBUG_FS
474     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475                 "Sleeping for %llu ms before retrying requests on plan %p.\n",
476                 (unsigned long long) delay.rel_value, pp);
477 #endif
478     GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# delay heap timeout"),
479                            delay.rel_value, GNUNET_NO);
480
481     pp->task =
482         GNUNET_SCHEDULER_add_delayed (delay, &schedule_peer_transmission, pp);
483     return;
484   }
485   GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plans executed"),
486                             1, GNUNET_NO);
487   /* process from priority heap */
488   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
489 #if DEBUG_FS > 1
490   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing query plan %p\n", rp);
491 #endif
492   GNUNET_assert (NULL != rp);
493   msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
494   pp->pth =
495       GSF_peer_transmit_ (pp->cp, GNUNET_YES, rp->priority,
496                           GNUNET_TIME_UNIT_FOREVER_REL, msize,
497                           &transmit_message_callback, pp);
498   GNUNET_assert (NULL != pp->pth);
499 }
500
501
502 /**
503  * Closure for 'merge_pr'.
504  */
505 struct MergeContext
506 {
507
508   struct GSF_PendingRequest *pr;
509
510   int merged;
511
512 };
513
514
515 /**
516  * Iterator that checks if an equivalent request is already
517  * present for this peer.
518  *
519  * @param cls closure
520  * @param query the query
521  * @param element request plan stored at the node
522  * @return GNUNET_YES if we should continue to iterate,
523  *         GNUNET_NO if not (merge success)
524  */
525 static int
526 merge_pr (void *cls, const GNUNET_HashCode * query, void *element)
527 {
528   struct MergeContext *mpr = cls;
529   struct GSF_RequestPlan *rp = element;
530   struct GSF_PendingRequestData *prd;
531   struct GSF_RequestPlanReference *rpr;
532   struct PendingRequestList *prl;
533   struct GSF_PendingRequest *latest;
534
535   if (GNUNET_OK !=
536       GSF_pending_request_is_compatible_ (mpr->pr, rp->prl_head->pr))
537     return GNUNET_YES;
538   /* merge new request with existing request plan */
539   rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
540   prl = GNUNET_malloc (sizeof (struct PendingRequestList));
541   rpr->rp = rp;
542   rpr->prl = prl;
543   prl->rpr = rpr;
544   prl->pr = mpr->pr;
545   prd = GSF_pending_request_get_data_ (mpr->pr);
546   GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
547   GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
548   mpr->merged = GNUNET_YES;
549   GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests merged"), 1,
550                             GNUNET_NO);
551   latest = get_latest (rp);
552   if (GSF_pending_request_get_data_ (latest)->ttl.abs_value <
553       prd->ttl.abs_value)
554   {
555     GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests refreshed"),
556                               1, GNUNET_NO);
557     rp->transmission_counter = 0;       /* reset */
558   }
559   return GNUNET_NO;
560 }
561
562
563 /**
564  * Create a new query plan entry.
565  *
566  * @param cp peer with the entry
567  * @param pr request with the entry
568  */
569 void
570 GSF_plan_add_ (struct GSF_ConnectedPeer *cp, struct GSF_PendingRequest *pr)
571 {
572   struct GNUNET_PeerIdentity id;
573   struct PeerPlan *pp;
574   struct GSF_PendingRequestData *prd;
575   struct GSF_RequestPlan *rp;
576   struct GSF_RequestPlanReference *rpr;
577   struct PendingRequestList *prl;
578   struct MergeContext mpc;
579
580   GNUNET_assert (NULL != cp);
581   GSF_connected_peer_get_identity_ (cp, &id);
582   pp = GNUNET_CONTAINER_multihashmap_get (plans, &id.hashPubKey);
583   if (NULL == pp)
584   {
585     pp = GNUNET_malloc (sizeof (struct PeerPlan));
586     pp->plan_map = GNUNET_CONTAINER_multihashmap_create (128);
587     pp->priority_heap =
588         GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
589     pp->delay_heap =
590         GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
591     pp->cp = cp;
592     GNUNET_CONTAINER_multihashmap_put (plans, &id.hashPubKey, pp,
593                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
594   }
595   mpc.merged = GNUNET_NO;
596   mpc.pr = pr;
597   GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
598                                               &GSF_pending_request_get_data_
599                                               (pr)->query, &merge_pr, &mpc);
600   if (mpc.merged != GNUNET_NO)
601     return;
602   GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
603                                               &GSF_pending_request_get_data_
604                                               (pr)->query, &merge_pr, &mpc);
605   if (mpc.merged != GNUNET_NO)
606     return;
607   plan_count++;
608   GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plan entries"), 1,
609                             GNUNET_NO);
610   prd = GSF_pending_request_get_data_ (pr);
611 #if DEBUG_FS
612   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
613               "Planning transmission of query `%s' to peer `%s'\n",
614               GNUNET_h2s (&prd->query), GNUNET_i2s (&id));
615 #endif
616   rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan));
617   rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
618   prl = GNUNET_malloc (sizeof (struct PendingRequestList));
619   rpr->rp = rp;
620   rpr->prl = prl;
621   prl->rpr = rpr;
622   prl->pr = pr;
623   GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
624   GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
625   rp->pp = pp;
626   GNUNET_assert (GNUNET_YES ==
627                  GNUNET_CONTAINER_multihashmap_put (pp->plan_map,
628                                                     get_rp_key (rp), rp,
629                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
630   plan (pp, rp);
631 }
632
633
634 /**
635  * Notify the plan about a peer being no longer available;
636  * destroy all entries associated with this peer.
637  *
638  * @param cp connected peer
639  */
640 void
641 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
642 {
643   struct GNUNET_PeerIdentity id;
644   struct PeerPlan *pp;
645   struct GSF_RequestPlan *rp;
646   struct GSF_PendingRequestData *prd;
647   struct PendingRequestList *prl;
648
649   GSF_connected_peer_get_identity_ (cp, &id);
650   pp = GNUNET_CONTAINER_multihashmap_get (plans, &id.hashPubKey);
651   if (NULL == pp)
652     return;                     /* nothing was ever planned for this peer */
653   GNUNET_assert (GNUNET_YES ==
654                  GNUNET_CONTAINER_multihashmap_remove (plans, &id.hashPubKey,
655                                                        pp));
656   if (NULL != pp->pth)
657     GSF_peer_transmit_cancel_ (pp->pth);
658   if (GNUNET_SCHEDULER_NO_TASK != pp->task)
659   {
660     GNUNET_SCHEDULER_cancel (pp->task);
661     pp->task = GNUNET_SCHEDULER_NO_TASK;
662   }
663   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
664   {
665     GNUNET_break (GNUNET_YES ==
666                   GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
667                                                         get_rp_key (rp), rp));
668     while (NULL != (prl = rp->prl_head))
669     {
670       GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
671       prd = GSF_pending_request_get_data_ (prl->pr);
672       GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, prl->rpr);
673       GNUNET_free (prl->rpr);
674       GNUNET_free (prl);
675     }
676     GNUNET_free (rp);
677   }
678   GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
679   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
680   {
681     GNUNET_break (GNUNET_YES ==
682                   GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
683                                                         get_rp_key (rp), rp));
684     while (NULL != (prl = rp->prl_head))
685     {
686       GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
687       prd = GSF_pending_request_get_data_ (prl->pr);
688       GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, prl->rpr);
689       GNUNET_free (prl->rpr);
690       GNUNET_free (prl);
691     }
692     GNUNET_free (rp);
693   }
694   GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
695                          plan_count, GNUNET_NO);
696
697   GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
698   GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
699   GNUNET_free (pp);
700 }
701
702 /**
703  * Get the last transmission attempt time for the request plan list
704  * referenced by 'rpr_head', that was sent to 'sender'
705  *
706  * @param rpr_head request plan reference list to check.
707  * @param sender the peer that we've sent the request to.
708  * @param result the timestamp to fill.
709  * @return GNUNET_YES if 'result' was changed, GNUNET_NO otherwise.
710  */
711 int
712 GSF_request_plan_reference_get_last_transmission_ (
713     struct GSF_RequestPlanReference *rpr_head, struct GSF_ConnectedPeer *sender,
714     struct GNUNET_TIME_Absolute *result)
715 {
716   struct GSF_RequestPlanReference *rpr;
717   for (rpr = rpr_head; rpr; rpr = rpr->next)
718   {
719     if (rpr->rp->pp->cp == sender)
720     {
721       *result = rpr->rp->last_transmission;
722       return GNUNET_YES;
723     }
724   }
725   return GNUNET_NO;
726 }
727
728 /**
729  * Notify the plan about a request being done; destroy all entries
730  * associated with this request.
731  *
732  * @param pr request that is done
733  */
734 void
735 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
736 {
737   struct GSF_RequestPlan *rp;
738   struct GSF_PendingRequestData *prd;
739   struct GSF_RequestPlanReference *rpr;
740
741   prd = GSF_pending_request_get_data_ (pr);
742   while (NULL != (rpr = prd->rpr_head))
743   {
744     GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, rpr);
745     rp = rpr->rp;
746     GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, rpr->prl);
747     if (NULL == rp->prl_head)
748     {
749       GNUNET_CONTAINER_heap_remove_node (rp->hn);
750       plan_count--;
751       GNUNET_break (GNUNET_YES ==
752                     GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
753                                                           &GSF_pending_request_get_data_
754                                                           (rpr->prl->pr)->query,
755                                                           rp));
756       GNUNET_free (rp);
757     }
758     GNUNET_free (rpr->prl);
759     GNUNET_free (rpr);
760   }
761   GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
762                          plan_count, GNUNET_NO);
763 }
764
765
766 /**
767  * Initialize plan subsystem.
768  */
769 void
770 GSF_plan_init ()
771 {
772   plans = GNUNET_CONTAINER_multihashmap_create (256);
773 }
774
775
776 /**
777  * Shutdown plan subsystem.
778  */
779 void
780 GSF_plan_done ()
781 {
782   GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (plans));
783   GNUNET_CONTAINER_multihashmap_destroy (plans);
784 }
785
786
787
788 /* end of gnunet-service-fs_pe.h */