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-solver_mlp.h"
32 #include "gnunet-service-ats_normalization.h"
33 #include "gnunet_ats_service.h"
34 #include "test_ats_api_common.h"
44 struct GAS_MLP_Handle *mlp;
50 struct GNUNET_STATISTICS_Handle * stats;
53 * Hashmap containing addresses
55 struct GNUNET_CONTAINER_MultiHashMap * addresses;
60 struct GNUNET_PeerIdentity p[2];
65 struct ATS_Address *address[3];
70 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
73 int addr_it (void *cls,
74 const struct GNUNET_HashCode * key,
77 struct ATS_Address *address = (struct ATS_Address *) value;
78 GAS_mlp_address_delete (mlp, address, GNUNET_NO);
79 GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
80 GNUNET_free (address);
88 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
90 GNUNET_SCHEDULER_cancel (timeout_task);
91 timeout_task = GNUNET_SCHEDULER_NO_TASK;
95 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
98 if (NULL != addresses)
100 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
101 GNUNET_CONTAINER_multihashmap_destroy (addresses);
109 GAS_normalization_stop ();
114 end_correctly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Test ending with success\n"));
121 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
124 timeout_task = GNUNET_SCHEDULER_NO_TASK;
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Test ending with timeout\n"));
131 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
133 return GAS_normalization_get_preferences (id);
137 get_property_cb (void *cls, const struct ATS_Address *address)
139 return GAS_normalization_get_properties ((struct ATS_Address *) address);
143 normalized_property_changed_cb (void *cls,
144 struct ATS_Address *peer,
153 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
155 static int cb_p0 = GNUNET_NO;
156 static int cb_p1 = GNUNET_NO;
158 unsigned long long in = ntohl(address->assigned_bw_in.value__);
159 unsigned long long out = ntohl(address->assigned_bw_out.value__);
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP suggests for peer `%s' address `%s':`%s' in %llu out %llu \n",
162 GNUNET_i2s(&address->peer),
167 if ((in > 0) && (out > 0) &&
168 (0 == memcmp(&p[0], &address->peer, sizeof (address->peer))))
171 if ((in > 0) && (out > 0) &&
172 (0 == memcmp(&p[1], &address->peer, sizeof (address->peer))))
175 if ((1 == cb_p0) && (1 == cb_p1))
176 GNUNET_SCHEDULER_add_now (&end_correctly, NULL);
177 else if ((1 < cb_p0) || (1 < cb_p1))
180 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
186 check (void *cls, char *const *args, const char *cfgfile,
187 const struct GNUNET_CONFIGURATION_Handle *cfg)
189 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
190 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
191 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
199 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
201 stats = GNUNET_STATISTICS_create("ats", cfg);
210 if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
211 GNUNET_ATS_NetworkTypeCount))
217 GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL);
218 /* Setup address hashmap */
219 addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
221 /* Init MLP solver */
222 mlp = GAS_mlp_init (cfg, stats, addresses,
223 quotas, quotas_out, quotas_in,
224 GNUNET_ATS_NetworkTypeCount,
225 &bandwidth_changed_cb, NULL,
226 &get_preferences_cb, NULL,
227 &get_property_cb, NULL);
234 mlp->mlp_auto_solve = GNUNET_NO;
237 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].hashPubKey))
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
245 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].hashPubKey))
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
252 /* Create address 0 */
253 address[0] = create_address (&p[0], "test_plugin0", "test_addr0", strlen("test_addr0")+1, 0);
254 if (NULL == address[0])
260 GNUNET_CONTAINER_multihashmap_put (addresses, &p[0].hashPubKey, address[0],
261 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
262 /* Adding address 0 */
263 GAS_mlp_address_add (mlp, address[0], GNUNET_ATS_NET_UNSPECIFIED);
265 /* Create address 1 */
266 address[1] = create_address (&p[0], "test_plugin1", "test_addr1", strlen("test_addr1")+1, 0);
267 if (NULL == address[1])
273 GNUNET_CONTAINER_multihashmap_put (addresses, &p[0].hashPubKey, address[1],
274 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
275 /* Adding address 1*/
276 GAS_mlp_address_add (mlp, address[1], GNUNET_ATS_NET_UNSPECIFIED);
279 /* Create address 3 */
280 address[2] = create_address (&p[1], "test_plugin2", "test_addr2", strlen("test_addr2")+1, 0);
281 if (NULL == address[2])
287 GNUNET_CONTAINER_multihashmap_put (addresses, &p[1].hashPubKey, address[2],
288 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
289 /* Adding address 3*/
290 GAS_mlp_address_add (mlp, address[2], GNUNET_ATS_NET_UNSPECIFIED);
293 /* Updating address 0*/
294 GAS_mlp_address_change_network(mlp, address[0], GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_WAN);
296 /* Retrieving preferred address for peer and wait for callback */
297 GAS_mlp_get_preferred_address (mlp, &p[0]);
298 GAS_mlp_get_preferred_address (mlp, &p[1]);
300 mlp->write_mip_mps = GNUNET_NO;
301 mlp->write_mip_sol = GNUNET_NO;
303 GAS_mlp_solve_problem (mlp);
308 main (int argc, char *argv[])
311 static char *const argv2[] = { "test_ats_mlp",
318 static struct GNUNET_GETOPT_CommandLineOption options[] = {
319 GNUNET_GETOPT_OPTION_END
322 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
323 "test_ats_mlp", "nohelp", options,
330 /* end of file test_ats_api_bandwidth_consumption.c */