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;
70 * Transmission plan for a peer.
75 * Heap with pending queries (struct GSF_RequestPlan), smaller weights mean higher priority.
77 struct GNUNET_CONTAINER_Heap *heap;
80 * Current transmission request handle.
82 struct GSF_PeerTransmitHandle *pth;
85 * Peer for which this is the plan.
87 struct GSF_ConnectedPeer *cp;
90 * Current task for executing the plan.
92 GNUNET_SCHEDULER_TaskIdentifier task;
97 * Hash map from peer identities to PeerPlans.
99 static struct GNUNET_CONTAINER_MultiHashMap *plans;
103 * Insert the given request plan into the heap with the appropriate weight.
105 * @param pp associated peer's plan
106 * @param rp request to plan
109 plan (struct PeerPlan *pp,
110 struct GSF_RequestPlan *rp)
112 GNUNET_CONTAINER_HeapCostType weight;
113 struct GSF_PendingRequestData *prd;
115 prd = GSF_pending_request_get_data_ (rp->pr);
116 weight = 0; // FIXME: calculate real weight!
117 // FIXME: calculate 'rp->earliest_transmission'!
118 rp->hn = GNUNET_CONTAINER_heap_insert (pp->heap,
125 * Figure out when and how to transmit to the given peer.
127 * @param cls the 'struct GSF_ConnectedPeer' for transmission
128 * @param tc scheduler context
131 schedule_peer_transmission (void *cls,
132 const struct GNUNET_SCHEDULER_TaskContext *tc);
136 * Function called to get a message for transmission.
139 * @param buf_size number of bytes available in buf
140 * @param buf where to copy the message, NULL on error (peer disconnect)
141 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
144 transmit_message_callback (void *cls,
148 struct PeerPlan *pp = cls;
149 struct GSF_RequestPlan *rp;
154 /* failed, try again... */
155 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
158 rp = GNUNET_CONTAINER_heap_peek (pp->heap);
159 msize = GSF_pending_request_get_message_ (rp->pr, buf_size, buf);
160 if (msize > buf_size)
162 /* buffer to small (message changed), try again */
163 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
166 /* remove from root, add again elsewhere... */
167 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->heap));
175 * Figure out when and how to transmit to the given peer.
177 * @param cls the 'struct PeerPlan'
178 * @param tc scheduler context
181 schedule_peer_transmission (void *cls,
182 const struct GNUNET_SCHEDULER_TaskContext *tc)
184 struct PeerPlan *pp = cls;
185 struct GSF_RequestPlan *rp;
186 struct GSF_PendingRequestData *prd;
188 struct GNUNET_TIME_Relative delay;
190 pp->task = GNUNET_SCHEDULER_NO_TASK;
191 if (NULL == pp->heap)
193 if (0 == GNUNET_CONTAINER_heap_get_size (pp->heap))
195 GNUNET_assert (NULL == pp->pth);
196 rp = GNUNET_CONTAINER_heap_peek (pp->heap);
197 prd = GSF_pending_request_get_data_ (rp->pr);
198 delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
199 if (delay.rel_value > 0)
201 pp->task = GNUNET_SCHEDULER_add_delayed (delay,
202 &schedule_peer_transmission,
206 msize = GSF_pending_request_get_message_ (rp->pr, 0, NULL);
207 pp->pth = GSF_peer_transmit_ (pp->cp,
209 0 /* FIXME: pr->priority? */,
210 GNUNET_TIME_UNIT_FOREVER_REL,
212 &transmit_message_callback,
214 GNUNET_assert (NULL != pp->pth);
219 * Create a new query plan entry.
221 * @param cp peer with the entry
222 * @param pr request with the entry
225 GSF_plan_add_ (const struct GSF_ConnectedPeer *cp,
226 struct GSF_PendingRequest *pr)
228 struct GNUNET_PeerIdentity id;
230 struct GSF_PendingRequestData *prd;
231 struct GSF_RequestPlan *rp;
233 GSF_connected_peer_get_identity_ (cp, &id);
234 pp = GNUNET_CONTAINER_multihashmap_get (plans,
238 pp = GNUNET_malloc (sizeof (struct PeerPlan));
239 pp->heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
240 GNUNET_CONTAINER_multihashmap_put (plans,
243 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
245 prd = GSF_pending_request_get_data_ (pr);
246 rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan));
248 GNUNET_CONTAINER_DLL_insert (prd->rp_head,
254 if (rp != GNUNET_CONTAINER_heap_peek (pp->heap))
256 GSF_peer_transmit_cancel_ (pp->pth);
259 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
260 GNUNET_SCHEDULER_cancel (pp->task);
261 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission,
267 * Notify the plan about a peer being no longer available;
268 * destroy all entries associated with this peer.
270 * @param cp connected peer
273 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
275 struct GNUNET_PeerIdentity id;
277 struct GSF_RequestPlan *rp;
278 struct GSF_PendingRequestData *prd;
280 GSF_connected_peer_get_identity_ (cp, &id);
281 pp = GNUNET_CONTAINER_multihashmap_get (plans,
283 GNUNET_CONTAINER_multihashmap_remove (plans,
287 GSF_peer_transmit_cancel_ (pp->pth);
288 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
289 GNUNET_SCHEDULER_cancel (pp->task);
290 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->heap)))
292 prd = GSF_pending_request_get_data_ (rp->pr);
293 GNUNET_CONTAINER_DLL_remove (prd->rp_head,
298 GNUNET_CONTAINER_heap_destroy (pp->heap);
304 * Notify the plan about a request being done; destroy all entries
305 * associated with this request.
307 * @param pr request that is done
310 GSF_plan_notify_request_done_ (const struct GSF_PendingRequest *pr)
312 struct GSF_RequestPlan *rp;
313 struct GSF_PendingRequestData *prd;
315 while (NULL != (rp = prd->rp_head))
317 prd = GSF_pending_request_get_data_ (rp->pr);
318 GNUNET_CONTAINER_heap_remove_node (rp->hn);
319 GNUNET_CONTAINER_DLL_remove (prd->rp_head,
328 * Initialize plan subsystem.
333 plans = GNUNET_CONTAINER_multihashmap_create (256);
338 * Shutdown plan subsystem.
344 GNUNET_CONTAINER_multihashmap_size (plans));
345 GNUNET_CONTAINER_multihashmap_destroy (plans);
350 /* end of gnunet-service-fs_pe.h */