2 This file is part of GNUnet.
3 (C) 2010,2011 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_api_performance_list_addresses.c
22 * @brief test performance API's address listing feature
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33 static GNUNET_SCHEDULER_TaskIdentifier die_task;
38 struct GNUNET_STATISTICS_Handle *stats;
41 * Configuration handle
43 struct GNUNET_CONFIGURATION_Handle *cfg;
46 * ATS scheduling handle
48 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
51 * ATS performance handle
53 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
55 struct GNUNET_ATS_AddressListHandle* phal;
68 struct GNUNET_ATS_Information *ats;
77 struct GNUNET_PeerIdentity id;
83 static struct PeerContext p[2];
85 static struct Address p0_addresses[2];
86 static struct Address p1_addresses[2];
88 struct GNUNET_HELLO_Address p0_ha[2];
89 struct GNUNET_HELLO_Address p1_ha[2];
90 struct GNUNET_HELLO_Address *s_ha[2];
95 const struct GNUNET_SCHEDULER_TaskContext *tc);
99 ats_perf_cb (void *cls,
100 const struct GNUNET_HELLO_Address *address,
102 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
103 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
104 const struct GNUNET_ATS_Information *ats,
107 static int counter = 0;
114 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
116 GNUNET_SCHEDULER_add_now (&end,
122 "Expected 2 addresses, got %d\n",
125 GNUNET_SCHEDULER_shutdown ();
131 if (0 != memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
134 GNUNET_SCHEDULER_shutdown ();
143 stat_cb (void *cls, const char *subsystem,
144 const char *name, uint64_t value,
148 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
149 "ATS statistics: `%s' `%s' %llu\n",
155 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
156 "All addresses added, connecting to performance\n");
157 if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL)))
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160 "Failed to connect to performance API\n");
161 GNUNET_SCHEDULER_shutdown ();
162 return GNUNET_SYSERR;
164 phal = GNUNET_ATS_performance_list_addresses (perf_ats,
175 const struct GNUNET_SCHEDULER_TaskContext *tc)
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 if (die_task != GNUNET_SCHEDULER_NO_TASK )
181 GNUNET_SCHEDULER_cancel (die_task);
182 die_task = GNUNET_SCHEDULER_NO_TASK;
185 if (NULL != sched_ats)
187 GNUNET_ATS_scheduling_done (sched_ats);
193 GNUNET_ATS_performance_list_addresses_cancel (phal);
197 if (NULL != perf_ats)
199 GNUNET_ATS_performance_done (perf_ats);
203 GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
206 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
209 GNUNET_free_non_null (p0_addresses[0].addr);
210 GNUNET_free_non_null (p0_addresses[1].addr);
211 GNUNET_free_non_null (p1_addresses[0].addr);
212 GNUNET_free_non_null (p1_addresses[1].addr);
219 end_badly (void *cls,
220 const struct GNUNET_SCHEDULER_TaskContext *tc)
222 die_task = GNUNET_SCHEDULER_NO_TASK;
229 address_suggest_cb (void *cls,
230 const struct GNUNET_PeerIdentity *peer,
231 const struct GNUNET_HELLO_Address *address,
232 struct Session *session,
233 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
234 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
235 const struct GNUNET_ATS_Information *atsi,
238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239 "Did not expect suggestion callback!\n");
240 GNUNET_SCHEDULER_shutdown ();
245 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
246 struct GNUNET_TESTING_Peer *peer)
249 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
250 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
252 stats = GNUNET_STATISTICS_create ("ats", cfg);
253 GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
256 memset (&p[0].id, '1', sizeof (p[0].id));
257 p0_addresses[0].plugin = "test";
258 p0_addresses[0].session = NULL;
259 p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
260 p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
262 p0_ha[0].address = p0_addresses[0].addr;
263 p0_ha[0].address_length = p0_addresses[0].addr_len;
264 p0_ha[0].peer = p[0].id;
265 p0_ha[0].transport_name = p0_addresses[0].plugin;
267 p0_addresses[1].plugin = "test";
268 p0_addresses[1].session = NULL;
269 p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
270 p0_addresses[1].addr_len = strlen (p0_addresses[1].addr) + 1;
272 p0_ha[1].address = p0_addresses[1].addr;
273 p0_ha[1].address_length = p0_addresses[1].addr_len;
274 p0_ha[1].peer = p[0].id;
275 p0_ha[1].transport_name = p0_addresses[1].plugin;
277 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
278 GNUNET_i2s (&p[0].id));
280 memset (&p[1].id, '2', sizeof (p[1].id));
281 p1_addresses[0].plugin = "test";
282 p1_addresses[0].session = NULL;
283 p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
284 p1_addresses[0].addr_len = strlen (p1_addresses[0].addr) + 1;
286 p1_ha[0].address = p1_addresses[0].addr;
287 p1_ha[0].address_length = p1_addresses[0].addr_len;
288 p1_ha[0].peer = p[1].id;
289 p1_ha[0].transport_name = p1_addresses[0].plugin;
291 p1_addresses[1].plugin = "test";
292 p1_addresses[1].session = NULL;
293 p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
294 p1_addresses[1].addr_len = strlen (p1_addresses[1].addr) + 1;
296 p1_ha[1].address = p1_addresses[1].addr;
297 p1_ha[1].address_length = p1_addresses[1].addr_len;
298 p1_ha[1].peer = p[1].id;
299 p1_ha[1].transport_name = p1_addresses[1].plugin;
301 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 1: `%s'\n",
302 GNUNET_i2s (&p[1].id));
305 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL );
306 if (NULL == sched_ats)
308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
309 "Could not setup peer!\n");
310 GNUNET_SCHEDULER_shutdown ();
314 GNUNET_ATS_address_add (sched_ats, &p0_ha[0], NULL, NULL, 0);
315 GNUNET_ATS_address_add (sched_ats, &p0_ha[1], NULL, NULL, 0);
317 GNUNET_ATS_address_add (sched_ats, &p1_ha[0], NULL, NULL, 0);
318 GNUNET_ATS_address_add (sched_ats, &p1_ha[1], NULL, NULL, 0);
323 main (int argc, char *argv[])
326 GNUNET_TESTING_peer_run ("test_ats_api_performance",
333 /* end of file test_ats_api_performance_list_addresses.c */