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