835650ef66808bbec837256cd44abbdf4440ea32
[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 2, 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_YES
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, 5)
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, 5)
46
47 #define SEED 42
48
49 static struct GNUNET_FS_TestDaemon *daemons[2];
50
51 static struct GNUNET_SCHEDULER_Handle *sched;
52
53 static int ok;
54
55 static struct GNUNET_TIME_Absolute start_time;
56
57 static void
58 do_stop (void *cls,
59          const struct GNUNET_SCHEDULER_TaskContext *tc)
60 {
61   struct GNUNET_TIME_Relative del;
62   char *fancy;
63
64   GNUNET_FS_TEST_daemons_stop (sched,
65                                2,
66                                daemons);
67   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
68     {
69       del = GNUNET_TIME_absolute_get_duration (start_time);
70       if (del.value == 0)
71         del.value = 1;
72       fancy = GNUNET_STRINGS_byte_size_fancy (((unsigned long long)FILESIZE) * 1000LL / del.value);
73       fprintf (stdout,
74                "Download speed was %s/s\n",
75                fancy);
76       GNUNET_free (fancy);
77       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78                   "Finished download, shutting down\n",
79                   (unsigned long long) FILESIZE);
80     }
81   else
82     {
83       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84                   "Timeout during download, shutting down with error\n");
85       ok = 1;
86     }
87 }
88
89
90 static void
91 do_download (void *cls,
92              const char *emsg)
93 {
94   struct GNUNET_FS_Uri *uri = cls;
95
96   if (emsg != NULL)
97     {
98       GNUNET_FS_TEST_daemons_stop (sched,
99                                    2,
100                                    daemons);
101       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
102                   "Failed to stop source daemon: %s\n",
103                   emsg);
104       ok = 1;
105       return;
106     }
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108               "Downloading %llu bytes\n",
109               (unsigned long long) FILESIZE);
110   start_time = GNUNET_TIME_absolute_get ();
111   GNUNET_FS_TEST_download (sched,
112                            daemons[0],
113                            TIMEOUT,
114                            1, SEED, uri, 
115                            VERBOSE, 
116                            &do_stop, NULL);
117 }
118
119
120 static void
121 stop_source_peer (void *cls,
122                   const struct GNUNET_SCHEDULER_TaskContext *tc)
123 {
124   struct GNUNET_FS_Uri *uri = cls;
125   struct GNUNET_TESTING_PeerGroup *pg;
126
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128               "Stopping source peer\n");
129   pg = GNUNET_FS_TEST_get_group (daemons);
130   GNUNET_TESTING_daemons_vary (pg, 1, GNUNET_NO, TIMEOUT,
131                                &do_download,
132                                uri);
133 }
134
135
136 static void
137 do_wait (void *cls,
138          const struct GNUNET_FS_Uri *uri)
139 {
140   struct GNUNET_FS_Uri *d;
141
142   if (NULL == uri)
143     {
144       GNUNET_FS_TEST_daemons_stop (sched,
145                                    2,
146                                    daemons);
147       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148                   "Timeout during upload attempt, shutting down with error\n");
149       ok = 1;
150       return;
151     }
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153               "Waiting to allow content to migrate\n"); 
154   d = GNUNET_FS_uri_dup (uri);
155   GNUNET_SCHEDULER_add_delayed (sched,
156                                 MIGRATION_DELAY,
157                                 &stop_source_peer,
158                                 d);
159 }
160
161
162
163 static void
164 do_publish (void *cls,
165             const struct GNUNET_SCHEDULER_TaskContext *tc)
166 {
167   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
168     {
169       GNUNET_FS_TEST_daemons_stop (sched,
170                                    2,
171                                    daemons);
172       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173                   "Timeout during connect attempt, shutting down with error\n");
174       ok = 1;
175       return;
176     }
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178               "Publishing %llu bytes\n",
179               (unsigned long long) FILESIZE);
180   GNUNET_FS_TEST_publish (sched,
181                           daemons[1],
182                           TIMEOUT,
183                           1, GNUNET_NO, FILESIZE, SEED, 
184                           VERBOSE, 
185                           &do_wait, NULL);
186 }
187
188
189 static void
190 do_connect (void *cls,
191             const struct GNUNET_SCHEDULER_TaskContext *tc)
192 {
193   GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195               "Daemons started, will now try to connect them\n");
196   GNUNET_FS_TEST_daemons_connect (sched,
197                                   daemons[0],
198                                   daemons[1],
199                                   TIMEOUT,
200                                   &do_publish,
201                                   NULL);  
202 }
203
204
205 static void
206 run (void *cls,
207      struct GNUNET_SCHEDULER_Handle *s,
208      char *const *args,
209      const char *cfgfile,
210      const struct GNUNET_CONFIGURATION_Handle *cfg)
211 {
212   sched = s;
213   GNUNET_FS_TEST_daemons_start (sched,
214                                 "test_gnunet_service_fs_migration_data.conf",
215                                 TIMEOUT,
216                                 2,
217                                 daemons,
218                                 &do_connect,
219                                 NULL);
220 }
221
222
223 int
224 main (int argc, char *argv[])
225 {
226   char *const argvx[] = { 
227     "test-gnunet-service-fs-migration",
228     "-c",
229     "fs_test_lib_data.conf",
230 #if VERBOSE
231     "-L", "DEBUG",
232 #endif
233     NULL
234   };
235   struct GNUNET_GETOPT_CommandLineOption options[] = {
236     GNUNET_GETOPT_OPTION_END
237   };
238
239   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
240   GNUNET_log_setup ("test_gnunet_service_fs_migration", 
241 #if VERBOSE
242                     "DEBUG",
243 #else
244                     "WARNING",
245 #endif
246                     NULL);
247   system ("netstat -ntpl");
248   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
249                       argvx, "test-gnunet-service-fs-migration",
250                       "nohelp", options, &run, NULL);
251   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
252   return ok;
253 }
254
255 /* end of test_gnunet_service_fs_migration.c */