2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file fs/gnunet-service-fs_pe.c
23 * @brief API to manage query plan
24 * @author Christian Grothoff
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"
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.
39 struct GSF_RequestPlan
43 * This is a doubly-linked list.
45 struct GSF_RequestPlan *next;
48 * This is a doubly-linked list.
50 struct GSF_RequestPlan *prev;
53 * Heap node associated with this request and this peer.
55 struct GNUNET_CONTAINER_HeapNode *hn;
58 * Associated pending request.
60 struct GSF_PendingRequest *pr;
63 * Earliest time we'd be happy to (re)transmit this request.
65 struct GNUNET_TIME_Absolute earliest_transmission;
68 * When was the last time we transmitted this request to this peer? 0 for never.
70 struct GNUNET_TIME_Absolute last_transmission;
73 * Current priority for this request for this target.
78 * How often did we transmit this request to this peer?
80 unsigned int transmission_counter;
86 * Transmission plan for a peer.
91 * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
93 struct GNUNET_CONTAINER_Heap *priority_heap;
96 * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
98 struct GNUNET_CONTAINER_Heap *delay_heap;
101 * Current transmission request handle.
103 struct GSF_PeerTransmitHandle *pth;
106 * Peer for which this is the plan.
108 struct GSF_ConnectedPeer *cp;
111 * Current task for executing the plan.
113 GNUNET_SCHEDULER_TaskIdentifier task;
118 * Hash map from peer identities to PeerPlans.
120 static struct GNUNET_CONTAINER_MultiHashMap *plans;
124 * Insert the given request plan into the heap with the appropriate weight.
126 * @param pp associated peer's plan
127 * @param rp request to plan
130 plan (struct PeerPlan *pp,
131 struct GSF_RequestPlan *rp)
133 struct GSF_PendingRequestData *prd;
135 prd = GSF_pending_request_get_data_ (rp->pr);
136 // FIXME: calculate 'rp->earliest_transmission'!
137 // fIXME: claculate 'rp->priority'!
139 if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value == 0)
140 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
144 rp->hn = GNUNET_CONTAINER_heap_insert (pp->delay_heap,
146 rp->earliest_transmission.abs_value);
151 * Figure out when and how to transmit to the given peer.
153 * @param cls the 'struct GSF_ConnectedPeer' for transmission
154 * @param tc scheduler context
157 schedule_peer_transmission (void *cls,
158 const struct GNUNET_SCHEDULER_TaskContext *tc);
162 * Function called to get a message for transmission.
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)
170 transmit_message_callback (void *cls,
174 struct PeerPlan *pp = cls;
175 struct GSF_RequestPlan *rp;
181 /* failed, try again... */
182 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
185 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
188 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
191 msize = GSF_pending_request_get_message_ (rp->pr, buf_size, buf);
192 if (msize > buf_size)
194 /* buffer to small (message changed), try again */
195 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
198 /* remove from root, add again elsewhere... */
199 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
201 rp->last_transmission = GNUNET_TIME_absolute_get ();
202 rp->transmission_counter++;
209 * Figure out when and how to transmit to the given peer.
211 * @param cls the 'struct PeerPlan'
212 * @param tc scheduler context
215 schedule_peer_transmission (void *cls,
216 const struct GNUNET_SCHEDULER_TaskContext *tc)
218 struct PeerPlan *pp = cls;
219 struct GSF_RequestPlan *rp;
222 pp->task = GNUNET_SCHEDULER_NO_TASK;
223 GNUNET_assert (NULL == pp->pth);
224 /* move ready requests to priority queue */
225 while ( (NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
226 (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value == 0) )
228 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
229 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
233 if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
235 /* priority heap (still) empty, check for delay... */
236 rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
238 return; /* both queues empty */
239 pp->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission),
240 &schedule_peer_transmission,
244 /* process from priority heap */
245 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
246 GNUNET_assert (NULL != rp);
247 msize = GSF_pending_request_get_message_ (rp->pr, 0, NULL);
248 pp->pth = GSF_peer_transmit_ (pp->cp,
251 GNUNET_TIME_UNIT_FOREVER_REL,
253 &transmit_message_callback,
255 GNUNET_assert (NULL != pp->pth);
260 * Create a new query plan entry.
262 * @param cp peer with the entry
263 * @param pr request with the entry
266 GSF_plan_add_ (struct GSF_ConnectedPeer *cp,
267 struct GSF_PendingRequest *pr)
269 struct GNUNET_PeerIdentity id;
271 struct GSF_PendingRequestData *prd;
272 struct GSF_RequestPlan *rp;
274 GSF_connected_peer_get_identity_ (cp, &id);
275 pp = GNUNET_CONTAINER_multihashmap_get (plans,
279 pp = GNUNET_malloc (sizeof (struct PeerPlan));
280 pp->priority_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
281 pp->delay_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
283 GNUNET_CONTAINER_multihashmap_put (plans,
286 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
288 prd = GSF_pending_request_get_data_ (pr);
289 rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan));
291 GNUNET_CONTAINER_DLL_insert (prd->rp_head,
295 if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
297 /* no request that should be done immediately, figure out delay */
298 if (rp != GNUNET_CONTAINER_heap_peek (pp->delay_heap))
299 return; /* did not change delay heap top, no need to do anything */
300 GNUNET_assert (NULL == pp->pth);
301 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
302 GNUNET_SCHEDULER_cancel (pp->task);
303 pp->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission),
304 &schedule_peer_transmission,
311 if (rp != GNUNET_CONTAINER_heap_peek (pp->priority_heap))
312 return; /* did not change priority heap top, no need to do anyhing */
313 GSF_peer_transmit_cancel_ (pp->pth);
316 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
317 GNUNET_SCHEDULER_cancel (pp->task);
318 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission,
324 * Notify the plan about a peer being no longer available;
325 * destroy all entries associated with this peer.
327 * @param cp connected peer
330 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
332 struct GNUNET_PeerIdentity id;
334 struct GSF_RequestPlan *rp;
335 struct GSF_PendingRequestData *prd;
337 GSF_connected_peer_get_identity_ (cp, &id);
338 pp = GNUNET_CONTAINER_multihashmap_get (plans,
341 return; /* nothing was ever planned for this peer */
342 GNUNET_CONTAINER_multihashmap_remove (plans,
346 GSF_peer_transmit_cancel_ (pp->pth);
347 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
348 GNUNET_SCHEDULER_cancel (pp->task);
349 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
351 prd = GSF_pending_request_get_data_ (rp->pr);
352 GNUNET_CONTAINER_DLL_remove (prd->rp_head,
357 GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
358 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
360 prd = GSF_pending_request_get_data_ (rp->pr);
361 GNUNET_CONTAINER_DLL_remove (prd->rp_head,
366 GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
372 * Notify the plan about a request being done; destroy all entries
373 * associated with this request.
375 * @param pr request that is done
378 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
380 struct GSF_RequestPlan *rp;
381 struct GSF_PendingRequestData *prd;
383 prd = GSF_pending_request_get_data_ (pr);
384 while (NULL != (rp = prd->rp_head))
386 GNUNET_CONTAINER_heap_remove_node (rp->hn);
387 GNUNET_CONTAINER_DLL_remove (prd->rp_head,
396 * Initialize plan subsystem.
401 plans = GNUNET_CONTAINER_multihashmap_create (256);
406 * Shutdown plan subsystem.
412 GNUNET_CONTAINER_multihashmap_size (plans));
413 GNUNET_CONTAINER_multihashmap_destroy (plans);
418 /* end of gnunet-service-fs_pe.h */