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_cp.h"
28 #include "gnunet-service-fs_pe.h"
29 #include "gnunet-service-fs_pr.h"
33 * Information we keep per request per peer. This is a doubly-linked
34 * list (with head and tail in the 'struct GSF_PendingRequestData')
35 * with one entry in each heap of each 'struct PeerPlan'. Each
36 * entry tracks information relevant for this request and this peer.
38 struct GSF_RequestPlan
42 * This is a doubly-linked list.
44 struct GSF_RequestPlan *next;
47 * This is a doubly-linked list.
49 struct GSF_RequestPlan *prev;
52 * Heap node associated with this request and this peer.
54 struct GNUNET_CONTAINER_HeapNode *hn;
57 * Associated pending request.
59 struct GSF_PendingRequest *pr;
62 * Earliest time we'd be happy to (re)transmit this request.
64 struct GNUNET_TIME_Absolute earliest_transmission;
67 * When was the last time we transmitted this request to this peer? 0 for never.
69 struct GNUNET_TIME_Absolute last_transmission;
72 * Current priority for this request for this target.
77 * How often did we transmit this request to this peer?
79 unsigned int transmission_counter;
85 * Transmission plan for a peer.
90 * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
92 struct GNUNET_CONTAINER_Heap *priority_heap;
95 * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
97 struct GNUNET_CONTAINER_Heap *delay_heap;
100 * Current transmission request handle.
102 struct GSF_PeerTransmitHandle *pth;
105 * Peer for which this is the plan.
107 struct GSF_ConnectedPeer *cp;
110 * Current task for executing the plan.
112 GNUNET_SCHEDULER_TaskIdentifier task;
117 * Hash map from peer identities to PeerPlans.
119 static struct GNUNET_CONTAINER_MultiHashMap *plans;
123 * Insert the given request plan into the heap with the appropriate weight.
125 * @param pp associated peer's plan
126 * @param rp request to plan
129 plan (struct PeerPlan *pp,
130 struct GSF_RequestPlan *rp)
132 struct GSF_PendingRequestData *prd;
134 prd = GSF_pending_request_get_data_ (rp->pr);
135 // FIXME: calculate 'rp->earliest_transmission'!
136 // fIXME: claculate 'rp->priority'!
138 if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value == 0)
139 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
143 rp->hn = GNUNET_CONTAINER_heap_insert (pp->delay_heap,
145 rp->earliest_transmission.abs_value);
150 * Figure out when and how to transmit to the given peer.
152 * @param cls the 'struct GSF_ConnectedPeer' for transmission
153 * @param tc scheduler context
156 schedule_peer_transmission (void *cls,
157 const struct GNUNET_SCHEDULER_TaskContext *tc);
161 * Function called to get a message for transmission.
164 * @param buf_size number of bytes available in buf
165 * @param buf where to copy the message, NULL on error (peer disconnect)
166 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
169 transmit_message_callback (void *cls,
173 struct PeerPlan *pp = cls;
174 struct GSF_RequestPlan *rp;
179 /* failed, try again... */
180 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
183 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
184 msize = GSF_pending_request_get_message_ (rp->pr, buf_size, buf);
185 if (msize > buf_size)
187 /* buffer to small (message changed), try again */
188 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
191 /* remove from root, add again elsewhere... */
192 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
194 rp->last_transmission = GNUNET_TIME_absolute_get ();
195 rp->transmission_counter++;
202 * Figure out when and how to transmit to the given peer.
204 * @param cls the 'struct PeerPlan'
205 * @param tc scheduler context
208 schedule_peer_transmission (void *cls,
209 const struct GNUNET_SCHEDULER_TaskContext *tc)
211 struct PeerPlan *pp = cls;
212 struct GSF_RequestPlan *rp;
213 struct GSF_PendingRequestData *prd;
216 pp->task = GNUNET_SCHEDULER_NO_TASK;
217 GNUNET_assert (NULL == pp->pth);
218 /* move ready requests to priority queue */
219 while ( (NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
220 (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value == 0) )
222 rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap);
223 GNUNET_CONTAINER_heap_insert (pp->priority_heap,
226 if (NULL == (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap)))
229 if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
231 /* priority heap (still) empty, check for delay... */
232 rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
234 return; /* both queues empty */
235 pp->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission),
236 &schedule_peer_transmission,
240 /* process from priority heap */
241 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
242 prd = GSF_pending_request_get_data_ (rp->pr);
243 msize = GSF_pending_request_get_message_ (rp->pr, 0, NULL);
244 pp->pth = GSF_peer_transmit_ (pp->cp,
247 GNUNET_TIME_UNIT_FOREVER_REL,
249 &transmit_message_callback,
251 GNUNET_assert (NULL != pp->pth);
256 * Create a new query plan entry.
258 * @param cp peer with the entry
259 * @param pr request with the entry
262 GSF_plan_add_ (const struct GSF_ConnectedPeer *cp,
263 struct GSF_PendingRequest *pr)
265 struct GNUNET_PeerIdentity id;
267 struct GSF_PendingRequestData *prd;
268 struct GSF_RequestPlan *rp;
270 GSF_connected_peer_get_identity_ (cp, &id);
271 pp = GNUNET_CONTAINER_multihashmap_get (plans,
275 pp = GNUNET_malloc (sizeof (struct PeerPlan));
276 pp->priority_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
277 pp->delay_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
278 GNUNET_CONTAINER_multihashmap_put (plans,
281 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
283 prd = GSF_pending_request_get_data_ (pr);
284 rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan));
286 GNUNET_CONTAINER_DLL_insert (prd->rp_head,
290 if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
292 /* no request that should be done immediately, figure out delay */
293 if (rp != GNUNET_CONTAINER_heap_peek (pp->delay_heap))
294 return; /* did not change delay heap top, no need to do anything */
295 GNUNET_assert (NULL == pp->pth);
296 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
297 GNUNET_SCHEDULER_cancel (pp->task);
298 pp->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission),
299 &schedule_peer_transmission,
306 if (rp != GNUNET_CONTAINER_heap_peek (pp->priority_heap))
307 return; /* did not change priority heap top, no need to do anyhing */
308 GSF_peer_transmit_cancel_ (pp->pth);
311 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
312 GNUNET_SCHEDULER_cancel (pp->task);
313 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission,
319 * Notify the plan about a peer being no longer available;
320 * destroy all entries associated with this peer.
322 * @param cp connected peer
325 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
327 struct GNUNET_PeerIdentity id;
329 struct GSF_RequestPlan *rp;
330 struct GSF_PendingRequestData *prd;
332 GSF_connected_peer_get_identity_ (cp, &id);
333 pp = GNUNET_CONTAINER_multihashmap_get (plans,
335 GNUNET_CONTAINER_multihashmap_remove (plans,
339 GSF_peer_transmit_cancel_ (pp->pth);
340 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
341 GNUNET_SCHEDULER_cancel (pp->task);
342 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
344 prd = GSF_pending_request_get_data_ (rp->pr);
345 GNUNET_CONTAINER_DLL_remove (prd->rp_head,
350 GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
351 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
353 prd = GSF_pending_request_get_data_ (rp->pr);
354 GNUNET_CONTAINER_DLL_remove (prd->rp_head,
359 GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
365 * Notify the plan about a request being done; destroy all entries
366 * associated with this request.
368 * @param pr request that is done
371 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
373 struct GSF_RequestPlan *rp;
374 struct GSF_PendingRequestData *prd;
376 prd = GSF_pending_request_get_data_ (pr);
377 while (NULL != (rp = prd->rp_head))
379 GNUNET_CONTAINER_heap_remove_node (rp->hn);
380 GNUNET_CONTAINER_DLL_remove (prd->rp_head,
389 * Initialize plan subsystem.
394 plans = GNUNET_CONTAINER_multihashmap_create (256);
399 * Shutdown plan subsystem.
405 GNUNET_CONTAINER_multihashmap_size (plans));
406 GNUNET_CONTAINER_multihashmap_destroy (plans);
411 /* end of gnunet-service-fs_pe.h */