2 if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL)))
4 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5 "Failed to connect to performance API\n");
6 GNUNET_SCHEDULER_add_now (end_badly, NULL);
8 This file is part of GNUnet.
9 (C) 2010-2013 Christian Grothoff (and other contributing authors)
11 GNUnet is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published
13 by the Free Software Foundation; either version 3, or (at your
14 option) any later version.
16 GNUnet is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GNUnet; see the file COPYING. If not, write to the
23 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA.
27 * @file ats/test_ats_solver_add_address.c
28 * @brief solver test: add address, request address, delete address and wait for disconnect
29 * @author Christian Grothoff
30 * @author Matthias Wachs
33 #include "gnunet_util_lib.h"
34 #include "gnunet_testbed_service.h"
35 #include "gnunet_ats_service.h"
36 #include "test_ats_api_common.h"
41 static GNUNET_SCHEDULER_TaskIdentifier die_task;
46 struct GNUNET_STATISTICS_Handle *stats;
51 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
61 static struct Test_Address test_addr;
66 static struct PeerContext p;
71 struct GNUNET_HELLO_Address test_hello_address;
76 static void *test_session;
81 struct GNUNET_ATS_Information test_ats_info[2];
86 static uint32_t test_ats_count;
89 static int address_deleted = GNUNET_NO;
92 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
96 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
99 if (die_task != GNUNET_SCHEDULER_NO_TASK)
101 GNUNET_SCHEDULER_cancel (die_task);
102 die_task = GNUNET_SCHEDULER_NO_TASK;
105 if (NULL != sched_ats)
107 GNUNET_ATS_scheduling_done (sched_ats);
111 GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
114 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
118 free_test_address (&test_addr);
125 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
127 die_task = GNUNET_SCHEDULER_NO_TASK;
134 address_suggest_cb (void *cls,
135 const struct GNUNET_PeerIdentity *peer,
136 const struct GNUNET_HELLO_Address *address,
137 struct Session *session,
138 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
139 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
140 const struct GNUNET_ATS_Information *atsi,
144 if (GNUNET_NO == address_deleted)
146 /* Expected address suggestion */
147 GNUNET_assert (NULL != address);
148 GNUNET_assert (NULL == session);
149 GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
150 GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
152 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
153 "Received sugggestion for peer `%s', deleting address\n",
154 GNUNET_i2s (&address->peer));
155 address_deleted = GNUNET_YES;
156 GNUNET_ATS_address_destroyed (sched_ats, &test_hello_address, NULL);
161 /* Expecting disconnect */
163 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
164 "Received sugggestion for peer `%s': %u %u\n",
165 GNUNET_i2s (&address->peer),
166 (unsigned int) ntohl (bandwidth_in.value__),
167 (unsigned int) ntohl (bandwidth_out.value__));
169 if ((ntohl(bandwidth_in.value__) == 0) &&
170 (ntohl(bandwidth_out.value__) == 0))
172 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
173 GNUNET_SCHEDULER_add_now (&end, NULL);
181 stat_cb(void *cls, const char *subsystem,
182 const char *name, uint64_t value,
186 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
187 subsystem,name, value);
188 if (GNUNET_NO == address_deleted)
189 GNUNET_ATS_suggest_address (sched_ats, &p.id, NULL, NULL);
194 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
195 struct GNUNET_TESTING_Peer *peer)
197 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
198 stats = GNUNET_STATISTICS_create ("ats", mycfg);
199 GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
202 /* Connect to ATS scheduling */
203 sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
204 if (sched_ats == NULL)
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
207 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
212 memset (&p.id, '1', sizeof (p.id));
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
214 GNUNET_i2s_full(&p.id));
216 /* Prepare ATS Information */
217 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
218 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
219 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
220 test_ats_info[1].value = htonl(1);
223 /* Adding address without session */
225 create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
226 test_hello_address.peer = p.id;
227 test_hello_address.transport_name = test_addr.plugin;
228 test_hello_address.address = test_addr.addr;
229 test_hello_address.address_length = test_addr.addr_len;
232 GNUNET_ATS_address_add (sched_ats, &test_hello_address,
233 NULL, test_ats_info, test_ats_count);
239 main (int argc, char *argv[])
242 char *src_filename = GNUNET_strdup (__FILE__);
243 char *test_filename = GNUNET_strdup (argv[0]);
249 if (NULL == (sep = (strstr (src_filename,".c"))))
256 if (NULL != (sep = strstr (test_filename, ".exe")))
259 if (NULL == (solver = strstr (test_filename, src_filename)))
264 solver += strlen (src_filename) +1;
266 if (0 == strcmp(solver, "proportional"))
268 config_file = "test_ats_solver_proportional.conf";
270 else if (0 == strcmp(solver, "mlp"))
272 config_file = "test_ats_solver_mlp.conf";
274 else if ((0 == strcmp(solver, "ril")))
276 config_file = "test_ats_solver_ril.conf";
281 GNUNET_free (src_filename);
282 GNUNET_free (test_filename);
286 GNUNET_free (src_filename);
287 GNUNET_free (test_filename);
289 if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
290 config_file, &run, NULL ))
291 return GNUNET_SYSERR;
296 /* end of file test_ats_solver_add_address.c */