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_put.c
23 * @brief API to PUT zero-anonymity index data from our datastore into the DHT
24 * @author Christian Grothoff
27 #include "gnunet-service-fs.h"
28 #include "gnunet-service-fs_put.h"
32 * How often do we at most PUT content into the DHT?
34 #define MAX_DHT_PUT_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
38 * Context for each zero-anonymity iterator.
44 * Request to datastore for DHT PUTs (or NULL).
46 struct GNUNET_DATASTORE_QueueEntry *dht_qe;
49 * Type we request from the datastore.
51 enum GNUNET_BLOCK_Type dht_put_type;
54 * ID of task that collects blocks for DHT PUTs.
56 GNUNET_SCHEDULER_TaskIdentifier dht_task;
59 * How many entires with zero anonymity of our type do we currently
60 * estimate to have in the database?
62 uint64_t zero_anonymity_count_estimate;
65 * Current offset when iterating the database.
67 uint64_t current_offset;
72 * ANY-terminated list of our operators (one per type
73 * of block that we're putting into the DHT).
75 static struct PutOperator operators[] = {
76 {NULL, GNUNET_BLOCK_TYPE_FS_KBLOCK, 0, 0, 0},
77 {NULL, GNUNET_BLOCK_TYPE_FS_SBLOCK, 0, 0, 0},
78 {NULL, GNUNET_BLOCK_TYPE_FS_NBLOCK, 0, 0, 0},
79 {NULL, GNUNET_BLOCK_TYPE_ANY, 0, 0, 0}
84 * Task that is run periodically to obtain blocks for DHT PUTs.
86 * @param cls type of blocks to gather
87 * @param tc scheduler context (unused)
90 gather_dht_put_blocks (void *cls,
91 const struct GNUNET_SCHEDULER_TaskContext *tc);
95 * Task that is run periodically to obtain blocks for DHT PUTs.
97 * @param cls type of blocks to gather
98 * @param tc scheduler context (unused)
101 delay_dht_put_blocks (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 struct PutOperator *po = cls;
104 struct GNUNET_TIME_Relative delay;
106 po->dht_task = GNUNET_SCHEDULER_NO_TASK;
107 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
109 if (po->zero_anonymity_count_estimate > 0)
112 GNUNET_TIME_relative_divide (GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY,
113 po->zero_anonymity_count_estimate);
114 delay = GNUNET_TIME_relative_min (delay, MAX_DHT_PUT_FREQ);
118 /* if we have NO zero-anonymity content yet, wait 5 minutes for some to
119 * (hopefully) appear */
120 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
123 GNUNET_SCHEDULER_add_delayed (delay, &gather_dht_put_blocks, po);
128 * Store content in DHT.
131 * @param key key for the content
132 * @param size number of bytes in data
133 * @param data content stored
134 * @param type type of the content
135 * @param priority priority of the content
136 * @param anonymity anonymity-level for the content
137 * @param expiration expiration time for the content
138 * @param uid unique identifier for the datum;
139 * maybe 0 if no unique identifier is available
142 process_dht_put_content (void *cls, const GNUNET_HashCode * key, size_t size,
143 const void *data, enum GNUNET_BLOCK_Type type,
144 uint32_t priority, uint32_t anonymity,
145 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
147 struct PutOperator *po = cls;
152 po->zero_anonymity_count_estimate = po->current_offset - 1;
153 po->current_offset = 0;
154 po->dht_task = GNUNET_SCHEDULER_add_now (&delay_dht_put_blocks, po);
157 po->zero_anonymity_count_estimate =
158 GNUNET_MAX (po->current_offset, po->zero_anonymity_count_estimate);
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "Retrieved block `%s' of type %u for DHT PUT\n", GNUNET_h2s (key),
164 GNUNET_DHT_put (GSF_dht, key, 5 /* DEFAULT_PUT_REPLICATION */,
165 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
166 type, size, data, expiration, GNUNET_TIME_UNIT_FOREVER_REL,
167 &delay_dht_put_blocks, po);
172 * Task that is run periodically to obtain blocks for DHT PUTs.
174 * @param cls type of blocks to gather
175 * @param tc scheduler context (unused)
178 gather_dht_put_blocks (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 struct PutOperator *po = cls;
182 po->dht_task = GNUNET_SCHEDULER_NO_TASK;
183 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
186 GNUNET_DATASTORE_get_zero_anonymity (GSF_dsh, po->current_offset++, 0,
188 GNUNET_TIME_UNIT_FOREVER_REL,
190 &process_dht_put_content, po);
191 if (NULL == po->dht_qe)
192 po->dht_task = GNUNET_SCHEDULER_add_now (&delay_dht_put_blocks, po);
205 while (operators[i].dht_put_type != GNUNET_BLOCK_TYPE_ANY)
207 operators[i].dht_task =
208 GNUNET_SCHEDULER_add_now (&gather_dht_put_blocks, &operators[i]);
215 * Shutdown the module.
220 struct PutOperator *po;
224 while ((po = &operators[i])->dht_put_type != GNUNET_BLOCK_TYPE_ANY)
226 if (GNUNET_SCHEDULER_NO_TASK != po->dht_task)
228 GNUNET_SCHEDULER_cancel (po->dht_task);
229 po->dht_task = GNUNET_SCHEDULER_NO_TASK;
231 if (NULL != po->dht_qe)
233 GNUNET_DATASTORE_cancel (po->dht_qe);
240 /* end of gnunet-service-fs_put.c */