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_put.h"
31 * Request to datastore for DHT PUTs (or NULL).
33 static struct GNUNET_DATASTORE_QueueEntry *dht_qe;
36 * Type we will request for the next DHT PUT round from the datastore.
38 static enum GNUNET_BLOCK_Type dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
41 * ID of task that collects blocks for DHT PUTs.
43 static GNUNET_SCHEDULER_TaskIdentifier dht_task;
46 * How many entires with zero anonymity do we currently estimate
47 * to have in the database?
49 static unsigned int zero_anonymity_count_estimate;
53 * Task that is run periodically to obtain blocks for DHT PUTs.
55 * @param cls type of blocks to gather
56 * @param tc scheduler context (unused)
59 gather_dht_put_blocks (void *cls,
60 const struct GNUNET_SCHEDULER_TaskContext *tc);
65 * If the DHT PUT gathering task is not currently running, consider
66 * (re)scheduling it with the appropriate delay.
69 consider_dht_put_gathering (void *cls)
71 struct GNUNET_TIME_Relative delay;
77 if (dht_task != GNUNET_SCHEDULER_NO_TASK)
79 if (zero_anonymity_count_estimate > 0)
81 delay = GNUNET_TIME_relative_divide (GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY,
82 zero_anonymity_count_estimate);
83 delay = GNUNET_TIME_relative_min (delay,
88 /* if we have NO zero-anonymity content yet, wait 5 minutes for some to
90 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
92 dht_task = GNUNET_SCHEDULER_add_delayed (delay,
93 &gather_dht_put_blocks,
99 * Store content in DHT.
102 * @param key key for the content
103 * @param size number of bytes in data
104 * @param data content stored
105 * @param type type of the content
106 * @param priority priority of the content
107 * @param anonymity anonymity-level for the content
108 * @param expiration expiration time for the content
109 * @param uid unique identifier for the datum;
110 * maybe 0 if no unique identifier is available
113 process_dht_put_content (void *cls,
114 const GNUNET_HashCode * key,
117 enum GNUNET_BLOCK_Type type,
120 struct GNUNET_TIME_Absolute
121 expiration, uint64_t uid)
123 static unsigned int counter;
124 static GNUNET_HashCode last_vhash;
125 static GNUNET_HashCode vhash;
130 consider_dht_put_gathering (cls);
133 /* slightly funky code to estimate the total number of values with zero
134 anonymity from the maximum observed length of a monotonically increasing
135 sequence of hashes over the contents */
136 GNUNET_CRYPTO_hash (data, size, &vhash);
137 if (GNUNET_CRYPTO_hash_cmp (&vhash, &last_vhash) <= 0)
139 if (zero_anonymity_count_estimate > 0)
140 zero_anonymity_count_estimate /= 2;
146 if (zero_anonymity_count_estimate < (1 << counter))
147 zero_anonymity_count_estimate = (1 << counter);
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "Retrieved block `%s' of type %u for DHT PUT\n",
154 GNUNET_DHT_put (dht_handle,
156 DEFAULT_PUT_REPLICATION,
162 GNUNET_TIME_UNIT_FOREVER_REL,
163 &dht_put_continuation,
169 * Task that is run periodically to obtain blocks for DHT PUTs.
171 * @param cls type of blocks to gather
172 * @param tc scheduler context (unused)
175 gather_dht_put_blocks (void *cls,
176 const struct GNUNET_SCHEDULER_TaskContext *tc)
178 dht_task = GNUNET_SCHEDULER_NO_TASK;
181 if (dht_put_type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
182 dht_put_type = GNUNET_BLOCK_TYPE_FS_KBLOCK;
183 dht_qe = GNUNET_DATASTORE_get_zero_anonymity (dsh, 0, UINT_MAX,
184 GNUNET_TIME_UNIT_FOREVER_REL,
186 &process_dht_put_content, NULL);
187 GNUNET_assert (dht_qe != NULL);
195 * @param cfg configuration to use
198 GSF_put_init_ (struct GNUNET_CONFIGURATION_Handle *cfg)
200 dht_task = GNUNET_SCHEDULER_add_now (&gather_dht_put_blocks, NULL);
205 * Shutdown the module.
210 if (GNUNET_SCHEDULER_NO_TASK != dht_task)
212 GNUNET_SCHEDULER_cancel (dht_task);
213 dht_task = GNUNET_SCHEDULER_NO_TASK;
217 GNUNET_DATASTORE_cancel (dht_qe);
222 /* end of gnunet-service-fs_put.c */