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_monitor.c
22 * @brief Test performance API:
23 * Add an address for a peer and request it. We expect an monitor callback
24 * @author Christian Grothoff
25 * @author Matthias Wachs
28 #include "gnunet_ats_service.h"
29 #include "gnunet_testing_lib.h"
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
33 #define SHUTDOWN_CORRECT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
37 static GNUNET_SCHEDULER_TaskIdentifier die_task;
38 static GNUNET_SCHEDULER_TaskIdentifier stage_task;
40 struct GNUNET_CONFIGURATION_Handle *cfg;
42 static struct GNUNET_ATS_SchedulingHandle *sh;
44 static struct GNUNET_ATS_PerformanceHandle *ph;
46 static struct GNUNET_HELLO_Address addr[2];
48 static struct GNUNET_ATS_Information atsi[ATS_COUNT];
52 static int res_suggest_cb_p0;
53 static int res_suggest_cb_p1;
55 static int res_perf_cb_p0;
56 static int res_perf_cb_p1;
59 * Stage 0: Init, request address and wait for peer0 suggest cb
60 * Stage 1: Got peer0 suggest cb, expect monitoring cb
61 * Stage 2: Got peer0 monitoring cb, update address and expect monitor cb
62 * Stage 3: Got 2nd peer0 monitoring cb, shutdown
68 static void cleanup_addresses ()
70 GNUNET_ATS_address_destroyed (sh, &addr[0], NULL);
71 GNUNET_ATS_address_destroyed (sh, &addr[1], NULL);
74 static void setup_addresses ()
76 memset (&addr[0].peer,'\0', sizeof (addr[0].peer));
77 addr[0].transport_name = "test0";
78 addr[0].address = "test_addr0";
79 addr[0].address_length = strlen ("test_addr0") + 1;
81 atsi[0].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
82 atsi[0].value = htonl(100);
84 atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
85 atsi[1].value = htonl(5);
87 GNUNET_ATS_address_add (sh, &addr[0], NULL, atsi, ATS_COUNT);
89 memset (&addr[1].peer,'\1', sizeof (addr[1].peer));
90 addr[1].transport_name = "test1";
91 addr[1].address = "test_addr1";
92 addr[1].address_length = strlen ("test_addr1") + 1;
94 GNUNET_ATS_address_add (sh, &addr[1], NULL, atsi, ATS_COUNT);
101 if (GNUNET_SCHEDULER_NO_TASK != stage_task)
103 GNUNET_SCHEDULER_cancel (stage_task);
104 stage_task = GNUNET_SCHEDULER_NO_TASK;
106 if (GNUNET_SCHEDULER_NO_TASK != die_task)
108 GNUNET_SCHEDULER_cancel (die_task);
109 die_task = GNUNET_SCHEDULER_NO_TASK;
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
113 cleanup_addresses ();
117 GNUNET_ATS_performance_done (ph);
123 GNUNET_ATS_scheduling_done (sh);
130 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132 die_task = GNUNET_SCHEDULER_NO_TASK;
133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error!\n");
134 if (GNUNET_NO == res_perf_cb_p0)
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not get performance information for requested peer!\n");
136 if (GNUNET_YES == res_perf_cb_p1)
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got suggestion for not requested peer!\n");
138 if (GNUNET_NO == res_suggest_cb_p0)
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not get suggestion for not peer!\n");
140 if (GNUNET_YES == res_suggest_cb_p1)
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got suggestion for not requested peer!\n");
145 static void end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Success\n");
153 perf_mon_cb (void *cls,
154 const struct GNUNET_PeerIdentity *peer,
155 const struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
156 const struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
157 const struct GNUNET_ATS_Information *ats,
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "ATS notifies about perfomance change for peer `%s'\n", GNUNET_i2s (peer));
167 if ((1 != stage) && (2 != stage))
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received 1st callback for peer `%s' with %u information\n",
177 GNUNET_i2s (&addr[0].peer), ats_count);
178 if (0 != memcmp (peer, &addr[0].peer, sizeof (addr[0].peer)))
180 res_perf_cb_p1 = GNUNET_YES;
182 if (GNUNET_SCHEDULER_NO_TASK != die_task)
183 GNUNET_SCHEDULER_cancel (die_task);
184 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
188 if (ats_count < ATS_COUNT)
191 if (GNUNET_SCHEDULER_NO_TASK != die_task)
192 GNUNET_SCHEDULER_cancel (die_task);
193 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
198 for (c1 = 0; c1 < ats_count; c1++)
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS information [%u] %u : %u \n",
201 c1, ntohl (ats[c1].type), ntohl (ats[c1].value));
202 for (c2 = 0; c2 < ATS_COUNT; c2++)
204 if (ats[c1].type == atsi[c2].type)
206 if (ats[c1].value == atsi[c2].value)
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received only %u correct ATS information \n", c3);
216 if (GNUNET_SCHEDULER_NO_TASK != die_task)
217 GNUNET_SCHEDULER_cancel (die_task);
218 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u correct ATS information \n", c3);
226 atsi[0].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
227 atsi[0].value = htonl(1000);
229 atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
230 atsi[1].value = htonl(50);
232 GNUNET_ATS_address_update (sh, &addr[0], NULL, atsi, ATS_COUNT);
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received 2nd callback for peer `%s' with %u information\n",
238 GNUNET_i2s (&addr[0].peer), ats_count);
239 if (0 != memcmp (peer, &addr[0].peer, sizeof (addr[0].peer)))
241 res_perf_cb_p1 = GNUNET_YES;
243 if (GNUNET_SCHEDULER_NO_TASK != die_task)
244 GNUNET_SCHEDULER_cancel (die_task);
245 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
249 if (ats_count < ATS_COUNT)
252 if (GNUNET_SCHEDULER_NO_TASK != die_task)
253 GNUNET_SCHEDULER_cancel (die_task);
254 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
260 for (c1 = 0; c1 < ats_count; c1++)
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS information [%u] %u : %u \n",
263 c1, ntohl (ats[c1].type), ntohl (ats[c1].value));
264 for (c2 = 0; c2 < ATS_COUNT; c2++)
266 if (ats[c1].type == atsi[c2].type)
268 if (ats[c1].value == atsi[c2].value)
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS information [%u] should be %u, but is %u \n",
273 ntohl (ats[c1].type), ntohl(atsi[c2].value), ntohl (ats[c1].value));
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received only %u correct ATS information \n", c3);
282 if (GNUNET_SCHEDULER_NO_TASK != die_task)
283 GNUNET_SCHEDULER_cancel (die_task);
284 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u correct ATS information \n", c3);
291 GNUNET_SCHEDULER_add_now (&end, NULL);
296 void ats_suggest_cb (void *cls,
297 const struct GNUNET_HELLO_Address * address,
298 struct Session * session,
299 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
300 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
301 const struct GNUNET_ATS_Information *ats,
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 "ATS is suggesting address for peer `%s'\n", GNUNET_i2s (&address->peer));
314 if (0 == memcmp (&addr[0].peer, &address->peer, sizeof (address->peer)))
316 res_suggest_cb_p0 = GNUNET_YES;
319 if (0 == memcmp (&addr[1].peer, &address->peer, sizeof (address->peer)))
320 res_suggest_cb_p1 = GNUNET_YES;
326 const struct GNUNET_CONFIGURATION_Handle *mycfg,
327 struct GNUNET_TESTING_Peer *peer)
331 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
332 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
334 sh = GNUNET_ATS_scheduling_init (cfg, &ats_suggest_cb, NULL);
335 GNUNET_assert (NULL != sh);
337 ph = GNUNET_ATS_performance_init (cfg, &perf_mon_cb, &ret, NULL, NULL);
338 GNUNET_assert (NULL != ph);
343 /* Get an active address for peer0 and expect callback */
344 GNUNET_ATS_suggest_address (sh, &addr[0].peer);
349 main (int argc, char *argv[])
351 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance_monitor",
358 /* end of file test_ats_api_performance_monitor.c */