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 and wait for suggest
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;
64 * Alternative test address
66 static struct Test_Address alt_test_addr;
71 static struct PeerContext p;
76 static struct GNUNET_HELLO_Address test_hello_address;
81 static struct GNUNET_HELLO_Address alt_test_hello_address;
86 static void *test_session;
91 static struct GNUNET_ATS_Information test_ats_info[2];
96 static uint32_t test_ats_count;
101 int addresses_added = GNUNET_NO;
103 int first_address_suggested = GNUNET_NO;
105 int first_address_deleted = GNUNET_NO;
107 int second_address_deleted = GNUNET_NO;
109 int second_address_suggested = GNUNET_YES;
111 static const struct GNUNET_HELLO_Address *first_suggestion = NULL;
113 static const struct GNUNET_HELLO_Address *second_suggestion = NULL;
117 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
121 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
123 if (die_task != GNUNET_SCHEDULER_NO_TASK)
125 GNUNET_SCHEDULER_cancel (die_task);
126 die_task = GNUNET_SCHEDULER_NO_TASK;
129 if (NULL != sched_ats)
131 GNUNET_ATS_scheduling_done (sched_ats);
135 GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
138 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
142 free_test_address (&test_addr);
149 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
151 die_task = GNUNET_SCHEDULER_NO_TASK;
157 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
158 struct Session *session,
159 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
160 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
161 const struct GNUNET_ATS_Information *atsi,
164 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received a sugggestion for peer `%s' : `%s'\n",
165 GNUNET_i2s (&address->peer), (char *) address->address);
167 if (GNUNET_NO == first_address_suggested)
169 if (NULL == first_suggestion)
171 if ((NULL == address) || (NULL != session))
174 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
177 if ((ntohl(bandwidth_in.value__) == 0) ||
178 (ntohl(bandwidth_out.value__) == 0))
181 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
185 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received 1st sugggestion for peer `%s' : `%s'\n",
186 GNUNET_i2s (&address->peer), (char *) address->address);
188 first_suggestion = address;
189 first_address_suggested = GNUNET_YES;
191 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Deleting 1st address for peer `%s' : `%s'\n",
192 GNUNET_i2s (&address->peer), (char *) address->address);
194 GNUNET_ATS_address_destroyed (sched_ats, address, session);
195 first_address_deleted = GNUNET_YES;
199 if (GNUNET_YES == first_address_deleted)
201 if (NULL == second_suggestion)
203 GNUNET_assert (NULL != address);
204 GNUNET_assert (NULL == session);
205 GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
206 GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
208 if ((NULL == address) || (NULL != session))
211 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
214 if ((ntohl(bandwidth_in.value__) == 0) ||
215 (ntohl(bandwidth_out.value__) == 0))
218 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
222 if (first_suggestion->address_length == address->address_length)
224 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Deleted address for peer `%s' was suggested after deletion: `%s'\n",
225 GNUNET_i2s (&address->peer), (char *) address->address);
227 //GNUNET_SCHEDULER_add_now (&end_badly, NULL);
231 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received 2nd sugggestion for peer `%s' : `%s'\n",
232 GNUNET_i2s (&address->peer), (char *) address->address);
233 second_suggestion = address;
234 second_address_suggested = GNUNET_YES;
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Deleting 2nd address for peer `%s' : `%s'\n",
237 GNUNET_i2s (&address->peer), (char *) address->address);
238 GNUNET_ATS_address_destroyed (sched_ats, address, session);
239 second_address_deleted = GNUNET_YES;
243 if (GNUNET_YES ==second_address_deleted)
245 /* Expecting disconnect */
246 if ((ntohl(bandwidth_in.value__) == 0) &&
247 (ntohl(bandwidth_out.value__) == 0))
249 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
250 GNUNET_SCHEDULER_add_now (&end, NULL);
255 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Expected disconnect received address `%s' with bandwidth \n",
256 (char *) address->address);
264 stat_cb(void *cls, const char *subsystem,
265 const char *name, uint64_t value,
268 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
269 subsystem,name, value);
270 if ((GNUNET_NO == addresses_added) && (value == 2))
272 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All addresses added, requesting....\n");
273 /* We have 2 addresses, so we can request */
274 addresses_added = GNUNET_YES;
275 GNUNET_ATS_suggest_address (sched_ats, &p.id);
281 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
282 struct GNUNET_TESTING_Peer *peer)
284 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
285 stats = GNUNET_STATISTICS_create ("ats", mycfg);
286 GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
289 /* Connect to ATS scheduling */
290 sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
291 if (sched_ats == NULL)
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
294 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
299 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
302 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
305 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p.id)));
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
308 GNUNET_i2s_full(&p.id));
310 /* Prepare ATS Information */
311 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
312 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
313 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
314 test_ats_info[1].value = htonl(1);
317 /* Adding address without session */
319 create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
320 test_hello_address.peer = p.id;
321 test_hello_address.transport_name = test_addr.plugin;
322 test_hello_address.address = test_addr.addr;
323 test_hello_address.address_length = test_addr.addr_len;
325 /* Adding alternative address without session */
327 create_test_address (&alt_test_addr, "test", test_session, "alt_test", strlen ("alt_test") + 1);
328 alt_test_hello_address.peer = p.id;
329 alt_test_hello_address.transport_name = alt_test_addr.plugin;
330 alt_test_hello_address.address = alt_test_addr.addr;
331 alt_test_hello_address.address_length = alt_test_addr.addr_len;
335 GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
336 /* Adding alternative address */
337 GNUNET_ATS_address_add (sched_ats, &alt_test_hello_address, NULL, test_ats_info, test_ats_count);
342 main (int argc, char *argv[])
345 char *src_filename = GNUNET_strdup (__FILE__);
346 char *test_filename = GNUNET_strdup (argv[0]);
352 if (NULL == (sep = (strstr (src_filename,".c"))))
359 if (NULL != (sep = strstr (test_filename, ".exe")))
362 if (NULL == (solver = strstr (test_filename, src_filename)))
367 solver += strlen (src_filename) +1;
369 if (0 == strcmp(solver, "proportional"))
371 config_file = "test_ats_solver_proportional.conf";
373 else if (0 == strcmp(solver, "mlp"))
375 config_file = "test_ats_solver_mlp.conf";
377 else if ((0 == strcmp(solver, "ril")))
379 config_file = "test_ats_solver_ril.conf";
384 GNUNET_free (src_filename);
385 GNUNET_free (test_filename);
389 GNUNET_free (src_filename);
390 GNUNET_free (test_filename);
392 if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
393 config_file, &run, NULL ))
394 return GNUNET_SYSERR;
399 /* end of file test_ats_solver_add_address.c */