fixing block reconstruction start/shutdown code
[oweals/gnunet.git] / src / fs / test_fs_test_lib.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_fs_test_lib.c
23  * @brief test fs test library
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, 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 ret;
48
49 static void
50 do_stop (void *cls,
51          const struct GNUNET_SCHEDULER_TaskContext *tc)
52 {
53   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
54     {
55       GNUNET_break (0);
56       ret = 1;
57     }
58   else
59     {
60       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
61                   "Finished download, shutting down\n",
62                   (unsigned long long) FILESIZE);
63     }
64   GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS,
65                                daemons);
66 }
67
68
69 static void
70 do_download (void *cls,
71              const struct GNUNET_FS_Uri *uri)
72 {
73   if (NULL == uri)
74     {
75       GNUNET_break (0);
76       GNUNET_SCHEDULER_add_now (&do_stop,
77                                 NULL);
78       ret = 1;
79       return;
80     }
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
82               "Downloading %llu bytes\n",
83               (unsigned long long) FILESIZE);
84   GNUNET_FS_TEST_download (daemons[0],
85                            TIMEOUT,
86                            1, SEED, uri, 
87                            VERBOSE, 
88                            &do_stop, NULL);
89 }
90
91
92 static void
93 do_publish (void *cls,
94             const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
97     {
98       GNUNET_break (0);
99       ret = 1;
100       GNUNET_SCHEDULER_add_now (&do_stop,
101                                 NULL);
102       return;
103     }
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105               "Publishing %llu bytes\n",
106               (unsigned long long) FILESIZE);
107   GNUNET_FS_TEST_publish (daemons[0],
108                           TIMEOUT,
109                           1, GNUNET_NO, FILESIZE, SEED, 
110                           VERBOSE, 
111                           &do_download, NULL);
112 }
113
114
115 static void
116 do_connect (void *cls,
117             const struct GNUNET_SCHEDULER_TaskContext *tc)
118 {
119   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
120     {
121       GNUNET_break (0);
122       ret = 1;
123       GNUNET_SCHEDULER_add_now (&do_stop,
124                                 NULL);
125       return;
126     }
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128               "Daemons started, will now try to connect them\n");
129   GNUNET_FS_TEST_daemons_connect (daemons[0],
130                                   daemons[1],
131                                   TIMEOUT,
132                                   &do_publish,
133                                   NULL);  
134 }
135
136
137 static void
138 run (void *cls,
139      char *const *args,
140      const char *cfgfile,
141      const struct GNUNET_CONFIGURATION_Handle *cfg)
142 {
143   GNUNET_FS_TEST_daemons_start ("fs_test_lib_data.conf",
144                                 TIMEOUT,
145                                 NUM_DAEMONS,
146                                 daemons,
147                                 &do_connect,
148                                 NULL);
149 }
150
151
152 int
153 main (int argc, char *argv[])
154 {
155   char *const argvx[] = { 
156     "test-fs-test-lib",
157     "-c",
158     "fs_test_lib_data.conf",
159 #if VERBOSE
160     "-L", "DEBUG",
161 #endif
162     NULL
163   };
164   struct GNUNET_GETOPT_CommandLineOption options[] = {
165     GNUNET_GETOPT_OPTION_END
166   };
167
168   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
169   GNUNET_log_setup ("test_fs_test_lib", 
170 #if VERBOSE
171                     "DEBUG",
172 #else
173                     "WARNING",
174 #endif
175                     NULL);
176   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
177                       argvx, "test-fs-test-lib",
178                       "nohelp", options, &run, NULL);
179   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
180   return ret;
181 }
182
183 /* end of test_fs_test_lib.c */