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_request_and_delete_address.c
22 * @brief solver test: add address, request address, delete address and wait for disconnect
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "test_ats_api_common.h"
35 static struct GNUNET_SCHEDULER_Task * die_task;
40 static struct GNUNET_STATISTICS_Handle *stats;
45 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
50 static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
60 static struct Test_Address test_addr;
65 static struct PeerContext p;
70 static struct GNUNET_HELLO_Address test_hello_address;
75 static void *test_session;
80 struct GNUNET_ATS_Information test_ats_info[2];
85 static uint32_t test_ats_count;
88 * The address we will delete.
90 static struct GNUNET_ATS_AddressRecord *ar;
92 static int address_deleted = GNUNET_NO;
96 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
101 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
104 if (die_task != NULL)
106 GNUNET_SCHEDULER_cancel (die_task);
110 if (NULL != sched_ats)
112 GNUNET_ATS_scheduling_done (sched_ats);
116 if (NULL != connect_ats)
118 GNUNET_ATS_connectivity_done (connect_ats);
121 GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
124 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
128 free_test_address (&test_addr);
135 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 address_suggest_cb (void *cls,
145 const struct GNUNET_PeerIdentity *peer,
146 const struct GNUNET_HELLO_Address *address,
147 struct Session *session,
148 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
149 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
151 if (GNUNET_NO == address_deleted)
153 /* Expected address suggestion */
154 GNUNET_assert (NULL != address);
155 GNUNET_assert (NULL == session);
156 GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
157 GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
159 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
160 "Received sugggestion for peer `%s', deleting address\n",
161 GNUNET_i2s (&address->peer));
162 address_deleted = GNUNET_YES;
163 GNUNET_ATS_address_destroy (ar);
168 /* Expecting disconnect */
170 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
171 "Received sugggestion for peer `%s': %u %u\n",
172 GNUNET_i2s (&address->peer),
173 (unsigned int) ntohl (bandwidth_in.value__),
174 (unsigned int) ntohl (bandwidth_out.value__));
176 if ((ntohl(bandwidth_in.value__) == 0) &&
177 (ntohl(bandwidth_out.value__) == 0))
179 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
180 GNUNET_SCHEDULER_add_now (&end, NULL);
187 stat_cb (void *cls, const char *subsystem,
188 const char *name, uint64_t value,
191 static struct GNUNET_ATS_ConnectivitySuggestHandle *sh;
193 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
194 "ATS statistics: `%s' `%s' %llu\n",
199 sh = GNUNET_ATS_connectivity_suggest (connect_ats, &p.id);
206 const struct GNUNET_CONFIGURATION_Handle *mycfg,
207 struct GNUNET_TESTING_Peer *peer)
209 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
210 stats = GNUNET_STATISTICS_create ("ats", mycfg);
211 GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
213 connect_ats = GNUNET_ATS_connectivity_init (mycfg);
215 /* Connect to ATS scheduling */
216 sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
217 if (sched_ats == NULL)
219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
220 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
225 memset (&p.id, '1', sizeof (p.id));
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
227 GNUNET_i2s_full(&p.id));
229 /* Prepare ATS Information */
230 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
231 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
232 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
233 test_ats_info[1].value = htonl(1);
236 /* Adding address without session */
238 create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
239 test_hello_address.peer = p.id;
240 test_hello_address.transport_name = test_addr.plugin;
241 test_hello_address.address = test_addr.addr;
242 test_hello_address.address_length = test_addr.addr_len;
245 ar = GNUNET_ATS_address_add (sched_ats, &test_hello_address,
247 test_ats_info, test_ats_count);
252 main (int argc, char *argv[])
255 char *src_filename = GNUNET_strdup (__FILE__);
256 char *test_filename = GNUNET_strdup (argv[0]);
262 if (NULL == (sep = (strstr (src_filename,".c"))))
269 if (NULL != (sep = strstr (test_filename, ".exe")))
272 if (NULL == (solver = strstr (test_filename, src_filename)))
277 solver += strlen (src_filename) +1;
279 if (0 == strcmp(solver, "proportional"))
281 config_file = "test_ats_solver_proportional.conf";
283 else if (0 == strcmp(solver, "mlp"))
285 config_file = "test_ats_solver_mlp.conf";
287 else if ((0 == strcmp(solver, "ril")))
289 config_file = "test_ats_solver_ril.conf";
294 GNUNET_free (src_filename);
295 GNUNET_free (test_filename);
299 GNUNET_free (src_filename);
300 GNUNET_free (test_filename);
302 if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
303 config_file, &run, NULL ))
304 return GNUNET_SYSERR;
309 /* end of file test_ats_solver_add_address.c */