-LRN: fix fip_signal_start recursion
[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_EXTRA_LOGGING
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 struct GNUNET_FS_TEST_ConnectContext *cc;
52
53 static void
54 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
55 {
56   struct GNUNET_TIME_Relative del;
57   char *fancy;
58
59   if (NULL != cc)
60   {
61     GNUNET_FS_TEST_daemons_connect_cancel (cc);
62     cc = NULL;
63   }
64   GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS, daemons);
65   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
66   {
67     del = GNUNET_TIME_absolute_get_duration (start_time);
68     if (del.rel_value == 0)
69       del.rel_value = 1;
70     fancy =
71         GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
72                                         1000LL / del.rel_value);
73     FPRINTF (stdout, "Download speed was %s/s\n", fancy);
74     GNUNET_free (fancy);
75     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished download, shutting down\n",
76                 (unsigned long long) FILESIZE);
77   }
78   else
79   {
80     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81                 "Timeout during download, shutting down with error\n");
82     ok = 1;
83   }
84 }
85
86
87 static void
88 do_download (void *cls, const struct GNUNET_FS_Uri *uri)
89 {
90   if (NULL == uri)
91   {
92     GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS, daemons);
93     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94                 "Timeout during upload attempt, shutting down with error\n");
95     ok = 1;
96     return;
97   }
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
99               (unsigned long long) FILESIZE);
100   start_time = GNUNET_TIME_absolute_get ();
101   GNUNET_FS_TEST_download (daemons[0], TIMEOUT, 1, SEED, uri, VERBOSE, &do_stop,
102                            NULL);
103 }
104
105
106 static void
107 do_publish (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 {
109   cc = NULL;
110   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
111   {
112     GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS, daemons);
113     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114                 "Timeout during connect attempt, shutting down with error\n");
115     ok = 1;
116     return;
117   }
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
119               (unsigned long long) FILESIZE);
120   GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO, FILESIZE, SEED,
121                           VERBOSE, &do_download, NULL);
122 }
123
124
125 static void
126 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
127 {
128   GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130               "Daemons started, will now try to connect them\n");
131   cc = GNUNET_FS_TEST_daemons_connect (daemons[0], daemons[1], TIMEOUT,
132                                        &do_publish, NULL);
133 }
134
135
136 static void
137 run (void *cls, char *const *args, const char *cfgfile,
138      const struct GNUNET_CONFIGURATION_Handle *cfg)
139 {
140   GNUNET_FS_TEST_daemons_start ("fs_test_lib_data.conf", TIMEOUT, NUM_DAEMONS,
141                                 daemons, &do_connect, NULL);
142 }
143
144
145 int
146 main (int argc, char *argv[])
147 {
148   char *const argvx[] = {
149     "test-gnunet-service-fs-p2p",
150     "-c",
151     "fs_test_lib_data.conf",
152 #if VERBOSE
153     "-L", "DEBUG",
154 #endif
155     NULL
156   };
157   struct GNUNET_GETOPT_CommandLineOption options[] = {
158     GNUNET_GETOPT_OPTION_END
159   };
160
161   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
162   GNUNET_log_setup ("test_gnunet_service_fs_p2p",
163 #if VERBOSE
164                     "DEBUG",
165 #else
166                     "WARNING",
167 #endif
168                     NULL);
169   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
170                       "test-gnunet-service-fs-p2p", "nohelp", options, &run,
171                       NULL);
172   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
173   return ok;
174 }
175
176 /* end of test_gnunet_service_fs_p2p.c */