paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / fs / test_gnunet_service_fs_migration.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010, 2012, 2015 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file fs/test_gnunet_service_fs_migration.c
21  * @brief test content migration between two peers
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "fs_test_lib.h"
26 #include "gnunet_testbed_service.h"
27
28 #define VERBOSE GNUNET_NO
29
30 /**
31  * File-size we use for testing.
32  */
33 #define FILESIZE (2 * 32 * 1024)
34
35 /**
36  * How long until we give up on transmitting the message?
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
39
40 /**
41  * How long do we give the peers for content migration?
42  */
43 #define MIGRATION_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
44
45 #define SEED 42
46
47 static struct GNUNET_TESTBED_Peer *daemons[2];
48
49 static int ok;
50
51 static struct GNUNET_TIME_Absolute start_time;
52
53 static struct GNUNET_TESTBED_Operation *op;
54
55
56 struct DownloadContext
57 {
58   char *fn;
59
60   struct GNUNET_FS_Uri *uri;
61 };
62
63
64 static void
65 do_stop (void *cls)
66 {
67   struct GNUNET_TIME_Relative del;
68   char *fancy;
69
70   GNUNET_SCHEDULER_shutdown ();
71   if (0 ==
72       GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (start_time,
73                                                                     TIMEOUT)).rel_value_us)
74   {
75     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76                 "Timeout during download, shutting down with error\n");
77     ok = 1;
78   }
79   else
80   {
81     del = GNUNET_TIME_absolute_get_duration (start_time);
82     if (del.rel_value_us == 0)
83       del.rel_value_us = 1;
84     fancy =
85         GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
86                                         1000000LL / del.rel_value_us);
87     FPRINTF (stdout,
88              "Download speed was %s/s\n",
89              fancy);
90     GNUNET_free (fancy);
91     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
92                 "Finished download, shutting down\n");
93   }
94 }
95
96
97 static void
98 do_download (void *cls,
99              const char *emsg)
100 {
101   struct DownloadContext *dc = cls;
102   struct GNUNET_FS_Uri *uri = dc->uri;
103
104   GNUNET_TESTBED_operation_done (op);
105   op = NULL;
106   if (NULL != dc->fn)
107   {
108     GNUNET_DISK_directory_remove (dc->fn);
109     GNUNET_free (dc->fn);
110   }
111   GNUNET_free (dc);
112   if (NULL != emsg)
113   {
114     GNUNET_SCHEDULER_shutdown ();
115     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116                 "Failed to stop source daemon: %s\n",
117                 emsg);
118     GNUNET_FS_uri_destroy (uri);
119     ok = 1;
120     return;
121   }
122   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123               "Downloading %llu bytes\n",
124               (unsigned long long) FILESIZE);
125   start_time = GNUNET_TIME_absolute_get ();
126   GNUNET_FS_TEST_download (daemons[0],
127                            TIMEOUT,
128                            1,
129                            SEED,
130                            uri,
131                            VERBOSE,
132                            &do_stop,
133                            NULL);
134   GNUNET_FS_uri_destroy (uri);
135 }
136
137
138 static void
139 stop_source_peer (void *cls)
140 {
141   struct DownloadContext *dc = cls;
142
143   /* FIXME: We should not interact with testbed when shutting down */
144   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145               "Stopping source peer\n");
146   op = GNUNET_TESTBED_peer_stop (NULL,
147                                  daemons[1],
148                                  &do_download, dc);
149   GNUNET_assert (NULL != op);
150 }
151
152
153 static void
154 do_wait (void *cls,
155          const struct GNUNET_FS_Uri *uri,
156          const char *fn)
157 {
158   struct DownloadContext *dc;
159
160   if (NULL == uri)
161   {
162     GNUNET_SCHEDULER_shutdown ();
163     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164                 "Timeout during upload attempt, shutting down with error\n");
165     ok = 1;
166     return;
167   }
168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169               "Waiting to allow content to migrate\n");
170   dc = GNUNET_new (struct DownloadContext);
171   dc->uri = GNUNET_FS_uri_dup (uri);
172   if (NULL != fn)
173     dc->fn = GNUNET_strdup (fn);
174   (void) GNUNET_SCHEDULER_add_delayed (MIGRATION_DELAY,
175                                        &stop_source_peer,
176                                        dc);
177 }
178
179
180 static void
181 do_publish (void *cls,
182             struct GNUNET_TESTBED_RunHandle *h,
183             unsigned int num_peers,
184             struct GNUNET_TESTBED_Peer **peers,
185             unsigned int links_succeeded,
186             unsigned int links_failed)
187 {
188   unsigned int i;
189
190   GNUNET_assert (2 == num_peers);
191   for (i=0;i<num_peers;i++)
192     daemons[i] = peers[i];
193   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194               "Publishing %llu bytes\n",
195               (unsigned long long) FILESIZE);
196   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO,
197                           FILESIZE, SEED,
198                           VERBOSE, &do_wait, NULL);
199 }
200
201
202 int
203 main (int argc,
204       char *argv[])
205 {
206   (void) GNUNET_TESTBED_test_run ("test-gnunet-service-fs-migration",
207                                   "fs_test_lib_data.conf",
208                                   2,
209                                   0, NULL, NULL,
210                                   &do_publish,
211                                   NULL);
212   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
213   return ok;
214 }
215
216 /* end of test_gnunet_service_fs_migration.c */