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_feedback
22 * @brief Test performance API:
23 * Test for address feedback API
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_addrinfo_cb_p0;
56 static int res_addrinfo_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);
77 if (GNUNET_SCHEDULER_NO_TASK != stage_task)
79 GNUNET_SCHEDULER_cancel (stage_task);
80 stage_task = GNUNET_SCHEDULER_NO_TASK;
82 if (GNUNET_SCHEDULER_NO_TASK != die_task)
84 GNUNET_SCHEDULER_cancel (die_task);
85 die_task = GNUNET_SCHEDULER_NO_TASK;
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
93 GNUNET_ATS_performance_done (ph);
99 GNUNET_ATS_scheduling_done (sh);
107 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 die_task = GNUNET_SCHEDULER_NO_TASK;
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error!\n");
111 if (GNUNET_NO == res_addrinfo_cb_p0)
112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not get address information for requested peer0!\n");
113 if (GNUNET_NO == res_addrinfo_cb_p1)
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not get address information for requested peer1!\n");
115 if (GNUNET_NO == res_suggest_cb_p0)
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not get suggestion for not peer!\n");
117 if (GNUNET_YES == res_suggest_cb_p1)
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got suggestion for not requested peer!\n");
123 static void end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Success\n");
130 addrinfo_cb (void *cls,
131 const struct GNUNET_HELLO_Address *address,
133 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
134 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
135 const struct GNUNET_ATS_Information *ats,
138 //static int shutdown = GNUNET_NO;
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140 "ATS has information about address for peer `%s'\n", GNUNET_i2s (&address->peer));
142 if (0 == memcmp (&addr[0].peer, &address->peer, sizeof (address->peer)))
143 res_addrinfo_cb_p0 = GNUNET_YES;
144 if (0 == memcmp (&addr[1].peer, &address->peer, sizeof (address->peer)))
145 res_addrinfo_cb_p1 = GNUNET_YES;
148 void ats_suggest_cb (void *cls,
149 const struct GNUNET_HELLO_Address * address,
150 struct Session * session,
151 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
152 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
153 const struct GNUNET_ATS_Information *ats,
156 static int feedbacks = 0;
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "ATS is suggesting address for peer `%s'\n", GNUNET_i2s (&address->peer));
165 if (0 == memcmp (&addr[0].peer, &address->peer, sizeof (address->peer)))
167 res_suggest_cb_p0 = GNUNET_YES;
169 GNUNET_ATS_address_update (sh, &addr[1], NULL, atsi, ATS_COUNT);
170 GNUNET_ATS_performance_give_feedback (ph, &addr[0].peer, GNUNET_ATS_PREFERENCE_BANDWIDTH,(double) 1000, GNUNET_ATS_PREFERENCE_END);
172 if (0 == memcmp (&addr[1].peer, &address->peer, sizeof (address->peer)))
174 GNUNET_ATS_performance_give_feedback (ph, &addr[1].peer, GNUNET_ATS_PREFERENCE_BANDWIDTH,(double) 1000, GNUNET_ATS_PREFERENCE_END);
175 res_suggest_cb_p1 = GNUNET_YES;
180 static void next (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
182 memset (&addr[0].peer,'\0', sizeof (addr[0].peer));
183 addr[0].transport_name = "test0";
184 addr[0].address = "test_addr0";
185 addr[0].address_length = strlen ("test_addr0") + 1;
187 atsi[0].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
188 atsi[0].value = htonl(100);
190 atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
191 atsi[1].value = htonl(5);
193 GNUNET_ATS_address_add (sh, &addr[0], NULL, atsi, ATS_COUNT);
194 GNUNET_ATS_suggest_address (sh, &addr[0].peer);
196 memset (&addr[1].peer,'\1', sizeof (addr[1].peer));
197 addr[1].transport_name = "test1";
198 addr[1].address = "test_addr1";
199 addr[1].address_length = strlen ("test_addr1") + 1;
201 GNUNET_ATS_address_add (sh, &addr[1], NULL, atsi, ATS_COUNT);
202 GNUNET_ATS_suggest_address (sh, &addr[1].peer);
203 GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &end, NULL);
208 const struct GNUNET_CONFIGURATION_Handle *mycfg,
209 struct GNUNET_TESTING_Peer *peer)
213 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
214 // die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
216 ph = GNUNET_ATS_performance_init (cfg, &addrinfo_cb, NULL);
217 GNUNET_assert (NULL != ph);
219 sh = GNUNET_ATS_scheduling_init (cfg, &ats_suggest_cb, NULL);
220 GNUNET_assert (NULL != sh);
222 GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &next, NULL);
227 main (int argc, char *argv[])
229 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance_monitor",
236 /* end of file test_ats_api_performance_feedback.c */