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