indentation
[oweals/gnunet.git] / src / fs / test_gnunet_service_fs_migration.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file fs/test_gnunet_service_fs_migration.c
23  * @brief test content migration between two peers
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "fs_test_lib.h"
28 #include "gnunet_testing_lib.h"
29
30 #define VERBOSE GNUNET_NO
31
32 /**
33  * File-size we use for testing.
34  */
35 #define FILESIZE (2 * 32 * 1024)
36
37 /**
38  * How long until we give up on transmitting the message?
39  */
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
41
42 /**
43  * How long do we give the peers for content migration?
44  */
45 #define MIGRATION_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
46
47 #define SEED 42
48
49 static struct GNUNET_FS_TestDaemon *daemons[2];
50
51 static int ok;
52
53 static struct GNUNET_TIME_Absolute start_time;
54
55 static void
56 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
57 {
58   struct GNUNET_TIME_Relative del;
59   char *fancy;
60
61   GNUNET_FS_TEST_daemons_stop (2, daemons);
62   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
63   {
64     del = GNUNET_TIME_absolute_get_duration (start_time);
65     if (del.rel_value == 0)
66       del.rel_value = 1;
67     fancy =
68         GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
69                                         1000LL / del.rel_value);
70     fprintf (stdout, "Download speed was %s/s\n", fancy);
71     GNUNET_free (fancy);
72     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
73                 "Finished download, shutting down\n",
74                 (unsigned long long) FILESIZE);
75   }
76   else
77   {
78     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
79                 "Timeout during download, shutting down with error\n");
80     ok = 1;
81   }
82 }
83
84
85 static void
86 do_download (void *cls, const char *emsg)
87 {
88   struct GNUNET_FS_Uri *uri = cls;
89
90   if (emsg != NULL)
91   {
92     GNUNET_FS_TEST_daemons_stop (2, daemons);
93     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94                 "Failed to stop source daemon: %s\n", emsg);
95     GNUNET_FS_uri_destroy (uri);
96     ok = 1;
97     return;
98   }
99   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
100               "Downloading %llu bytes\n", (unsigned long long) FILESIZE);
101   start_time = GNUNET_TIME_absolute_get ();
102   GNUNET_FS_TEST_download (daemons[0],
103                            TIMEOUT, 1, SEED, uri, VERBOSE, &do_stop, NULL);
104   GNUNET_FS_uri_destroy (uri);
105 }
106
107
108 static void
109 stop_source_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 {
111   struct GNUNET_FS_Uri *uri = cls;
112   struct GNUNET_TESTING_PeerGroup *pg;
113
114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping source peer\n");
115   pg = GNUNET_FS_TEST_get_group (daemons);
116   GNUNET_TESTING_daemons_vary (pg, 1, GNUNET_NO, TIMEOUT, &do_download, uri);
117 }
118
119
120 static void
121 do_wait (void *cls, const struct GNUNET_FS_Uri *uri)
122 {
123   struct GNUNET_FS_Uri *d;
124
125   if (NULL == uri)
126   {
127     GNUNET_FS_TEST_daemons_stop (2, daemons);
128     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129                 "Timeout during upload attempt, shutting down with error\n");
130     ok = 1;
131     return;
132   }
133   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting to allow content to migrate\n");
134   d = GNUNET_FS_uri_dup (uri);
135   (void) GNUNET_SCHEDULER_add_delayed (MIGRATION_DELAY, &stop_source_peer, d);
136 }
137
138
139 static void
140 do_publish (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
141 {
142   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
143   {
144     GNUNET_FS_TEST_daemons_stop (2, daemons);
145     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146                 "Timeout during connect attempt, shutting down with error\n");
147     ok = 1;
148     return;
149   }
150   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151               "Publishing %llu bytes\n", (unsigned long long) FILESIZE);
152   GNUNET_FS_TEST_publish (daemons[1],
153                           TIMEOUT,
154                           1, GNUNET_NO, FILESIZE, SEED,
155                           VERBOSE, &do_wait, NULL);
156 }
157
158
159 static void
160 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 {
162   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
163   {
164     fprintf (stderr, "Daemons failed to start!\n");
165     GNUNET_break (0);
166     ok = 1;
167     return;
168   }
169   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170               "Daemons started, will now try to connect them\n");
171   GNUNET_FS_TEST_daemons_connect (daemons[0],
172                                   daemons[1], TIMEOUT, &do_publish, NULL);
173 }
174
175
176 static void
177 run (void *cls,
178      char *const *args,
179      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
180 {
181   GNUNET_FS_TEST_daemons_start ("test_gnunet_service_fs_migration_data.conf",
182                                 TIMEOUT, 2, daemons, &do_connect, NULL);
183 }
184
185
186 int
187 main (int argc, char *argv[])
188 {
189   char *const argvx[] = {
190     "test-gnunet-service-fs-migration",
191     "-c",
192     "fs_test_lib_data.conf",
193 #if VERBOSE
194     "-L", "DEBUG",
195 #endif
196     NULL
197   };
198   struct GNUNET_GETOPT_CommandLineOption options[] = {
199     GNUNET_GETOPT_OPTION_END
200   };
201
202   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
203   GNUNET_log_setup ("test_gnunet_service_fs_migration",
204 #if VERBOSE
205                     "DEBUG",
206 #else
207                     "WARNING",
208 #endif
209                     NULL);
210   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
211                       argvx, "test-gnunet-service-fs-migration",
212                       "nohelp", options, &run, NULL);
213   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
214   return ok;
215 }
216
217 /* end of test_gnunet_service_fs_migration.c */