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