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 2 addresses, request address, delete, expect alternative
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;
55 static struct Test_Address test_addr;
58 * Alternative test address
60 static struct Test_Address alt_test_addr;
65 static struct PeerContext p;
70 static struct GNUNET_HELLO_Address test_hello_address;
75 static struct GNUNET_HELLO_Address alt_test_hello_address;
80 static void *test_session;
85 static struct GNUNET_ATS_Information test_ats_info[2];
90 static uint32_t test_ats_count;
95 static int addresses_added = GNUNET_NO;
97 static int first_address_suggested = GNUNET_NO;
99 static int first_address_deleted = GNUNET_NO;
101 static int second_address_deleted = GNUNET_NO;
103 static int second_address_suggested = GNUNET_YES;
105 static struct GNUNET_HELLO_Address *first_suggestion = NULL;
107 static struct GNUNET_HELLO_Address *second_suggestion = NULL;
111 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
115 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 if (die_task != NULL)
119 GNUNET_SCHEDULER_cancel (die_task);
123 if (NULL != sched_ats)
125 GNUNET_ATS_scheduling_done (sched_ats);
129 GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
132 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
136 free_test_address (&test_addr);
137 GNUNET_free_non_null (first_suggestion);
138 GNUNET_free_non_null (second_suggestion);
144 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 if (NULL != die_task)
157 GNUNET_SCHEDULER_cancel (die_task);
160 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
164 address_suggest_cb (void *cls,
165 const struct GNUNET_PeerIdentity *peer,
166 const struct GNUNET_HELLO_Address *address,
167 struct Session *session,
168 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
169 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
171 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received a sugggestion for peer `%s' : `%s'\n",
172 GNUNET_i2s (&address->peer), (char *) address->address);
174 if (GNUNET_NO == first_address_suggested)
176 if (NULL == first_suggestion)
178 if ((NULL == address) || (NULL != session))
184 if ((ntohl(bandwidth_in.value__) == 0) ||
185 (ntohl(bandwidth_out.value__) == 0))
192 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received 1st sugggestion for peer `%s' : `%s'\n",
193 GNUNET_i2s (&address->peer), (char *) address->address);
195 first_suggestion = GNUNET_HELLO_address_copy (address);
196 first_address_suggested = GNUNET_YES;
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Deleting 1st address for peer `%s' : `%s'\n",
200 GNUNET_i2s (&address->peer), (char *) address->address);
201 GNUNET_ATS_address_destroyed (sched_ats, address, session);
202 first_address_deleted = GNUNET_YES;
207 if (GNUNET_YES == first_address_deleted)
209 if (NULL == second_suggestion)
211 if ((NULL == address) || (NULL != session))
218 if (0 != memcmp (address->address, first_suggestion->address,
219 (first_suggestion->address_length < address->address_length) ? first_suggestion->address_length : address->address_length))
221 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received 2nd sugggestion for peer `%s' : `%s'\n",
222 GNUNET_i2s (&address->peer), (char *) address->address);
223 second_suggestion = GNUNET_HELLO_address_copy (address);
224 second_address_suggested = GNUNET_YES;
226 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Deleting 2nd address for peer `%s' : `%s'\n",
227 GNUNET_i2s (&address->peer), (char *) address->address);
228 GNUNET_ATS_address_destroyed (sched_ats, address, session);
229 second_address_deleted = GNUNET_YES;
235 if (GNUNET_YES == second_address_deleted)
237 /* Expecting disconnect */
238 if ((ntohl(bandwidth_in.value__) == 0) &&
239 (ntohl(bandwidth_out.value__) == 0))
241 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS tells me to disconnect\n");
242 GNUNET_SCHEDULER_add_now (&end, NULL);
247 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Expected disconnect but received address `%s' with bandwidth \n",
248 (char *) address->address);
256 stat_cb(void *cls, const char *subsystem,
257 const char *name, uint64_t value,
260 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
261 subsystem,name, value);
262 if ((GNUNET_NO == addresses_added) && (value == 2))
264 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All addresses added, requesting....\n");
265 /* We have 2 addresses, so we can request */
266 addresses_added = GNUNET_YES;
267 GNUNET_ATS_suggest_address (sched_ats, &p.id);
274 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
275 struct GNUNET_TESTING_Peer *peer)
277 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
278 stats = GNUNET_STATISTICS_create ("ats", mycfg);
279 GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
282 /* Connect to ATS scheduling */
283 sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
284 if (sched_ats == NULL)
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
292 memset (&p.id, '1', sizeof (p.id));
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
294 GNUNET_i2s_full(&p.id));
296 /* Prepare ATS Information */
297 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
298 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
299 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
300 test_ats_info[1].value = htonl(1);
303 /* Adding address without session */
305 create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
306 test_hello_address.peer = p.id;
307 test_hello_address.transport_name = test_addr.plugin;
308 test_hello_address.address = test_addr.addr;
309 test_hello_address.address_length = test_addr.addr_len;
311 /* Adding alternative address without session */
313 create_test_address (&alt_test_addr, "test", test_session, "alt_test", strlen ("alt_test") + 1);
314 alt_test_hello_address.peer = p.id;
315 alt_test_hello_address.transport_name = alt_test_addr.plugin;
316 alt_test_hello_address.address = alt_test_addr.addr;
317 alt_test_hello_address.address_length = alt_test_addr.addr_len;
321 GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
322 /* Adding alternative address */
323 GNUNET_ATS_address_add (sched_ats, &alt_test_hello_address, NULL, test_ats_info, test_ats_count);
328 main (int argc, char *argv[])
331 char *src_filename = GNUNET_strdup (__FILE__);
332 char *test_filename = GNUNET_strdup (argv[0]);
338 if (NULL == (sep = (strstr (src_filename,".c"))))
345 if (NULL != (sep = strstr (test_filename, ".exe")))
348 if (NULL == (solver = strstr (test_filename, src_filename)))
353 solver += strlen (src_filename) +1;
355 if (0 == strcmp(solver, "proportional"))
357 config_file = "test_ats_solver_proportional.conf";
359 else if (0 == strcmp(solver, "mlp"))
361 config_file = "test_ats_solver_mlp.conf";
363 else if ((0 == strcmp(solver, "ril")))
365 config_file = "test_ats_solver_ril.conf";
370 GNUNET_free (src_filename);
371 GNUNET_free (test_filename);
375 GNUNET_free (src_filename);
376 GNUNET_free (test_filename);
378 if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
379 config_file, &run, NULL ))
380 return GNUNET_SYSERR;
385 /* end of file test_ats_solver_add_address.c */