2 This file is part of GNUnet.
3 (C) 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 migration/gnunet-daemon-migration.c
23 * @brief migrating (file-sharing) content through the network; this
24 * daemon is only responsible for pushing content out (not for
25 * processing inbound messages)
26 * @author Christian Grothoff
31 #include "gnunet_constants.h"
32 #include "gnunet_core_service.h"
33 #include "gnunet_datastore_service.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_util_lib.h"
39 #define DEBUG_MIGRATION GNUNET_YES
42 * Information we keep per peer.
47 * Last time we migrated data to this peer.
49 struct GNUNET_TIME_Absolute last_migration;
57 static struct GNUNET_SCHEDULER_Handle *sched;
62 static const struct GNUNET_CONFIGURATION_Handle *cfg;
65 * Handle to the core API.
67 static struct GNUNET_CORE_Handle *handle;
70 * Handle for reporting statistics.
72 static struct GNUNET_STATISTICS_Handle *stats;
75 * Handle for the core service.
77 static struct GNUNET_CORE_Handle *handle;
80 * Handle to the datastore.
82 static struct GNUNET_DATASTORE_Handle *datastore;
85 * Anonymity level for the current block.
87 static unsigned int current_anonymity;
90 * Type of the current block.
92 static enum GNUNET_BLOCK_Type current_type;
95 * Data of the current block (already encrypted).
97 static char current_block[GNUNET_SERVER_MAX_MESSAGE_SIZE];
100 * Size of the current block.
102 static size_t current_block_size;
105 * Key of the current block.
107 static GNUNET_HashCode current_key;
110 * Task scheduled to receive content from the datastore (with some delay).
112 static GNUNET_SCHEDULER_TaskIdentifier get_task;
116 * Select a peer for transmitting the current block to.
121 /* FIXME: select a peer for transmission... */
126 * Method called whenever a peer connects.
129 * @param peer peer identity this notification is about
130 * @param latency reported latency of the connection with 'other'
131 * @param distance reported distance (DV) to 'other'
134 connect_notify (void *cls,
136 GNUNET_PeerIdentity * peer,
137 struct GNUNET_TIME_Relative latency,
140 /* FIXME: track peer */
145 * Method called whenever a peer disconnects.
148 * @param peer peer identity this notification is about
151 disconnect_notify (void *cls,
153 GNUNET_PeerIdentity * peer)
155 /* FIXME: untrack peer */
160 * Ask datastore for more content.
162 * @param tc scheduler context
165 get_content (void *cls,
166 const struct GNUNET_SCHEDULER_TaskContext *tc);
170 * An iterator over a set of items stored in the datastore.
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 expiration expiration time for the content
180 * @param uid unique identifier for the datum;
181 * maybe 0 if no unique identifier is available
184 content_processor (void *cls,
185 const GNUNET_HashCode * key,
188 enum GNUNET_BLOCK_Type type,
191 struct GNUNET_TIME_Absolute
192 expiration, uint64_t uid)
196 memcpy (current_block, data, size);
197 current_block_size = size;
199 current_anonymity = anonymity;
203 if (current_block_size == 0)
205 get_task = GNUNET_SCHEDULER_add_delayed (sched,
206 GNUNET_TIME_UNIT_MINUTES,
211 if (current_type == GNUNET_BLOCK_TYPE_ONDEMAND)
213 /* FIXME: do on-demand encoding... */
221 * Ask datastore for more content.
223 * @param tc scheduler context
226 get_content (void *cls,
227 const struct GNUNET_SCHEDULER_TaskContext *tc)
229 get_task = GNUNET_SCHEDULER_NO_TASK;
230 GNUNET_DATASTORE_get_random (datastore,
233 GNUNET_CONSTANTS_SERVICE_TIMEOUT);
238 * Function called after GNUNET_CORE_connect has succeeded
239 * (or failed for good).
242 * @param server handle to the server, NULL if we failed
243 * @param my_id ID of this peer, NULL if we failed
244 * @param publicKey public key of this peer, NULL if we failed
247 core_init (void *cls,
248 struct GNUNET_CORE_Handle * server,
249 const struct GNUNET_PeerIdentity *
252 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
256 if (datastore != NULL)
257 get_task = GNUNET_SCHEDULER_add_now (sched,
264 * Last task run during shutdown. Disconnects us from
267 * @param cls unused, NULL
268 * @param tc scheduler context
271 cleaning_task (void *cls,
272 const struct GNUNET_SCHEDULER_TaskContext *tc)
274 if (get_task != GNUNET_SCHEDULER_NO_TASK)
276 GNUNET_SCHEDULER_cancel (sched,
278 get_task = GNUNET_SCHEDULER_NO_TASK;
282 GNUNET_CORE_disconnect (handle);
285 if (datastore != NULL)
287 GNUNET_DATASTORE_disconnect (datastore, GNUNET_NO);
292 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
299 * Main function that will be run.
302 * @param s the scheduler to use
303 * @param args remaining command-line arguments
304 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
305 * @param c configuration
309 struct GNUNET_SCHEDULER_Handle * s,
312 const struct GNUNET_CONFIGURATION_Handle * c)
314 struct GNUNET_CORE_MessageHandler handlers[] =
320 stats = GNUNET_STATISTICS_create (sched, "topology", cfg);
321 handle = GNUNET_CORE_connect (sched,
323 GNUNET_TIME_UNIT_FOREVER_REL,
331 datastore = GNUNET_DATASTORE_connect (cfg, sched);
332 GNUNET_SCHEDULER_add_delayed (sched,
333 GNUNET_TIME_UNIT_FOREVER_REL,
334 &cleaning_task, NULL);
335 if ( (NULL == handle) ||
336 (NULL == datastore) )
338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
339 _("Failed to connect to `%s' service.\n"),
340 (NULL == handle) ? "core" : "datastore");
341 GNUNET_SCHEDULER_shutdown (sched);
348 * gnunet-daemon-topology command line options.
350 static struct GNUNET_GETOPT_CommandLineOption options[] = {
351 GNUNET_GETOPT_OPTION_END
356 * The main function for the topology daemon.
358 * @param argc number of arguments from the command line
359 * @param argv command line arguments
360 * @return 0 ok, 1 on error
363 main (int argc, char *const *argv)
368 GNUNET_PROGRAM_run (argc,
371 _("Content migration for anonymous file-sharing"),
373 &run, NULL)) ? 0 : 1;
377 /* end of gnunet-daemon-migration.c */