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[2];
63 struct ATS_Address *address[3];
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 struct ATS_Address *address = (struct ATS_Address *) value;
90 GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
91 GNUNET_free (address);
99 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
101 GNUNET_SCHEDULER_cancel (timeout_task);
102 timeout_task = GNUNET_SCHEDULER_NO_TASK;
106 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
114 if (NULL != addresses)
116 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
117 GNUNET_CONTAINER_multihashmap_destroy (addresses);
124 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP suggests for peer `%s' address `%s':`%s' in %llu out %llu \n",
127 GNUNET_i2s(&address->peer), address->plugin, address->addr,
128 ntohl(address->assigned_bw_in.value__),
129 ntohl(address->assigned_bw_out.value__));
134 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 timeout_task = GNUNET_SCHEDULER_NO_TASK;
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Test failed: timeout\n"));
143 check (void *cls, char *const *args, const char *cfgfile,
144 const struct GNUNET_CONFIGURATION_Handle *cfg)
146 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
147 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
148 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
149 struct GNUNET_ATS_Information ats;
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
157 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
159 stats = GNUNET_STATISTICS_create("ats", cfg);
168 if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
169 GNUNET_ATS_NetworkTypeCount))
176 /* Setup address hashmap */
177 addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
179 /* Init MLP solver */
180 mlp = GAS_mlp_init (cfg, stats, quotas, quotas_out, quotas_in,
181 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL);
190 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].hashPubKey))
192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
198 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].hashPubKey))
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
205 /* Create address 3 */
206 address[2] = create_address (&p[1], "test_plugin2", "test_addr2", strlen("test_addr2")+1, 0);
207 if (NULL == address[2])
213 GNUNET_CONTAINER_multihashmap_put (addresses, &p[1].hashPubKey, address[2],
214 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
217 /* Adding address 1*/
218 GAS_mlp_address_add (mlp, addresses, address[2]);
221 /* Create address 0 */
222 address[0] = create_address (&p[0], "test_plugin0", "test_addr0", strlen("test_addr0")+1, 0);
223 if (NULL == address[0])
229 GNUNET_CONTAINER_multihashmap_put (addresses, &p[0].hashPubKey, address[0],
230 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
232 /* Adding address 0 */
233 GAS_mlp_address_add (mlp, addresses, address[0]);
235 /* Updating address 0*/
236 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
237 ats.value = htonl (GNUNET_ATS_NET_WAN);
238 GAS_mlp_address_update (mlp, addresses, address[0], 1, GNUNET_NO, &ats, 1);
240 /* Retrieving preferred address for peer and wait for callback */
241 GAS_mlp_get_preferred_address (mlp, addresses, &p[0]);
243 /* Create address 1 */
244 address[1] = create_address (&p[0], "test_plugin1", "test_addr1", strlen("test_addr1")+1, 0);
245 if (NULL == address[1])
251 GNUNET_CONTAINER_multihashmap_put (addresses, &p[0].hashPubKey, address[1],
252 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
255 /* Adding address 1*/
256 GAS_mlp_address_add (mlp, addresses, address[1]);
258 /* Updating address 1*/
259 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
260 ats.value = htonl (GNUNET_ATS_NET_WAN);
261 GAS_mlp_address_update (mlp, addresses, address[1], 1, GNUNET_NO, &ats, 1);
263 GAS_mlp_address_delete (mlp, addresses, address[0], GNUNET_NO);
266 //struct GAS_MLP_SolutionContext ctx;
267 //GAS_mlp_solve_problem (mlp, &ctx);
272 main (int argc, char *argv[])
275 static char *const argv2[] = { "test_ats_mlp",
282 static struct GNUNET_GETOPT_CommandLineOption options[] = {
283 GNUNET_GETOPT_OPTION_END
286 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
287 "test_ats_mlp", "nohelp", options,
294 /* end of file test_ats_api_bandwidth_consumption.c */