2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file ats/test_ats_solver_add_address.c
22 * @brief solver test: add address, request address and wait for suggest
23 * @author Christian Grothoff
24 * @author Matthias Wachs
25 * @author Fabian Oehlmann
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_ats_service.h"
31 #include "test_ats_api_common.h"
36 static GNUNET_SCHEDULER_TaskIdentifier end_task;
41 struct GNUNET_STATISTICS_Handle *stats;
46 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
56 static struct Test_Address test_addr;
61 static struct PeerContext p;
66 struct GNUNET_HELLO_Address test_hello_address;
71 static void *test_session;
76 struct GNUNET_ATS_Information test_ats_info[2];
81 uint32_t test_ats_count;
84 * Seconds to run the test
89 * When the test starts
91 struct GNUNET_TIME_Absolute time_start;
94 * Whether to write a data file
101 struct GNUNET_DISK_FileHandle *data_file_handle;
106 char *data_file_name;
109 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
113 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
116 if (end_task == GNUNET_SCHEDULER_NO_TASK)
118 GNUNET_SCHEDULER_cancel (end_task);
119 end_task = GNUNET_SCHEDULER_NO_TASK;
122 if (NULL != sched_ats)
124 GNUNET_ATS_scheduling_done (sched_ats);
128 GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
131 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
135 /* Close data file */
138 if (GNUNET_SYSERR == GNUNET_DISK_file_close (data_file_handle))
139 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file '%s'\n",
141 GNUNET_free_non_null(data_file_name);
144 free_test_address (&test_addr);
151 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
159 struct Session *session,
160 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
161 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
162 const struct GNUNET_ATS_Information *atsi,
165 struct GNUNET_TIME_Relative time_delta;
168 GNUNET_assert (NULL != address);
169 GNUNET_assert (NULL == session);
170 GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
171 GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
173 time_delta = GNUNET_TIME_absolute_get_difference(time_start, GNUNET_TIME_absolute_get());
175 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received suggestion for peer '%s': IN %lu - OUT %lu\n",
176 GNUNET_i2s (&address->peer), ntohl(bandwidth_in.value__)/1024, ntohl(bandwidth_out.value__)/1024);
180 GNUNET_asprintf(&data,"%f\tIN %u\tOUT %u\n",
181 (double) time_delta.rel_value_us / 1000000.,
182 ntohl(bandwidth_in.value__)/1024,
183 ntohl(bandwidth_out.value__)/1024);
184 if (GNUNET_SYSERR == GNUNET_DISK_file_write(data_file_handle, data, strlen(data)))
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot write data to file `%s'\n", data_file_name);
194 stat_cb(void *cls, const char *subsystem,
195 const char *name, uint64_t value,
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
200 subsystem,name, value);
201 GNUNET_ATS_suggest_address (sched_ats, &p.id);
206 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
207 struct GNUNET_TESTING_Peer *peer)
209 stats = GNUNET_STATISTICS_create ("ats", mycfg);
210 GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
213 /* Connect to ATS scheduling */
214 sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
215 if (sched_ats == NULL)
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
218 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
222 /* Set up file handle */
223 GNUNET_asprintf (&data_file_name, "test_convergence_s%d.data", seconds);
224 data_file_handle = GNUNET_DISK_file_open (data_file_name,
225 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
226 GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
227 if (NULL == data_file_handle) {
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open data file\n");
229 GNUNET_free(data_file_handle);
230 GNUNET_free(data_file_name);
231 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
236 memset (&p.id, '1', sizeof (p.id));
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
238 GNUNET_i2s_full(&p.id));
240 /* Prepare ATS Information */
241 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
242 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
243 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
244 test_ats_info[1].value = htonl(1);
247 /* Adding address without session */
249 create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
250 test_hello_address.peer = p.id;
251 test_hello_address.transport_name = test_addr.plugin;
252 test_hello_address.address = test_addr.addr;
253 test_hello_address.address_length = test_addr.addr_len;
256 GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
257 time_start = GNUNET_TIME_absolute_get();
259 end_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_relative_get_second_(), seconds), &end, NULL);
264 test_run (void *cls, char *const *args,
266 const struct GNUNET_CONFIGURATION_Handle *
270 char *src_filename = GNUNET_strdup (__FILE__);
271 char *test_filename = cls;
272 char *config_file = "none";
277 if (NULL == (sep = (strstr (src_filename,".c"))))
285 if (NULL != (sep = strstr (test_filename, ".exe")))
288 if (NULL == (solver = strstr (test_filename, src_filename)))
293 solver += strlen (src_filename) +1;
295 if (0 == strcmp(solver, "proportional"))
297 config_file = "test_ats_solver_proportional.conf";
299 else if (0 == strcmp(solver, "mlp"))
301 config_file = "test_ats_solver_mlp.conf";
303 else if ((0 == strcmp(solver, "ril")))
305 config_file = "test_ats_solver_ril.conf";
310 GNUNET_free (src_filename);
314 GNUNET_free (src_filename);
316 if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
317 config_file, &run, NULL ))
322 main (int argc, char *argv[])
326 static struct GNUNET_GETOPT_CommandLineOption options[] = {
327 { 's', "seconds", NULL,
328 gettext_noop ("seconds to run the test"),
329 1, &GNUNET_GETOPT_set_uint, &seconds },
330 { 'd', "data-file", NULL,
331 gettext_noop ("generate data file"),
332 0, &GNUNET_GETOPT_set_one, &write_data_file},
333 GNUNET_GETOPT_OPTION_END
336 GNUNET_PROGRAM_run2 (argc, argv, argv[0], NULL, options, &test_run, argv[0], GNUNET_YES);
341 /* end of file test_ats_solver_convergence.c */