2 This file is part of GNUnet.
3 (C) 2009, 2010 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 2, 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_drq.c
23 * @brief queueing of requests to the datastore service
24 * @author Christian Grothoff
27 #include "gnunet-service-fs_drq.h"
29 #define DEBUG_DRQ GNUNET_NO
32 * Signature of a function that is called whenever a datastore
33 * request can be processed (or an entry put on the queue times out).
36 * @param ok GNUNET_OK if DS is ready, GNUNET_SYSERR on timeout
38 typedef void (*RequestFunction)(void *cls,
43 * Doubly-linked list of our requests for the datastore.
45 struct DatastoreRequestQueue
49 * This is a doubly-linked list.
51 struct DatastoreRequestQueue *next;
54 * This is a doubly-linked list.
56 struct DatastoreRequestQueue *prev;
59 * Function to call for each entry.
61 GNUNET_DATASTORE_Iterator iter;
69 * Key we are doing the 'get' for.
74 * Timeout for this operation.
76 struct GNUNET_TIME_Absolute timeout;
79 * ID of task used for signaling timeout.
81 GNUNET_SCHEDULER_TaskIdentifier task;
84 * Datastore entry type we are doing the 'get' for.
86 enum GNUNET_BLOCK_Type type;
89 * Is this request at the head of the queue irrespective of its
99 static struct GNUNET_SCHEDULER_Handle *sched;
104 static const struct GNUNET_CONFIGURATION_Handle *cfg;
107 * Head of request queue for the datastore, sorted by timeout.
109 static struct DatastoreRequestQueue *drq_head;
112 * Tail of request queue for the datastore.
114 static struct DatastoreRequestQueue *drq_tail;
117 * Our connection to the datastore.
119 static struct GNUNET_DATASTORE_Handle *dsh;
122 * Pointer to the currently actively running request,
123 * NULL if none is running.
125 static struct DatastoreRequestQueue *drq_running;
129 * Run the next DS request in our queue, we're done with the current
137 * Wrapper for the datastore get operation. Makes sure to trigger the
138 * next datastore operation in the queue once the operation is
141 * @param cls our 'struct DatastoreRequestQueue*'
142 * @param key key for the content
143 * @param size number of bytes in data
144 * @param data content stored
145 * @param type type of the content
146 * @param priority priority of the content
147 * @param anonymity anonymity-level for the content
148 * @param expiration expiration time for the content
149 * @param uid unique identifier for the datum;
150 * maybe 0 if no unique identifier is available
153 get_iterator (void *cls,
154 const GNUNET_HashCode * key,
157 enum GNUNET_BLOCK_Type type,
160 struct GNUNET_TIME_Absolute
164 struct DatastoreRequestQueue *gc = cls;
166 if (gc->iter == NULL)
168 /* stop the iteration */
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "Iteration terminated\n");
174 GNUNET_DATASTORE_get_next (dsh, GNUNET_NO);
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Iteration produced %u-byte result for `%s'\n",
185 gc->iter (gc->iter_cls,
186 key, size, data, type,
187 priority, anonymity, expiration, uid);
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193 "Iteration completed\n");
195 GNUNET_assert (gc == drq_running);
204 * A datastore request can be run right now. Run it.
206 * @param cls closure (of type "struct DatastoreRequestQueue*")
207 * @param tc task context, unused
210 run_next_request (void *cls,
211 const struct GNUNET_SCHEDULER_TaskContext *tc)
213 struct DatastoreRequestQueue *gc = cls;
215 gc->task = GNUNET_SCHEDULER_NO_TASK;
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218 "Running datastore request for `%s' of type %u\n",
219 GNUNET_h2s (&gc->key),
222 GNUNET_DATASTORE_get (dsh,
227 GNUNET_TIME_absolute_get_remaining(gc->timeout));
232 * Run the next DS request in our queue, we're done with the current
238 struct DatastoreRequestQueue *e;
240 GNUNET_free_non_null (drq_running);
245 GNUNET_CONTAINER_DLL_remove (drq_head, drq_tail, e);
247 GNUNET_SCHEDULER_cancel (sched, e->task);
248 e->task = GNUNET_SCHEDULER_add_now (sched,
255 * A datastore request had to be timed out.
257 * @param cls closure (unused)
258 * @param tc task context, unused
261 timeout_ds_request (void *cls,
262 const struct GNUNET_SCHEDULER_TaskContext *tc)
264 struct DatastoreRequestQueue *e = cls;
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Datastore request timed out in queue before transmission\n");
270 e->task = GNUNET_SCHEDULER_NO_TASK;
271 GNUNET_CONTAINER_DLL_remove (drq_head, drq_tail, e);
273 e->iter (e->iter_cls,
274 NULL, 0, NULL, 0, 0, 0,
275 GNUNET_TIME_UNIT_ZERO_ABS, 0);
281 * Task run during shutdown.
287 shutdown_task (void *cls,
288 const struct GNUNET_SCHEDULER_TaskContext *tc)
290 struct DatastoreRequestQueue *drq;
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "DRQ shutdown initiated\n");
296 GNUNET_assert (NULL != dsh);
297 GNUNET_DATASTORE_disconnect (dsh,
300 while (NULL != (drq = drq_head))
302 drq_head = drq->next;
303 GNUNET_SCHEDULER_cancel (sched, drq->task);
304 if (drq->iter != NULL)
305 drq->iter (drq->iter_cls,
306 NULL, 0, NULL, 0, 0, 0,
307 GNUNET_TIME_UNIT_ZERO_ABS, 0);
311 if (drq_running != NULL)
313 if (drq_running->task != GNUNET_SCHEDULER_NO_TASK)
315 GNUNET_SCHEDULER_cancel (sched,
318 if (drq_running->iter != NULL)
320 drq_running->iter (drq_running->iter_cls,
321 NULL, 0, NULL, 0, 0, 0,
322 GNUNET_TIME_UNIT_ZERO_ABS, 0);
324 GNUNET_free (drq_running);
331 * Iterate over the results for a particular key
332 * in the datastore. The iterator will only be called
333 * once initially; if the first call did contain a
334 * result, further results can be obtained by calling
335 * "GNUNET_DATASTORE_get_next" with the given argument.
337 * @param key maybe NULL (to match all entries)
338 * @param type desired type, 0 for any
339 * @param iter function to call on each matching value;
340 * will be called once with a NULL value at the end
341 * @param iter_cls closure for iter
342 * @param timeout how long to wait at most for a response
343 * @param immediate should this be queued immediately at
344 * the head of the queue (irrespecitive of the timeout)?
346 struct DatastoreRequestQueue *
347 GNUNET_FS_drq_get (const GNUNET_HashCode * key,
348 enum GNUNET_BLOCK_Type type,
349 GNUNET_DATASTORE_Iterator iter,
351 struct GNUNET_TIME_Relative timeout,
354 struct DatastoreRequestQueue *e;
355 struct DatastoreRequestQueue *bef;
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "DRQ receives request for `%s' of type %u\n",
363 e = GNUNET_malloc (sizeof (struct DatastoreRequestQueue));
364 e->timeout = GNUNET_TIME_relative_to_absolute (timeout);
365 e->forced_head = immediate;
369 e->iter_cls = iter_cls;
370 e->timeout = GNUNET_TIME_relative_to_absolute (timeout);
371 if (GNUNET_YES == immediate)
373 /* local request, highest prio, put at head of queue
374 regardless of deadline */
380 while ( (NULL != bef) &&
381 (e->timeout.value < bef->timeout.value) &&
382 (GNUNET_YES != e->forced_head) )
385 GNUNET_CONTAINER_DLL_insert_after (drq_head, drq_tail, bef, e);
386 e->task = GNUNET_SCHEDULER_add_delayed (sched,
390 if (drq_running == NULL)
397 * Cancel the given operation.
399 * @param drq the queued operation (must not have been
403 GNUNET_FS_drq_get_cancel (struct DatastoreRequestQueue *drq)
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "DRQ receives request cancellation request\n");
409 if (drq == drq_running)
411 /* 'DATASTORE_get' has already been started (and this call might
412 actually be be legal since it is possible that the client has
413 not yet received any calls to its the iterator; so we need to
414 cancel somehow; we do this by zeroing the 'iter' field, which
415 stops the iteration */
416 drq_running->iter = NULL;
420 GNUNET_CONTAINER_DLL_remove (drq_head, drq_tail, drq);
421 GNUNET_SCHEDULER_cancel (sched, drq->task);
428 * Function called to trigger obtaining the next result
429 * from the datastore.
431 * @param more GNUNET_YES to get more results, GNUNET_NO to abort
432 * iteration (with a final call to "iter" with key/data == NULL).
435 GNUNET_FS_drq_get_next (int more)
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "DRQ receives request for next result (more is %d)\n",
442 GNUNET_DATASTORE_get_next (dsh, more);
447 * Closure for 'drq_remove_cont'.
451 struct GNUNET_DATASTORE_Handle *rmdsh;
452 GNUNET_DATASTORE_ContinuationWithStatus cont;
458 drq_remove_cont (void *cls,
462 struct RemoveContext *rc = cls;
464 rc->cont (rc->cont_cls,
467 GNUNET_DATASTORE_disconnect (rc->rmdsh, GNUNET_NO);
473 * Explicitly remove some content from the database.
474 * The "cont"inuation will be called with status
475 * "GNUNET_OK" if content was removed, "GNUNET_NO"
476 * if no matching entry was found and "GNUNET_SYSERR"
477 * on all other types of errors.
479 * @param key key for the value
480 * @param size number of bytes in data
481 * @param data content stored
482 * @param cont continuation to call when done
483 * @param cont_cls closure for cont
484 * @param timeout how long to wait at most for a response
487 GNUNET_FS_drq_remove (const GNUNET_HashCode *key,
488 uint32_t size, const void *data,
489 GNUNET_DATASTORE_ContinuationWithStatus cont,
491 struct GNUNET_TIME_Relative timeout)
493 struct GNUNET_DATASTORE_Handle *rmdsh;
494 struct RemoveContext *rc;
496 rmdsh = GNUNET_DATASTORE_connect (cfg,
503 _("Failed to connect to datastore"));
506 rc = GNUNET_malloc (sizeof (struct RemoveContext));
508 rc->cont_cls = cont_cls;
510 GNUNET_DATASTORE_remove (rmdsh, key, size, data,
517 * Setup datastore request queues.
519 * @param s scheduler to use
520 * @param c configuration to use
521 * @return GNUNET_OK on success
524 GNUNET_FS_drq_init (struct GNUNET_SCHEDULER_Handle *s,
525 const struct GNUNET_CONFIGURATION_Handle *c)
529 dsh = GNUNET_DATASTORE_connect (cfg,
533 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
534 _("Failed to connect to `%s' service.\n"),
536 return GNUNET_SYSERR;
538 GNUNET_SCHEDULER_add_delayed (sched,
539 GNUNET_TIME_UNIT_FOREVER_REL,
546 /* end of gnunet-service-fs_drq.c */