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_mlp_updating.c
22 * @brief basic test for updating ATS in MLP solver
23 * @author Christian Grothoff
24 * @author Matthias Wachs
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_ats_service.h"
31 #include "gnunet-service-ats-solver_mlp.h"
32 #include "gnunet-service-ats_normalization.h"
33 #include "test_ats_api_common.h"
43 struct GNUNET_ATS_Information ats[4];
45 struct GNUNET_ATS_Information ats_prev[4];
50 struct GAS_MLP_Handle *mlp;
56 struct GNUNET_STATISTICS_Handle * stats;
59 * Hashmap containing addresses
61 struct GNUNET_CONTAINER_MultiHashMap * addresses;
66 struct GNUNET_PeerIdentity p[2];
71 struct ATS_Address *address[3];
76 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
79 int addr_it (void *cls,
80 const struct GNUNET_HashCode * key,
83 struct ATS_Address *address = (struct ATS_Address *) value;
84 GAS_mlp_address_delete (mlp, address, GNUNET_NO);
85 GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
86 GNUNET_free (address);
94 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
96 GNUNET_SCHEDULER_cancel (timeout_task);
97 timeout_task = GNUNET_SCHEDULER_NO_TASK;
101 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
104 if (NULL != addresses)
106 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
107 GNUNET_CONTAINER_multihashmap_destroy (addresses);
115 GAS_normalization_stop ();
121 end_correctly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Test ending with success\n"));
128 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
131 timeout_task = GNUNET_SCHEDULER_NO_TASK;
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Test ending with timeout\n"));
136 static const double *
137 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
139 return GAS_normalization_get_preferences (id);
143 get_property_cb (void *cls, const struct ATS_Address *address)
145 return GAS_normalization_get_properties ((struct ATS_Address *) address);
149 normalized_property_changed_cb (void *cls,
150 const struct ATS_Address *peer,
159 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
161 static int cb_p0 = GNUNET_NO;
163 unsigned long long in = ntohl(address->assigned_bw_in.value__);
164 unsigned long long out = ntohl(address->assigned_bw_out.value__);
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP suggests for peer `%s' address `%s':`%s' in %llu out %llu \n",
167 GNUNET_i2s(&address->peer),
172 if ((in > 0) && (out > 0) &&
173 (0 == memcmp(&p[0], &address->peer, sizeof (address->peer))))
177 GNUNET_SCHEDULER_add_now (&end_correctly, NULL);
182 check (void *cls, char *const *args, const char *cfgfile,
183 const struct GNUNET_CONFIGURATION_Handle *cfg)
185 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
186 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
187 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
195 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
197 stats = GNUNET_STATISTICS_create("ats", cfg);
206 if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
207 GNUNET_ATS_NetworkTypeCount))
213 GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL);
215 /* Setup address hashmap */
216 addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
218 /* Init MLP solver */
219 mlp = GAS_mlp_init (cfg, stats, addresses,
220 quotas, quotas_out, quotas_in,
221 GNUNET_ATS_NetworkTypeCount,
222 &bandwidth_changed_cb, NULL,
223 &get_preferences_cb, NULL,
224 &get_property_cb, NULL);
231 mlp->mlp_auto_solve = GNUNET_NO;
234 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].hashPubKey))
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
241 /* Create address 0 */
242 address[0] = create_address (&p[0], "test_plugin0", "test_addr0", strlen("test_addr0")+1, 0);
243 if (NULL == address[0])
249 GNUNET_CONTAINER_multihashmap_put (addresses, &p[0].hashPubKey, address[0],
250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
251 /* Adding address 0 */
252 GAS_mlp_address_add (mlp, address[0], GNUNET_ATS_NET_UNSPECIFIED);
254 /* Retrieving preferred address for peer and wait for callback */
255 GAS_mlp_get_preferred_address (mlp, &p[0]);
257 /* Solve problem to build matrix */
258 GAS_mlp_solve_problem (mlp);
260 address[0]->atsi = ats;
261 address[0]->atsi_count = 4;
262 /* Updating address 0*/
263 ats[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
264 ats[0].value = htonl (GNUNET_ATS_NET_WAN);
265 ats[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
266 ats[1].value = htonl (10);
267 ats[2].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
268 ats[2].value = htonl (1);
269 ats[3].type = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
270 ats[3].value = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
272 ats_prev[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
273 ats_prev[0].value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
274 ats_prev[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
275 ats_prev[1].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
276 ats_prev[2].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
277 ats_prev[2].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
278 ats_prev[3].type = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
279 ats_prev[3].value = htonl (GNUNET_ATS_VALUE_UNDEFINED);
281 GAS_mlp_address_update (mlp, address[0], 1, GNUNET_NO, ats_prev, 4);
283 /* Solve problem to build matrix */
284 GAS_mlp_solve_problem (mlp);
289 main (int argc, char *argv[])
292 static char *const argv2[] = { "test_ats_mlp_updating",
299 static struct GNUNET_GETOPT_CommandLineOption options[] = {
300 GNUNET_GETOPT_OPTION_END
303 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
304 "test_ats_mlp_updating", "nohelp", options,
311 /* end of file test_ats_mlp_updating.c */