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.c
22 * @brief basic test for the 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_addresses_mlp.h"
32 #include "test_ats_api_common.h"
42 struct GAS_MLP_Handle *mlp;
48 struct GNUNET_STATISTICS_Handle * stats;
51 * Hashmap containing addresses
53 struct GNUNET_CONTAINER_MultiHashMap * addresses;
58 struct GNUNET_PeerIdentity p;
63 struct ATS_Address *address;
68 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
73 #define MLP_MAX_EXEC_DURATION GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
74 #define MLP_MAX_ITERATIONS INT_MAX
77 set_ats (struct GNUNET_ATS_Information *ats, uint32_t type, uint32_t value)
85 int addr_it (void *cls,
86 const struct GNUNET_HashCode * key,
89 GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
97 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
99 GNUNET_SCHEDULER_cancel (timeout_task);
100 timeout_task = GNUNET_SCHEDULER_NO_TASK;
104 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
112 if (NULL != addresses)
114 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
115 GNUNET_CONTAINER_multihashmap_destroy (addresses);
120 GNUNET_free (address);
127 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bandwidth_changed_cb\n");
134 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 timeout_task = GNUNET_SCHEDULER_NO_TASK;
141 check (void *cls, char *const *args, const char *cfgfile,
142 const struct GNUNET_CONFIGURATION_Handle *cfg)
144 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
145 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
146 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
154 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
156 stats = GNUNET_STATISTICS_create("ats", cfg);
165 if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
166 GNUNET_ATS_NetworkTypeCount))
173 /* Setup address hashmap */
174 addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
176 /* Init MLP solver */
177 mlp = GAS_mlp_init (cfg, stats, quotas, quotas_out, quotas_in,
178 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL);
187 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.hashPubKey))
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
195 address = create_address (&p, "test_plugin", "test_addr", strlen("test_addr")+1, 0);
202 GNUNET_CONTAINER_multihashmap_put (addresses, &p.hashPubKey, address,
203 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
206 GAS_mlp_address_add (mlp, addresses, address);
208 GAS_mlp_get_preferred_address (mlp, addresses, &p);
212 struct ATS_Address addr[10];
213 struct ATS_Address *res[10];
214 struct GAS_MLP_SolutionContext ctx;
215 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
216 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
217 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
224 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
225 mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
226 mlp->auto_solve = GNUNET_NO;
228 struct GNUNET_PeerIdentity p[10];
230 /* Creating peer 1 */
231 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
232 /* Creating peer 2 */
233 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[1].hashPubKey);
235 /* Creating peer 1 address 1 */
236 addr[0].peer.hashPubKey = p[0].hashPubKey;
237 struct GNUNET_ATS_Information a1_ats[3];
238 set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
239 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
240 set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
241 create_address (&addr[0], "dummy", 3, &a1_ats[0]);
242 addr[0].atsp_network_type = GNUNET_ATS_NET_WAN;
244 /* Creating peer 1 address 2 */
245 addr[1].peer.hashPubKey = p[0].hashPubKey;
246 struct GNUNET_ATS_Information a2_ats[3];
247 set_ats (&a2_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
248 set_ats (&a2_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
249 set_ats (&a2_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
250 create_address (&addr[1], "dummy2", 3, &a2_ats[0]);
251 addr[1].atsp_network_type = GNUNET_ATS_NET_LAN;
253 /* Creating peer 2 address 1 */
254 addr[2].peer.hashPubKey = p[1].hashPubKey;
255 struct GNUNET_ATS_Information a3_ats[3];
256 set_ats (&a3_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
257 set_ats (&a3_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
258 set_ats (&a3_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
259 create_address (&addr[2], "dummy3", 3, &a3_ats[0]);
260 addr[2].atsp_network_type = GNUNET_ATS_NET_LAN;
262 GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
264 /* Add peer 1 address 1 */
265 GAS_mlp_address_update (mlp, addresses, &addr[0]);
267 GNUNET_assert (mlp != NULL);
268 GNUNET_assert (mlp->addr_in_problem == 1);
270 /* Update an peer 1 address 1 */
271 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
272 GAS_mlp_address_update (mlp, addresses, &addr[0]);
273 GNUNET_assert (mlp->addr_in_problem == 1);
275 /* Add peer 1 address 2 */
276 GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[1], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
277 GAS_mlp_address_update (mlp, addresses, &addr[1]);
278 GNUNET_assert (mlp->addr_in_problem == 2);
280 /* Add peer 2 address 1 */
281 GNUNET_CONTAINER_multihashmap_put(addresses, &addr[2].peer.hashPubKey, &addr[2], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
282 GAS_mlp_address_update (mlp, addresses, &addr[2]);
283 GNUNET_assert (mlp->addr_in_problem == 3);
285 GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
286 GNUNET_assert (GNUNET_OK == ctx.lp_result);
287 GNUNET_assert (GNUNET_OK == ctx.mlp_result);
289 res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
290 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[0]->plugin, res[0]->assigned_bw_out);
291 res[1] = GAS_mlp_get_preferred_address(mlp, addresses, &p[1]);
292 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[1]->plugin, res[1]->assigned_bw_out);
294 /* Delete an address */
295 GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
296 GAS_mlp_address_delete (mlp, addresses, &addr[0]);
297 GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[1].peer.hashPubKey, &addr[1]);
298 GAS_mlp_address_delete (mlp, addresses, &addr[1]);
299 GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[2].peer.hashPubKey, &addr[2]);
300 GAS_mlp_address_delete (mlp, addresses, &addr[2]);
302 GNUNET_assert (mlp->addr_in_problem == 0);
304 GNUNET_free (addr[0].plugin);
305 GNUNET_free (addr[1].plugin);
314 main (int argc, char *argv[])
317 static char *const argv2[] = { "test_ats_mlp",
324 static struct GNUNET_GETOPT_CommandLineOption options[] = {
325 GNUNET_GETOPT_OPTION_END
328 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
329 "test_ats_mlp", "nohelp", options,
336 /* end of file test_ats_api_bandwidth_consumption.c */