879f054b8c5e60a6400f926db4dda1b9b679088d
[oweals/gnunet.git] / src / fs / test_fs_download.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2008, 2009 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_fs_download.c
23  * @brief simple testcase for download
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_fs_service.h"
31
32 #define VERBOSE GNUNET_NO
33
34 #define START_ARM GNUNET_YES
35
36 #define FILESIZE (1024 * 1024 * 2)
37
38 /**
39  * How long until we give up on transmitting the message?
40  */
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
42
43 /**
44  * How long should our test-content live?
45  */ 
46 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47
48 struct PeerContext
49 {
50   struct GNUNET_CONFIGURATION_Handle *cfg;
51   struct GNUNET_PeerIdentity id;   
52 #if START_ARM
53   pid_t arm_pid;
54 #endif
55 };
56
57 static struct PeerContext p1;
58
59
60 static struct GNUNET_SCHEDULER_Handle *sched;
61
62 static struct GNUNET_FS_Handle *fs;
63
64 static struct GNUNET_FS_DownloadContext *download;
65
66 static struct GNUNET_FS_PublishContext *publish;
67
68 static char *fn;
69
70
71 static void
72 abort_download_task (void *cls,
73                      const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75   GNUNET_FS_download_stop (download, GNUNET_YES);
76   download = NULL;
77 }
78
79
80 static void
81 abort_publish_task (void *cls,
82                      const struct GNUNET_SCHEDULER_TaskContext *tc)
83 {
84   GNUNET_FS_publish_stop (publish);
85   publish = NULL;
86 }
87
88
89 static void *
90 progress_cb (void *cls, 
91              const struct GNUNET_FS_ProgressInfo *event)
92 {
93
94   switch (event->status)
95     {
96     case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
97 #if VERBOSE
98       printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
99               (unsigned long long) event->value.publish.completed,
100               (unsigned long long) event->value.publish.size,
101               event->value.publish.specifics.progress.depth,
102               (unsigned long long) event->value.publish.specifics.progress.offset);
103 #endif
104       break;
105     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
106 #if VERBOSE
107       printf ("Publish complete.\n");
108 #endif
109       GNUNET_SCHEDULER_add_continuation (sched,
110                                          GNUNET_NO,
111                                          &abort_publish_task,
112                                          NULL,
113                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
114       fn = GNUNET_DISK_mktemp ("gnunet-download-test-dstXXXXXX");
115       download = GNUNET_FS_download_start (fs,
116                                            event->value.publish.specifics.completed.chk_uri,
117                                            NULL,
118                                            fn,
119                                            0,
120                                            FILESIZE,
121                                            1,
122                                            GNUNET_FS_DOWNLOAD_OPTION_NONE,
123                                            NULL);
124       GNUNET_assert (download != NULL);
125       break;
126     case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
127 #if VERBOSE
128       printf ("Download complete.\n");
129 #endif
130       GNUNET_SCHEDULER_add_continuation (sched,
131                                          GNUNET_NO,
132                                          &abort_download_task,
133                                          NULL,
134                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
135       break;
136     case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
137 #if VERBOSE
138       printf ("Download is progressing (%llu/%llu at level %u off %llu)...\n",
139               (unsigned long long) event->value.download.completed,
140               (unsigned long long) event->value.download.size,
141               event->value.download.specifics.progress.depth,
142               (unsigned long long) event->value.download.specifics.progress.offset);
143 #endif
144       break;
145     case GNUNET_FS_STATUS_PUBLISH_ERROR:
146       fprintf (stderr,
147                "Error publishing file: %s\n",
148                event->value.publish.specifics.error.message);
149       GNUNET_break (0);
150       GNUNET_SCHEDULER_add_continuation (sched,
151                                          GNUNET_NO,
152                                          &abort_publish_task,
153                                          NULL,
154                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
155       break;
156     case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
157       fprintf (stderr,
158                "Error downloading file: %s\n",
159                event->value.download.specifics.error.message);
160       GNUNET_SCHEDULER_add_continuation (sched,
161                                          GNUNET_NO,
162                                          &abort_download_task,
163                                          NULL,
164                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
165       break;
166     case GNUNET_FS_STATUS_PUBLISH_START:
167       /* FIXME: add checks here... */
168       break;
169     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
170       /* FIXME: add checks here... */
171       break;
172     case GNUNET_FS_STATUS_DOWNLOAD_START:
173       /* FIXME: add checks here... */
174       break;
175     case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
176       /* FIXME: add checks here... */
177       GNUNET_FS_stop (fs);
178       fs = NULL;
179       break;
180     default:
181       printf ("Unexpected event: %d\n", 
182               event->status);
183       break;
184     }
185   return NULL;
186 }
187
188
189 static void
190 setup_peer (struct PeerContext *p, const char *cfgname)
191 {
192   p->cfg = GNUNET_CONFIGURATION_create ();
193 #if START_ARM
194   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
195                                         "gnunet-service-arm",
196 #if VERBOSE
197                                         "-L", "DEBUG",
198 #endif
199                                         "-c", cfgname, NULL);
200   sleep (1);                    /* allow ARM to start */
201 #endif
202   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
203   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
204 }
205
206
207 static void
208 stop_arm (struct PeerContext *p)
209 {
210 #if START_ARM
211   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
212     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
213   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
214     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216               "ARM process %u stopped\n", p->arm_pid);
217 #endif
218   GNUNET_CONFIGURATION_destroy (p->cfg);
219 }
220
221
222 static void
223 run (void *cls,
224      struct GNUNET_SCHEDULER_Handle *s,
225      char *const *args,
226      const char *cfgfile,
227      const struct GNUNET_CONFIGURATION_Handle *cfg)
228 {
229   const char *keywords[] = {
230     "down_foo",
231     "down_bar",
232   };
233   char *buf;
234   struct GNUNET_CONTAINER_MetaData *meta;
235   struct GNUNET_FS_Uri *kuri;
236   struct GNUNET_FS_FileInformation *fi;
237   size_t i;
238
239   sched = s;
240   setup_peer (&p1, "test_fs_download_data.conf");
241   fs = GNUNET_FS_start (sched,
242                         cfg,
243                         "test-fs-download",
244                         &progress_cb,
245                         NULL,
246                         GNUNET_FS_FLAGS_NONE,
247                         GNUNET_FS_OPTIONS_END);
248   GNUNET_assert (NULL != fs); 
249   buf = GNUNET_malloc (FILESIZE);
250   for (i = 0; i < FILESIZE; i++)
251     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
252   meta = GNUNET_CONTAINER_meta_data_create ();
253   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
254   fi = GNUNET_FS_file_information_create_from_data ("file-to-publish",
255                                                     FILESIZE,
256                                                     buf,
257                                                     kuri,
258                                                     meta,
259                                                     GNUNET_NO,
260                                                     1,
261                                                     42,
262                                                     GNUNET_TIME_relative_to_absolute (LIFETIME)); 
263   GNUNET_FS_uri_destroy (kuri);
264   GNUNET_CONTAINER_meta_data_destroy (meta);
265   GNUNET_assert (NULL != fi);
266   publish = GNUNET_FS_publish_start (fs,
267                                     "publish-context",
268                                     fi,
269                                     NULL, NULL, NULL,
270                                     GNUNET_FS_PUBLISH_OPTION_NONE);
271   GNUNET_assert (publish != NULL);
272 }
273
274
275 int
276 main (int argc, char *argv[])
277 {
278   char *const argvx[] = { 
279     "test-fs-download",
280     "-c",
281     "test_fs_download_data.conf",
282 #if VERBOSE
283     "-L", "DEBUG",
284 #endif
285     NULL
286   };
287   struct GNUNET_GETOPT_CommandLineOption options[] = {
288     GNUNET_GETOPT_OPTION_END
289   };
290
291   GNUNET_log_setup ("test_fs_download", 
292 #if VERBOSE
293                     "DEBUG",
294 #else
295                     "WARNING",
296 #endif
297                     NULL);
298   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
299                       argvx, "test-fs-download",
300                       "nohelp", options, &run, NULL);
301   stop_arm (&p1);
302   return 0;
303 }
304
305 /* end of test_fs_download.c */