fe6c995ed14bb78102d352fd600bfde106d3eba4
[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  * Information we keep per request per peer.  This is a doubly-linked
35  * list (with head and tail in the 'struct GSF_PendingRequestData')
36  * with one entry in each heap of each 'struct PeerPlan'.  Each
37  * entry tracks information relevant for this request and this peer.
38  */
39 struct GSF_RequestPlan
40 {
41
42   /**
43    * This is a doubly-linked list.
44    */
45   struct GSF_RequestPlan *next;
46
47   /**
48    * This is a doubly-linked list.
49    */
50   struct GSF_RequestPlan *prev;
51
52   /**
53    * Heap node associated with this request and this peer.
54    */
55   struct GNUNET_CONTAINER_HeapNode *hn;
56
57   /**
58    * Associated pending request.
59    */
60   struct GSF_PendingRequest *pr;
61
62   /**
63    * Earliest time we'd be happy to (re)transmit this request.
64    */
65   struct GNUNET_TIME_Absolute earliest_transmission;
66
67   /**
68    * When was the last time we transmitted this request to this peer? 0 for never.
69    */
70   struct GNUNET_TIME_Absolute last_transmission;
71
72   /**
73    * Current priority for this request for this target.
74    */
75   uint64_t priority;
76
77   /**
78    * How often did we transmit this request to this peer?
79    */
80   unsigned int transmission_counter;
81
82 };
83
84
85 /**
86  * Transmission plan for a peer.
87  */
88 struct PeerPlan
89 {
90   /**
91    * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
92    */
93   struct GNUNET_CONTAINER_Heap *priority_heap;
94
95   /**
96    * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
97    */
98   struct GNUNET_CONTAINER_Heap *delay_heap;
99
100   /**
101    * Current transmission request handle.
102    */
103   struct GSF_PeerTransmitHandle *pth;
104
105   /**
106    * Peer for which this is the plan.
107    */
108   struct GSF_ConnectedPeer *cp;
109
110   /**
111    * Current task for executing the plan.
112    */
113   GNUNET_SCHEDULER_TaskIdentifier task;
114 };
115
116
117 /**
118  * Hash map from peer identities to PeerPlans.
119  */
120 static struct GNUNET_CONTAINER_MultiHashMap *plans;
121
122
123 /**
124  * Insert the given request plan into the heap with the appropriate weight.
125  *
126  * @param pp associated peer's plan
127  * @param rp request to plan
128  */
129 static void
130 plan (struct PeerPlan *pp,
131       struct GSF_RequestPlan *rp)
132 {
133   struct GSF_PendingRequestData *prd;
134
135   prd = GSF_pending_request_get_data_ (rp->pr);
136   // FIXME: calculate 'rp->earliest_transmission'!
137   // fIXME: claculate 'rp->priority'! 
138
139   if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value == 0)
140     rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
141                                            rp,
142                                            rp->priority);
143   else
144     rp->hn = GNUNET_CONTAINER_heap_insert (pp->delay_heap,
145                                            rp,
146                                            rp->earliest_transmission.abs_value);
147 }
148
149
150 /**
151  * Figure out when and how to transmit to the given peer.
152  *
153  * @param cls the 'struct GSF_ConnectedPeer' for transmission
154  * @param tc scheduler context
155  */
156 static void
157 schedule_peer_transmission (void *cls,
158                             const struct GNUNET_SCHEDULER_TaskContext *tc);
159
160
161 /**
162  * Function called to get a message for transmission.
163  *
164  * @param cls closure
165  * @param buf_size number of bytes available in buf
166  * @param buf where to copy the message, NULL on error (peer disconnect)
167  * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
168  */
169 static size_t 
170 transmit_message_callback (void *cls,
171                            size_t buf_size,
172                            void *buf)
173 {
174   struct PeerPlan *pp = cls;
175   struct GSF_RequestPlan *rp;
176   size_t msize;
177
178   pp->pth = NULL;
179   if (NULL == buf)
180     {
181       /* failed, try again... */
182       pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
183       return 0;
184     }
185   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
186   if (NULL == rp)
187     {
188       pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
189       return 0;
190     }
191   msize = GSF_pending_request_get_message_ (rp->pr, buf_size, buf);
192   if (msize > buf_size)
193     {
194       /* buffer to small (message changed), try again */
195       pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
196       return 0;
197     }
198   /* remove from root, add again elsewhere... */
199   GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
200   rp->hn = NULL;
201   rp->last_transmission = GNUNET_TIME_absolute_get ();
202   rp->transmission_counter++;
203   plan (pp, rp);
204   return msize;
205 }
206
207
208 /**
209  * Figure out when and how to transmit to the given peer.
210  *
211  * @param cls the 'struct PeerPlan'
212  * @param tc scheduler context
213  */
214 static void
215 schedule_peer_transmission (void *cls,
216                             const struct GNUNET_SCHEDULER_TaskContext *tc)
217 {
218   struct PeerPlan *pp = cls;
219   struct GSF_RequestPlan *rp;
220   struct GSF_PendingRequestData *prd;
221   size_t msize;
222
223   pp->task = GNUNET_SCHEDULER_NO_TASK;
224   GNUNET_assert (NULL == pp->pth);
225   /* move ready requests to priority queue */
226   while ( (NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
227           (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value == 0) )
228     {
229       rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap);
230       GNUNET_CONTAINER_heap_insert (pp->priority_heap,
231                                     rp, 
232                                     rp->priority);                                      
233       if (NULL == (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap)))
234         break;
235     }   
236   if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
237     {
238       /* priority heap (still) empty, check for delay... */
239       rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
240       if (NULL == rp)
241         return; /* both queues empty */
242       pp->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission),
243                                                &schedule_peer_transmission,
244                                                pp);
245       return;
246     }
247   /* process from priority heap */
248   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
249   GNUNET_assert (NULL != rp);
250   prd = GSF_pending_request_get_data_ (rp->pr);
251   msize = GSF_pending_request_get_message_ (rp->pr, 0, NULL);                                      
252   pp->pth = GSF_peer_transmit_ (pp->cp,
253                                 GNUNET_YES,
254                                 rp->priority,
255                                 GNUNET_TIME_UNIT_FOREVER_REL,
256                                 msize,
257                                 &transmit_message_callback,
258                                 pp);
259   GNUNET_assert (NULL != pp->pth);
260 }
261
262
263 /**
264  * Create a new query plan entry.
265  *
266  * @param cp peer with the entry
267  * @param pr request with the entry
268  */
269 void
270 GSF_plan_add_ (const struct GSF_ConnectedPeer *cp,
271                struct GSF_PendingRequest *pr)
272 {
273   struct GNUNET_PeerIdentity id;
274   struct PeerPlan *pp;
275   struct GSF_PendingRequestData *prd;
276   struct GSF_RequestPlan *rp;
277   
278   GSF_connected_peer_get_identity_ (cp, &id);
279   pp = GNUNET_CONTAINER_multihashmap_get (plans,
280                                           &id.hashPubKey);
281   if (NULL == pp)
282     {
283       pp = GNUNET_malloc (sizeof (struct PeerPlan));
284       pp->priority_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
285       pp->delay_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
286       GNUNET_CONTAINER_multihashmap_put (plans,
287                                          &id.hashPubKey,
288                                          pp,
289                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
290     }
291   prd = GSF_pending_request_get_data_ (pr);
292   rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan));
293   rp->pr = pr;
294   GNUNET_CONTAINER_DLL_insert (prd->rp_head,
295                                prd->rp_tail,
296                                rp);
297   plan (pp, rp);
298   if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
299     {
300       /* no request that should be done immediately, figure out delay */
301       if (rp != GNUNET_CONTAINER_heap_peek (pp->delay_heap))
302         return; /* did not change delay heap top, no need to do anything */
303       GNUNET_assert (NULL == pp->pth);
304       if (GNUNET_SCHEDULER_NO_TASK != pp->task)
305         GNUNET_SCHEDULER_cancel (pp->task);
306       pp->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission),
307                                                &schedule_peer_transmission,
308                                                pp);
309       return;
310     }
311
312   if (pp->pth != NULL)
313     {
314       if (rp != GNUNET_CONTAINER_heap_peek (pp->priority_heap))
315         return; /* did not change priority heap top, no need to do anyhing */
316       GSF_peer_transmit_cancel_ (pp->pth);
317       pp->pth = NULL;
318     }
319   if (GNUNET_SCHEDULER_NO_TASK != pp->task)
320     GNUNET_SCHEDULER_cancel (pp->task);
321   pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission,
322                                        pp);
323 }
324
325
326 /**
327  * Notify the plan about a peer being no longer available;
328  * destroy all entries associated with this peer.
329  *
330  * @param cp connected peer 
331  */
332 void
333 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
334 {
335   struct GNUNET_PeerIdentity id;
336   struct PeerPlan *pp;
337   struct GSF_RequestPlan *rp;
338   struct GSF_PendingRequestData *prd;
339
340   GSF_connected_peer_get_identity_ (cp, &id);
341   pp = GNUNET_CONTAINER_multihashmap_get (plans,
342                                           &id.hashPubKey);
343   if (NULL == pp)
344     return; /* nothing was ever planned for this peer */
345   GNUNET_CONTAINER_multihashmap_remove (plans,
346                                         &id.hashPubKey,
347                                         pp);
348   if (NULL != pp->pth)
349     GSF_peer_transmit_cancel_ (pp->pth);
350   if (GNUNET_SCHEDULER_NO_TASK != pp->task)
351     GNUNET_SCHEDULER_cancel (pp->task);
352   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
353     {
354       prd = GSF_pending_request_get_data_ (rp->pr);
355       GNUNET_CONTAINER_DLL_remove (prd->rp_head,
356                                    prd->rp_tail,
357                                    rp);
358       GNUNET_free (rp);
359     }
360   GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
361   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
362     {
363       prd = GSF_pending_request_get_data_ (rp->pr);
364       GNUNET_CONTAINER_DLL_remove (prd->rp_head,
365                                    prd->rp_tail,
366                                    rp);
367       GNUNET_free (rp);
368     }
369   GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
370   GNUNET_free (pp);
371 }
372
373
374 /**
375  * Notify the plan about a request being done; destroy all entries
376  * associated with this request.
377  *
378  * @param pr request that is done
379  */
380 void
381 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
382 {
383   struct GSF_RequestPlan *rp;
384   struct GSF_PendingRequestData *prd;
385
386   prd = GSF_pending_request_get_data_ (pr);
387   while (NULL != (rp = prd->rp_head))
388     {
389       GNUNET_CONTAINER_heap_remove_node (rp->hn);
390       GNUNET_CONTAINER_DLL_remove (prd->rp_head,
391                                    prd->rp_tail,
392                                    rp);
393       GNUNET_free (rp);
394     }
395 }
396
397
398 /**
399  * Initialize plan subsystem.
400  */
401 void
402 GSF_plan_init ()
403 {
404   plans = GNUNET_CONTAINER_multihashmap_create (256);
405 }
406
407
408 /**
409  * Shutdown plan subsystem.
410  */
411 void
412 GSF_plan_done ()
413 {
414   GNUNET_assert (0 == 
415                  GNUNET_CONTAINER_multihashmap_size (plans));
416   GNUNET_CONTAINER_multihashmap_destroy (plans);
417 }
418
419
420
421 /* end of gnunet-service-fs_pe.h */