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, "Finished download, shutting down\n",
73                 (unsigned long long) FILESIZE);
74   }
75   else
76   {
77     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78                 "Timeout during download, shutting down with error\n");
79     ok = 1;
80   }
81 }
82
83
84 static void
85 do_download (void *cls, const char *emsg)
86 {
87   struct GNUNET_FS_Uri *uri = cls;
88
89   if (emsg != NULL)
90   {
91     GNUNET_FS_TEST_daemons_stop (2, daemons);
92     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to stop source daemon: %s\n",
93                 emsg);
94     GNUNET_FS_uri_destroy (uri);
95     ok = 1;
96     return;
97   }
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
99               (unsigned long long) FILESIZE);
100   start_time = GNUNET_TIME_absolute_get ();
101   GNUNET_FS_TEST_download (daemons[0], TIMEOUT, 1, SEED, uri, VERBOSE, &do_stop,
102                            NULL);
103   GNUNET_FS_uri_destroy (uri);
104 }
105
106
107 static void
108 stop_source_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   struct GNUNET_FS_Uri *uri = cls;
111   struct GNUNET_TESTING_PeerGroup *pg;
112
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping source peer\n");
114   pg = GNUNET_FS_TEST_get_group (daemons);
115   GNUNET_TESTING_daemons_vary (pg, 1, GNUNET_NO, TIMEOUT, &do_download, uri);
116 }
117
118
119 static void
120 do_wait (void *cls, const struct GNUNET_FS_Uri *uri)
121 {
122   struct GNUNET_FS_Uri *d;
123
124   if (NULL == uri)
125   {
126     GNUNET_FS_TEST_daemons_stop (2, daemons);
127     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128                 "Timeout during upload attempt, shutting down with error\n");
129     ok = 1;
130     return;
131   }
132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting to allow content to migrate\n");
133   d = GNUNET_FS_uri_dup (uri);
134   (void) GNUNET_SCHEDULER_add_delayed (MIGRATION_DELAY, &stop_source_peer, d);
135 }
136
137
138 static void
139 do_publish (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
140 {
141   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
142   {
143     GNUNET_FS_TEST_daemons_stop (2, daemons);
144     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145                 "Timeout during connect attempt, shutting down with error\n");
146     ok = 1;
147     return;
148   }
149   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
150               (unsigned long long) FILESIZE);
151   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO, FILESIZE, SEED,
152                           VERBOSE, &do_wait, NULL);
153 }
154
155
156 static void
157 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 {
159   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
160   {
161     fprintf (stderr, "Daemons failed to start!\n");
162     GNUNET_break (0);
163     ok = 1;
164     return;
165   }
166   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167               "Daemons started, will now try to connect them\n");
168   GNUNET_FS_TEST_daemons_connect (daemons[0], daemons[1], TIMEOUT, &do_publish,
169                                   NULL);
170 }
171
172
173 static void
174 run (void *cls, char *const *args, const char *cfgfile,
175      const struct GNUNET_CONFIGURATION_Handle *cfg)
176 {
177   GNUNET_FS_TEST_daemons_start ("test_gnunet_service_fs_migration_data.conf",
178                                 TIMEOUT, 2, daemons, &do_connect, NULL);
179 }
180
181
182 int
183 main (int argc, char *argv[])
184 {
185   char *const argvx[] = {
186     "test-gnunet-service-fs-migration",
187     "-c",
188     "fs_test_lib_data.conf",
189 #if VERBOSE
190     "-L", "DEBUG",
191 #endif
192     NULL
193   };
194   struct GNUNET_GETOPT_CommandLineOption options[] = {
195     GNUNET_GETOPT_OPTION_END
196   };
197
198   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
199   GNUNET_log_setup ("test_gnunet_service_fs_migration",
200 #if VERBOSE
201                     "DEBUG",
202 #else
203                     "WARNING",
204 #endif
205                     NULL);
206   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
207                       "test-gnunet-service-fs-migration", "nohelp", options,
208                       &run, NULL);
209   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
210   return ok;
211 }
212
213 /* end of test_gnunet_service_fs_migration.c */