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 transmit this request.
64 struct GNUNET_TIME_Absolute earliest_transmission;
67 * Priority for this request for this target.
75 * Transmission plan for a peer.
80 * Heap with pending queries (struct GSF_RequestPlan), smaller weights mean higher priority.
82 struct GNUNET_CONTAINER_Heap *heap;
85 * Current transmission request handle.
87 struct GSF_PeerTransmitHandle *pth;
90 * Peer for which this is the plan.
92 struct GSF_ConnectedPeer *cp;
95 * Current task for executing the plan.
97 GNUNET_SCHEDULER_TaskIdentifier task;
102 * Hash map from peer identities to PeerPlans.
104 static struct GNUNET_CONTAINER_MultiHashMap *plans;
108 * Insert the given request plan into the heap with the appropriate weight.
110 * @param pp associated peer's plan
111 * @param rp request to plan
114 plan (struct PeerPlan *pp,
115 struct GSF_RequestPlan *rp)
117 GNUNET_CONTAINER_HeapCostType weight;
118 struct GSF_PendingRequestData *prd;
120 prd = GSF_pending_request_get_data_ (rp->pr);
121 weight = 0; // FIXME: calculate real weight!
122 // FIXME: calculate 'rp->earliest_transmission'!
123 // fIXME: claculate 'rp->priority'!
124 rp->hn = GNUNET_CONTAINER_heap_insert (pp->heap,
131 * Figure out when and how to transmit to the given peer.
133 * @param cls the 'struct GSF_ConnectedPeer' for transmission
134 * @param tc scheduler context
137 schedule_peer_transmission (void *cls,
138 const struct GNUNET_SCHEDULER_TaskContext *tc);
142 * Function called to get a message for transmission.
145 * @param buf_size number of bytes available in buf
146 * @param buf where to copy the message, NULL on error (peer disconnect)
147 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
150 transmit_message_callback (void *cls,
154 struct PeerPlan *pp = cls;
155 struct GSF_RequestPlan *rp;
160 /* failed, try again... */
161 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
164 rp = GNUNET_CONTAINER_heap_peek (pp->heap);
165 msize = GSF_pending_request_get_message_ (rp->pr, buf_size, buf);
166 if (msize > buf_size)
168 /* buffer to small (message changed), try again */
169 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
172 /* remove from root, add again elsewhere... */
173 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->heap));
181 * Figure out when and how to transmit to the given peer.
183 * @param cls the 'struct PeerPlan'
184 * @param tc scheduler context
187 schedule_peer_transmission (void *cls,
188 const struct GNUNET_SCHEDULER_TaskContext *tc)
190 struct PeerPlan *pp = cls;
191 struct GSF_RequestPlan *rp;
192 struct GSF_PendingRequestData *prd;
194 struct GNUNET_TIME_Relative delay;
196 pp->task = GNUNET_SCHEDULER_NO_TASK;
197 if (NULL == pp->heap)
199 if (0 == GNUNET_CONTAINER_heap_get_size (pp->heap))
201 GNUNET_assert (NULL == pp->pth);
202 rp = GNUNET_CONTAINER_heap_peek (pp->heap);
203 prd = GSF_pending_request_get_data_ (rp->pr);
204 delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
205 if (delay.rel_value > 0)
207 pp->task = GNUNET_SCHEDULER_add_delayed (delay,
208 &schedule_peer_transmission,
212 msize = GSF_pending_request_get_message_ (rp->pr, 0, NULL);
213 pp->pth = GSF_peer_transmit_ (pp->cp,
216 GNUNET_TIME_UNIT_FOREVER_REL,
218 &transmit_message_callback,
220 GNUNET_assert (NULL != pp->pth);
225 * Create a new query plan entry.
227 * @param cp peer with the entry
228 * @param pr request with the entry
231 GSF_plan_add_ (const struct GSF_ConnectedPeer *cp,
232 struct GSF_PendingRequest *pr)
234 struct GNUNET_PeerIdentity id;
236 struct GSF_PendingRequestData *prd;
237 struct GSF_RequestPlan *rp;
239 GSF_connected_peer_get_identity_ (cp, &id);
240 pp = GNUNET_CONTAINER_multihashmap_get (plans,
244 pp = GNUNET_malloc (sizeof (struct PeerPlan));
245 pp->heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
246 GNUNET_CONTAINER_multihashmap_put (plans,
249 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
251 prd = GSF_pending_request_get_data_ (pr);
252 rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan));
254 GNUNET_CONTAINER_DLL_insert (prd->rp_head,
260 if (rp != GNUNET_CONTAINER_heap_peek (pp->heap))
262 GSF_peer_transmit_cancel_ (pp->pth);
265 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
266 GNUNET_SCHEDULER_cancel (pp->task);
267 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission,
273 * Notify the plan about a peer being no longer available;
274 * destroy all entries associated with this peer.
276 * @param cp connected peer
279 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
281 struct GNUNET_PeerIdentity id;
283 struct GSF_RequestPlan *rp;
284 struct GSF_PendingRequestData *prd;
286 GSF_connected_peer_get_identity_ (cp, &id);
287 pp = GNUNET_CONTAINER_multihashmap_get (plans,
289 GNUNET_CONTAINER_multihashmap_remove (plans,
293 GSF_peer_transmit_cancel_ (pp->pth);
294 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
295 GNUNET_SCHEDULER_cancel (pp->task);
296 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->heap)))
298 prd = GSF_pending_request_get_data_ (rp->pr);
299 GNUNET_CONTAINER_DLL_remove (prd->rp_head,
304 GNUNET_CONTAINER_heap_destroy (pp->heap);
310 * Notify the plan about a request being done; destroy all entries
311 * associated with this request.
313 * @param pr request that is done
316 GSF_plan_notify_request_done_ (const struct GSF_PendingRequest *pr)
318 struct GSF_RequestPlan *rp;
319 struct GSF_PendingRequestData *prd;
321 while (NULL != (rp = prd->rp_head))
323 prd = GSF_pending_request_get_data_ (rp->pr);
324 GNUNET_CONTAINER_heap_remove_node (rp->hn);
325 GNUNET_CONTAINER_DLL_remove (prd->rp_head,
334 * Initialize plan subsystem.
339 plans = GNUNET_CONTAINER_multihashmap_create (256);
344 * Shutdown plan subsystem.
350 GNUNET_CONTAINER_multihashmap_size (plans));
351 GNUNET_CONTAINER_multihashmap_destroy (plans);
356 /* end of gnunet-service-fs_pe.h */