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