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