curly wars / auto-indentation
[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   struct GSF_PendingRequestData *prd;
255   struct GNUNET_TIME_Relative delay;
256
257   GNUNET_assert (rp->pp == pp);
258   GNUNET_STATISTICS_set (GSF_stats,
259                          gettext_noop ("# average retransmission delay (ms)"),
260                          total_delay * 1000LL / plan_count, GNUNET_NO);
261   prd = GSF_pending_request_get_data_ (rp->prl_head->pr);
262   // FIXME: calculate 'rp->priority'!
263   if (rp->transmission_counter < 8)
264     delay =
265         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
266                                        rp->transmission_counter);
267   else if (rp->transmission_counter < 32)
268     delay =
269         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
270                                        8 +
271                                        (1LL << (rp->transmission_counter - 8)));
272   else
273     delay =
274         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
275                                        8 + (1LL << 24));
276   delay.rel_value =
277       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
278                                 delay.rel_value + 1);
279   if (rp->transmission_counter != 0)
280     delay.rel_value += TTL_DECREMENT;
281   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282               "Considering (re)transmission number %u in %llu ms\n",
283               (unsigned int) rp->transmission_counter,
284               (unsigned long long) delay.rel_value);
285   rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
286 #if DEBUG_FS
287   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288               "Earliest (re)transmission for `%s' in %us\n",
289               GNUNET_h2s (&prd->query), rp->transmission_counter);
290 #endif
291
292   GNUNET_assert (rp->hn == NULL);
293   if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value
294       == 0)
295     rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
296   else
297     rp->hn =
298         GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
299                                       rp->earliest_transmission.abs_value);
300   GNUNET_assert (GNUNET_YES ==
301                  GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
302                                                                get_rp_key (rp),
303                                                                rp));
304   if (GNUNET_SCHEDULER_NO_TASK != pp->task)
305     GNUNET_SCHEDULER_cancel (pp->task);
306   pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
307 }
308
309
310 /**
311  * Get the pending request with the highest TTL from the given plan.
312  *
313  * @param rp plan to investigate
314  * @return pending request with highest TTL
315  */
316 struct GSF_PendingRequest *
317 get_latest (const struct GSF_RequestPlan *rp)
318 {
319   struct GSF_PendingRequest *ret;
320   struct PendingRequestList *prl;
321
322   prl = rp->prl_head;
323   ret = prl->pr;
324   prl = prl->next;
325   while (NULL != prl)
326   {
327     if (GSF_pending_request_get_data_ (prl->pr)->ttl.abs_value >
328         GSF_pending_request_get_data_ (ret)->ttl.abs_value)
329       ret = prl->pr;
330     prl = prl->next;
331   }
332   return ret;
333 }
334
335
336 /**
337  * Function called to get a message for transmission.
338  *
339  * @param cls closure
340  * @param buf_size number of bytes available in buf
341  * @param buf where to copy the message, NULL on error (peer disconnect)
342  * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
343  */
344 static size_t
345 transmit_message_callback (void *cls, size_t buf_size, void *buf)
346 {
347   struct PeerPlan *pp = cls;
348   struct GSF_RequestPlan *rp;
349   size_t msize;
350
351   pp->pth = NULL;
352   if (NULL == buf)
353   {
354     /* failed, try again... */
355     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
356     GNUNET_STATISTICS_update (GSF_stats,
357                               gettext_noop
358                               ("# transmission failed (core has no bandwidth)"),
359                               1, GNUNET_NO);
360     return 0;
361   }
362   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
363   if (NULL == rp)
364   {
365     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
366     return 0;
367   }
368   msize = GSF_pending_request_get_message_ (get_latest (rp), buf_size, buf);
369   if (msize > buf_size)
370   {
371     /* buffer to small (message changed), try again */
372     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
373     return 0;
374   }
375   /* remove from root, add again elsewhere... */
376   GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
377   rp->hn = NULL;
378   rp->last_transmission = GNUNET_TIME_absolute_get ();
379   rp->transmission_counter++;
380   total_delay++;
381 #if DEBUG_FS
382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383               "Executing plan %p executed %u times, planning retransmission\n",
384               rp, rp->transmission_counter);
385 #endif
386   plan (pp, rp);
387   GNUNET_STATISTICS_update (GSF_stats,
388                             gettext_noop
389                             ("# query messages sent to other peers"), 1,
390                             GNUNET_NO);
391   return msize;
392 }
393
394
395 /**
396  * Figure out when and how to transmit to the given peer.
397  *
398  * @param cls the 'struct PeerPlan'
399  * @param tc scheduler context
400  */
401 static void
402 schedule_peer_transmission (void *cls,
403                             const struct GNUNET_SCHEDULER_TaskContext *tc)
404 {
405   struct PeerPlan *pp = cls;
406   struct GSF_RequestPlan *rp;
407   size_t msize;
408   struct GNUNET_TIME_Relative delay;
409
410   pp->task = GNUNET_SCHEDULER_NO_TASK;
411   if (pp->pth != NULL)
412   {
413     GSF_peer_transmit_cancel_ (pp->pth);
414     pp->pth = NULL;
415   }
416   /* move ready requests to priority queue */
417   while ((NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
418          (GNUNET_TIME_absolute_get_remaining
419           (rp->earliest_transmission).rel_value == 0))
420   {
421     GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
422     rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
423   }
424   if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
425   {
426     /* priority heap (still) empty, check for delay... */
427     rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
428     if (NULL == rp)
429     {
430 #if DEBUG_FS
431       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No active requests for plan %p.\n",
432                   pp);
433 #endif
434       return;                   /* both queues empty */
435     }
436     delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
437 #if DEBUG_FS
438     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439                 "Sleeping for %llu ms before retrying requests on plan %p.\n",
440                 (unsigned long long) delay.rel_value, pp);
441 #endif
442     GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# delay heap timeout"),
443                            delay.rel_value, GNUNET_NO);
444
445     pp->task =
446         GNUNET_SCHEDULER_add_delayed (delay, &schedule_peer_transmission, pp);
447     return;
448   }
449   GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plans executed"),
450                             1, GNUNET_NO);
451   /* process from priority heap */
452   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
453 #if DEBUG_FS > 1
454   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing query plan %p\n", rp);
455 #endif
456   GNUNET_assert (NULL != rp);
457   msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
458   pp->pth =
459       GSF_peer_transmit_ (pp->cp, GNUNET_YES, rp->priority,
460                           GNUNET_TIME_UNIT_FOREVER_REL, msize,
461                           &transmit_message_callback, pp);
462   GNUNET_assert (NULL != pp->pth);
463 }
464
465
466 /**
467  * Closure for 'merge_pr'.
468  */
469 struct MergeContext
470 {
471
472   struct GSF_PendingRequest *pr;
473
474   int merged;
475
476 };
477
478
479 /**
480  * Iterator that checks if an equivalent request is already
481  * present for this peer.
482  *
483  * @param cls closure
484  * @param query the query
485  * @param element request plan stored at the node
486  * @return GNUNET_YES if we should continue to iterate,
487  *         GNUNET_NO if not (merge success)
488  */
489 static int
490 merge_pr (void *cls, const GNUNET_HashCode * query, void *element)
491 {
492   struct MergeContext *mpr = cls;
493   struct GSF_RequestPlan *rp = element;
494   struct GSF_PendingRequestData *prd;
495   struct GSF_RequestPlanReference *rpr;
496   struct PendingRequestList *prl;
497   struct GSF_PendingRequest *latest;
498
499   if (GNUNET_OK !=
500       GSF_pending_request_is_compatible_ (mpr->pr, rp->prl_head->pr))
501     return GNUNET_YES;
502   /* merge new request with existing request plan */
503   rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
504   prl = GNUNET_malloc (sizeof (struct PendingRequestList));
505   rpr->rp = rp;
506   rpr->prl = prl;
507   prl->rpr = rpr;
508   prl->pr = mpr->pr;
509   prd = GSF_pending_request_get_data_ (mpr->pr);
510   GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
511   GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
512   mpr->merged = GNUNET_YES;
513   GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests merged"), 1,
514                             GNUNET_NO);
515   latest = get_latest (rp);
516   if (GSF_pending_request_get_data_ (latest)->ttl.abs_value <
517       prd->ttl.abs_value)
518   {
519     GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests refreshed"),
520                               1, GNUNET_NO);
521     rp->transmission_counter = 0;       /* reset */
522   }
523   return GNUNET_NO;
524 }
525
526
527 /**
528  * Create a new query plan entry.
529  *
530  * @param cp peer with the entry
531  * @param pr request with the entry
532  */
533 void
534 GSF_plan_add_ (struct GSF_ConnectedPeer *cp, struct GSF_PendingRequest *pr)
535 {
536   struct GNUNET_PeerIdentity id;
537   struct PeerPlan *pp;
538   struct GSF_PendingRequestData *prd;
539   struct GSF_RequestPlan *rp;
540   struct GSF_RequestPlanReference *rpr;
541   struct PendingRequestList *prl;
542   struct MergeContext mpc;
543
544   GNUNET_assert (NULL != cp);
545   GSF_connected_peer_get_identity_ (cp, &id);
546   pp = GNUNET_CONTAINER_multihashmap_get (plans, &id.hashPubKey);
547   if (NULL == pp)
548   {
549     pp = GNUNET_malloc (sizeof (struct PeerPlan));
550     pp->plan_map = GNUNET_CONTAINER_multihashmap_create (128);
551     pp->priority_heap =
552         GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
553     pp->delay_heap =
554         GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
555     pp->cp = cp;
556     GNUNET_CONTAINER_multihashmap_put (plans, &id.hashPubKey, pp,
557                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
558   }
559   mpc.merged = GNUNET_NO;
560   mpc.pr = pr;
561   GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
562                                               &GSF_pending_request_get_data_
563                                               (pr)->query, &merge_pr, &mpc);
564   if (mpc.merged != GNUNET_NO)
565     return;
566   GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
567                                               &GSF_pending_request_get_data_
568                                               (pr)->query, &merge_pr, &mpc);
569   if (mpc.merged != GNUNET_NO)
570     return;
571   plan_count++;
572   GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plan entries"), 1,
573                             GNUNET_NO);
574   prd = GSF_pending_request_get_data_ (pr);
575 #if DEBUG_FS
576   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577               "Planning transmission of query `%s' to peer `%s'\n",
578               GNUNET_h2s (&prd->query), GNUNET_i2s (&id));
579 #endif
580   rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan));
581   rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
582   prl = GNUNET_malloc (sizeof (struct PendingRequestList));
583   rpr->rp = rp;
584   rpr->prl = prl;
585   prl->rpr = rpr;
586   prl->pr = pr;
587   GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
588   GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
589   rp->pp = pp;
590   GNUNET_assert (GNUNET_YES ==
591                  GNUNET_CONTAINER_multihashmap_put (pp->plan_map,
592                                                     get_rp_key (rp), rp,
593                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
594   plan (pp, rp);
595 }
596
597
598 /**
599  * Notify the plan about a peer being no longer available;
600  * destroy all entries associated with this peer.
601  *
602  * @param cp connected peer
603  */
604 void
605 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
606 {
607   struct GNUNET_PeerIdentity id;
608   struct PeerPlan *pp;
609   struct GSF_RequestPlan *rp;
610   struct GSF_PendingRequestData *prd;
611   struct PendingRequestList *prl;
612
613   GSF_connected_peer_get_identity_ (cp, &id);
614   pp = GNUNET_CONTAINER_multihashmap_get (plans, &id.hashPubKey);
615   if (NULL == pp)
616     return;                     /* nothing was ever planned for this peer */
617   GNUNET_assert (GNUNET_YES ==
618                  GNUNET_CONTAINER_multihashmap_remove (plans, &id.hashPubKey,
619                                                        pp));
620   if (NULL != pp->pth)
621     GSF_peer_transmit_cancel_ (pp->pth);
622   if (GNUNET_SCHEDULER_NO_TASK != pp->task)
623   {
624     GNUNET_SCHEDULER_cancel (pp->task);
625     pp->task = GNUNET_SCHEDULER_NO_TASK;
626   }
627   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
628   {
629     GNUNET_break (GNUNET_YES ==
630                   GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
631                                                         get_rp_key (rp), rp));
632     while (NULL != (prl = rp->prl_head))
633     {
634       GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
635       prd = GSF_pending_request_get_data_ (prl->pr);
636       GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, prl->rpr);
637       GNUNET_free (prl->rpr);
638       GNUNET_free (prl);
639     }
640     GNUNET_free (rp);
641   }
642   GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
643   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
644   {
645     GNUNET_break (GNUNET_YES ==
646                   GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
647                                                         get_rp_key (rp), rp));
648     while (NULL != (prl = rp->prl_head))
649     {
650       GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
651       prd = GSF_pending_request_get_data_ (prl->pr);
652       GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, prl->rpr);
653       GNUNET_free (prl->rpr);
654       GNUNET_free (prl);
655     }
656     GNUNET_free (rp);
657   }
658   GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
659                          plan_count, GNUNET_NO);
660
661   GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
662   GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
663   GNUNET_free (pp);
664 }
665
666
667 /**
668  * Notify the plan about a request being done; destroy all entries
669  * associated with this request.
670  *
671  * @param pr request that is done
672  */
673 void
674 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
675 {
676   struct GSF_RequestPlan *rp;
677   struct GSF_PendingRequestData *prd;
678   struct GSF_RequestPlanReference *rpr;
679
680   prd = GSF_pending_request_get_data_ (pr);
681   while (NULL != (rpr = prd->rpr_head))
682   {
683     GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, rpr);
684     rp = rpr->rp;
685     GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, rpr->prl);
686     if (NULL == rp->prl_head)
687     {
688       GNUNET_CONTAINER_heap_remove_node (rp->hn);
689       plan_count--;
690       GNUNET_break (GNUNET_YES ==
691                     GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
692                                                           &GSF_pending_request_get_data_
693                                                           (rpr->prl->pr)->query,
694                                                           rp));
695       GNUNET_free (rp);
696     }
697     GNUNET_free (rpr->prl);
698     GNUNET_free (rpr);
699   }
700   GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
701                          plan_count, GNUNET_NO);
702 }
703
704
705 /**
706  * Initialize plan subsystem.
707  */
708 void
709 GSF_plan_init ()
710 {
711   plans = GNUNET_CONTAINER_multihashmap_create (256);
712 }
713
714
715 /**
716  * Shutdown plan subsystem.
717  */
718 void
719 GSF_plan_done ()
720 {
721   GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (plans));
722   GNUNET_CONTAINER_multihashmap_destroy (plans);
723 }
724
725
726
727 /* end of gnunet-service-fs_pe.h */