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"
32 * Transmission plan for a peer.
37 * Heap with pending queries, smaller weights mean higher priority.
39 struct GNUNET_CONTAINER_Heap *heap;
42 * Current transmission request handle.
44 struct GSF_PeerTransmitHandle *pth;
47 * Peer for which this is the plan.
49 struct GSF_ConnectedPeer *cp;
52 * Current task for executing the plan.
54 GNUNET_SCHEDULER_TaskIdentifier task;
59 * Hash map from peer identities to PeerPlans.
61 static struct GNUNET_CONTAINER_MultiHashMap *plans;
65 * Figure out when and how to transmit to the given peer.
67 * @param cls the 'struct GSF_ConnectedPeer' for transmission
68 * @param tc scheduler context
71 schedule_peer_transmission (void *cls,
72 const struct GNUNET_SCHEDULER_TaskContext *tc);
76 * Function called to get a message for transmission.
79 * @param buf_size number of bytes available in buf
80 * @param buf where to copy the message, NULL on error (peer disconnect)
81 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
84 transmit_message_callback (void *cls,
88 struct PeerPlan *pp = cls;
89 struct GSF_PendingRequest *pr;
94 /* failed, try again... */
95 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
98 pr = GNUNET_CONTAINER_heap_peek (pp->heap);
99 msize = GSF_pending_request_get_message_ (pr, buf_size, buf);
100 if (msize > buf_size)
102 /* buffer to small (message changed), try again */
103 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
106 /* remove from root, add again elsewhere... */
107 GNUNET_assert (pr == GNUNET_CONTAINER_heap_remove_root (pp->heap));
108 GSF_plan_add_ (pp->cp, pr);
114 * Figure out when and how to transmit to the given peer.
116 * @param cls the 'struct PeerPlan'
117 * @param tc scheduler context
120 schedule_peer_transmission (void *cls,
121 const struct GNUNET_SCHEDULER_TaskContext *tc)
123 struct PeerPlan *pp = cls;
124 struct GSF_PendingRequest *pr;
126 struct GNUNET_TIME_Relative delay;
128 pp->task = GNUNET_SCHEDULER_NO_TASK;
129 if (NULL == pp->heap)
131 if (0 == GNUNET_CONTAINER_heap_get_size (pp->heap))
133 GNUNET_assert (NULL == pp->pth);
134 pr = GNUNET_CONTAINER_heap_peek (pp->heap);
135 if (0) // FIXME: if (re)transmission should wait, wait...
137 delay = GNUNET_TIME_UNIT_SECONDS;
139 pp->task = GNUNET_SCHEDULER_add_delayed (delay,
140 &schedule_peer_transmission,
144 msize = GSF_pending_request_get_message_ (pr, 0, NULL);
145 pp->pth = GSF_peer_transmit_ (pp->cp,
147 0 /* FIXME: pr->priority? */,
148 GNUNET_TIME_UNIT_FOREVER_REL,
150 &transmit_message_callback,
152 GNUNET_assert (NULL != pp->pth);
157 * Create a new query plan entry.
159 * @param cp peer with the entry
160 * @param pr request with the entry
163 GSF_plan_add_ (const struct GSF_ConnectedPeer *cp,
164 struct GSF_PendingRequest *pr)
166 struct GNUNET_PeerIdentity id;
168 GNUNET_CONTAINER_HeapCostType weight;
170 GSF_connected_peer_get_identity_ (cp, &id);
171 pp = GNUNET_CONTAINER_multihashmap_get (plans,
175 pp = GNUNET_malloc (sizeof (struct PeerPlan));
176 pp->heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
177 GNUNET_CONTAINER_multihashmap_put (plans,
180 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
182 weight = 0; // FIXME: calculate real weight!
183 GNUNET_CONTAINER_heap_insert (pp->heap,
188 if (pr != GNUNET_CONTAINER_heap_peek (pp->heap))
190 GSF_peer_transmit_cancel_ (pp->pth);
193 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
194 GNUNET_SCHEDULER_cancel (pp->task);
195 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission,
201 * Notify the plan about a peer being no longer available;
202 * destroy all entries associated with this peer.
204 * @param cp connected peer
207 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
209 struct GNUNET_PeerIdentity id;
212 GSF_connected_peer_get_identity_ (cp, &id);
213 pp = GNUNET_CONTAINER_multihashmap_get (plans,
215 GNUNET_CONTAINER_multihashmap_remove (plans,
219 GSF_peer_transmit_cancel_ (pp->pth);
220 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
221 GNUNET_SCHEDULER_cancel (pp->task);
222 GNUNET_CONTAINER_heap_destroy (pp->heap);
228 * Closure for 'find_request'.
230 struct FindRequestClosure
233 * Place to store the node that was found (NULL for none).
235 struct GNUNET_CONTAINER_HeapNode *node;
238 * Value we're looking for
240 const struct GSF_PendingRequest *pr;
245 * Find a heap node where the value matches the
246 * pending request given in the closure.
248 * @param cls the 'struct FindRequestClosure'
249 * @param node heap structure we're looking for on a match
250 * @param element the pending request stored in the heap
251 * @param cost weight of the request
252 * @return GNUNET_YES to continue looking
255 find_request (void *cls,
256 struct GNUNET_CONTAINER_HeapNode *node,
258 GNUNET_CONTAINER_HeapCostType cost)
260 struct FindRequestClosure *frc = cls;
261 struct GSF_PendingRequest *pr = element;
273 * Remove the given request from all heaps. * FIXME: O(n) -- inefficient!
275 * @param cls 'struct GSF_PendingRequest' to purge
276 * @param key identity of the peer we're currently looking at (unused)
277 * @param value PeerPlan for the given peer to search for the 'cls'
278 * @return GNUNET_OK (continue iteration)
281 remove_request (void *cls,
282 const GNUNET_HashCode *key,
285 const struct GSF_PendingRequest *pr = cls;
286 struct PeerPlan *pp = value;
287 struct GNUNET_CONTAINER_Heap *h = pp->heap;
288 struct FindRequestClosure frc;
294 GNUNET_CONTAINER_heap_iterate (h, &find_request, &frc);
295 if (frc.node != NULL)
296 GNUNET_CONTAINER_heap_remove_node (h, frc.node);
298 while (NULL != frc.node);
304 * Notify the plan about a request being done; destroy all entries
305 * associated with this request. Note that this implementation is
306 * currently terribly inefficient (O(n)) and could instead be done in
307 * O(1). But for now, I first want to see it work correctly...
309 * @param pr request that is done
312 GSF_plan_notify_request_done_ (const struct GSF_PendingRequest *pr)
314 GNUNET_CONTAINER_multihashmap_iterate (plans,
321 * Initialize plan subsystem.
326 plans = GNUNET_CONTAINER_multihashmap_create (256);
331 * Shutdown plan subsystem.
337 GNUNET_CONTAINER_multihashmap_size (plans));
338 GNUNET_CONTAINER_multihashmap_destroy (plans);
343 /* end of gnunet-service-fs_pe.h */