2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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)
37 * How many replicas do we try to create per PUT?
39 #define DEFAULT_PUT_REPLICATION 5
43 * Context for each zero-anonymity iterator.
49 * Request to datastore for DHT PUTs (or NULL).
51 struct GNUNET_DATASTORE_QueueEntry *dht_qe;
54 * Type we request from the datastore.
56 enum GNUNET_BLOCK_Type dht_put_type;
59 * Handle to PUT operation.
61 struct GNUNET_DHT_PutHandle *dht_put;
64 * ID of task that collects blocks for DHT PUTs.
66 struct GNUNET_SCHEDULER_Task * dht_task;
69 * How many entires with zero anonymity of our type do we currently
70 * estimate to have in the database?
72 uint64_t zero_anonymity_count_estimate;
75 * Count of results received from the database.
77 uint64_t result_count;
80 * Next UID to request when iterating the database.
87 * ANY-terminated list of our operators (one per type
88 * of block that we're putting into the DHT).
90 static struct PutOperator operators[] = {
91 {NULL, GNUNET_BLOCK_TYPE_FS_UBLOCK, 0, 0, 0},
92 {NULL, GNUNET_BLOCK_TYPE_ANY, 0, 0, 0}
97 * Task that is run periodically to obtain blocks for DHT PUTs.
99 * @param cls type of blocks to gather
100 * @param tc scheduler context (unused)
103 gather_dht_put_blocks (void *cls);
107 * Calculate when to run the next PUT operation and schedule it.
109 * @param po put operator to schedule
112 schedule_next_put (struct PutOperator *po)
114 struct GNUNET_TIME_Relative delay;
116 if (po->zero_anonymity_count_estimate > 0)
119 GNUNET_TIME_relative_divide (GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY,
120 po->zero_anonymity_count_estimate);
121 delay = GNUNET_TIME_relative_min (delay, MAX_DHT_PUT_FREQ);
125 /* if we have NO zero-anonymity content yet, wait 5 minutes for some to
126 * (hopefully) appear */
127 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
130 GNUNET_SCHEDULER_add_delayed (delay, &gather_dht_put_blocks, po);
135 * Continuation called after DHT PUT operation has finished.
137 * @param cls type of blocks to gather
138 * @param success GNUNET_OK if the PUT was transmitted,
139 * GNUNET_NO on timeout,
140 * GNUNET_SYSERR on disconnect from service
141 * after the PUT message was transmitted
142 * (so we don't know if it was received or not)
145 delay_dht_put_blocks (void *cls, int success)
147 struct PutOperator *po = cls;
150 schedule_next_put (po);
155 * Task that is run periodically to obtain blocks for DHT PUTs.
157 * @param cls type of blocks to gather
160 delay_dht_put_task (void *cls)
162 struct PutOperator *po = cls;
165 schedule_next_put (po);
170 * Store content in DHT.
173 * @param key key for the content
174 * @param size number of bytes in data
175 * @param data content stored
176 * @param type type of the content
177 * @param priority priority of the content
178 * @param anonymity anonymity-level for the content
179 * @param replication replication-level for the content
180 * @param expiration expiration time for the content
181 * @param uid unique identifier for the datum;
182 * maybe 0 if no unique identifier is available
185 process_dht_put_content (void *cls,
186 const struct GNUNET_HashCode * key,
189 enum GNUNET_BLOCK_Type type,
192 uint32_t replication,
193 struct GNUNET_TIME_Absolute expiration,
196 struct PutOperator *po = cls;
201 po->zero_anonymity_count_estimate = po->result_count;
202 po->result_count = 0;
204 po->dht_task = GNUNET_SCHEDULER_add_now (&delay_dht_put_task, po);
208 po->next_uid = uid + 1;
209 po->zero_anonymity_count_estimate =
210 GNUNET_MAX (po->result_count, po->zero_anonymity_count_estimate);
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Retrieved block `%s' of type %u for DHT PUT\n", GNUNET_h2s (key),
214 po->dht_put = GNUNET_DHT_put (GSF_dht,
216 DEFAULT_PUT_REPLICATION,
217 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
222 &delay_dht_put_blocks,
228 * Task that is run periodically to obtain blocks for DHT PUTs.
230 * @param cls type of blocks to gather
233 gather_dht_put_blocks (void *cls)
235 struct PutOperator *po = cls;
239 GNUNET_DATASTORE_get_zero_anonymity (GSF_dsh,
244 &process_dht_put_content,
246 if (NULL == po->dht_qe)
247 po->dht_task = GNUNET_SCHEDULER_add_now (&delay_dht_put_task, po);
260 while (operators[i].dht_put_type != GNUNET_BLOCK_TYPE_ANY)
262 operators[i].dht_task =
263 GNUNET_SCHEDULER_add_now (&gather_dht_put_blocks, &operators[i]);
270 * Shutdown the module.
275 struct PutOperator *po;
279 while ((po = &operators[i])->dht_put_type != GNUNET_BLOCK_TYPE_ANY)
281 if (NULL != po->dht_task)
283 GNUNET_SCHEDULER_cancel (po->dht_task);
286 if (NULL != po->dht_put)
288 GNUNET_DHT_put_cancel (po->dht_put);
291 if (NULL != po->dht_qe)
293 GNUNET_DATASTORE_cancel (po->dht_qe);
300 /* end of gnunet-service-fs_put.c */