fix compiler warnings
[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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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_testbed_service.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, 90)
46
47 #define SEED 42
48
49 static struct GNUNET_TESTBED_Peer *daemons[2];
50
51 static int ok;
52
53 static struct GNUNET_TIME_Absolute start_time;
54
55 static struct GNUNET_TESTBED_Operation *op;
56
57
58 struct DownloadContext
59 {
60   char *fn;
61
62   struct GNUNET_FS_Uri *uri;
63 };
64
65
66 static void
67 do_stop (void *cls)
68 {
69   struct GNUNET_TIME_Relative del;
70   char *fancy;
71
72   GNUNET_SCHEDULER_shutdown ();
73   if (0 ==
74       GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (start_time,
75                                                                     TIMEOUT)).rel_value_us)
76   {
77     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
78                 "Timeout during download, shutting down with error\n");
79     ok = 1;
80   }
81   else
82   {
83     del = GNUNET_TIME_absolute_get_duration (start_time);
84     if (del.rel_value_us == 0)
85       del.rel_value_us = 1;
86     fancy =
87         GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
88                                         1000000LL / del.rel_value_us);
89     FPRINTF (stdout,
90              "Download speed was %s/s\n",
91              fancy);
92     GNUNET_free (fancy);
93     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94                 "Finished download, shutting down\n",
95                 (unsigned long long) FILESIZE);
96   }
97 }
98
99
100 static void
101 do_download (void *cls,
102              const char *emsg)
103 {
104   struct DownloadContext *dc = cls;
105   struct GNUNET_FS_Uri *uri = dc->uri;
106
107   GNUNET_TESTBED_operation_done (op);
108   op = NULL;
109   if (NULL != dc->fn)
110   {
111     GNUNET_DISK_directory_remove (dc->fn);
112     GNUNET_free (dc->fn);
113   }
114   GNUNET_free (dc);
115   if (NULL != emsg)
116   {
117     GNUNET_SCHEDULER_shutdown ();
118     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119                 "Failed to stop source daemon: %s\n",
120                 emsg);
121     GNUNET_FS_uri_destroy (uri);
122     ok = 1;
123     return;
124   }
125   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126               "Downloading %llu bytes\n",
127               (unsigned long long) FILESIZE);
128   start_time = GNUNET_TIME_absolute_get ();
129   GNUNET_FS_TEST_download (daemons[0],
130                            TIMEOUT,
131                            1,
132                            SEED,
133                            uri,
134                            VERBOSE,
135                            &do_stop,
136                            NULL);
137   GNUNET_FS_uri_destroy (uri);
138 }
139
140
141 static void
142 stop_source_peer (void *cls)
143 {
144   struct DownloadContext *dc = cls;
145
146   /* FIXME: We should not interact with testbed when shutting down */
147   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148               "Stopping source peer\n");
149   op = GNUNET_TESTBED_peer_stop (NULL,
150                                  daemons[1],
151                                  &do_download, dc);
152   GNUNET_assert (NULL != op);
153 }
154
155
156 static void
157 do_wait (void *cls,
158          const struct GNUNET_FS_Uri *uri,
159          const char *fn)
160 {
161   struct DownloadContext *dc;
162
163   if (NULL == uri)
164   {
165     GNUNET_SCHEDULER_shutdown ();
166     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167                 "Timeout during upload attempt, shutting down with error\n");
168     ok = 1;
169     return;
170   }
171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172               "Waiting to allow content to migrate\n");
173   dc = GNUNET_new (struct DownloadContext);
174   dc->uri = GNUNET_FS_uri_dup (uri);
175   if (NULL != fn)
176     dc->fn = GNUNET_strdup (fn);
177   (void) GNUNET_SCHEDULER_add_delayed (MIGRATION_DELAY,
178                                        &stop_source_peer,
179                                        dc);
180 }
181
182
183 static void
184 do_publish (void *cls,
185             struct GNUNET_TESTBED_RunHandle *h,
186             unsigned int num_peers,
187             struct GNUNET_TESTBED_Peer **peers,
188             unsigned int links_succeeded,
189             unsigned int links_failed)
190 {
191   unsigned int i;
192
193   GNUNET_assert (2 == num_peers);
194   for (i=0;i<num_peers;i++)
195     daemons[i] = peers[i];
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197               "Publishing %llu bytes\n",
198               (unsigned long long) FILESIZE);
199   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO,
200                           FILESIZE, SEED,
201                           VERBOSE, &do_wait, NULL);
202 }
203
204
205 int
206 main (int argc,
207       char *argv[])
208 {
209   (void) GNUNET_TESTBED_test_run ("test-gnunet-service-fs-migration",
210                                   "fs_test_lib_data.conf",
211                                   2,
212                                   0, NULL, NULL,
213                                   &do_publish,
214                                   NULL);
215   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
216   return ok;
217 }
218
219 /* end of test_gnunet_service_fs_migration.c */