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