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 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"
33 #define MLP_MAX_EXEC_DURATION GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
34 #define MLP_MAX_ITERATIONS INT_MAX
39 struct GNUNET_STATISTICS_Handle * stats;
41 struct GNUNET_CONTAINER_MultiHashMap * addresses;
43 struct GAS_MLP_Handle *mlp;
47 create_address (struct ATS_Address *addr, char * plugin, int ats_count, struct GNUNET_ATS_Information *ats)
49 addr->mlp_information = NULL;
52 addr->plugin = GNUNET_strdup (plugin);
53 addr->ats_count = ats_count;
58 set_ats (struct GNUNET_ATS_Information *ats, uint32_t type, uint32_t value)
66 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
68 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
69 char * entry_in = NULL;
70 char * entry_out = NULL;
75 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
80 case GNUNET_ATS_NET_UNSPECIFIED:
81 entry_out = "UNSPECIFIED_QUOTA_OUT";
82 entry_in = "UNSPECIFIED_QUOTA_IN";
84 case GNUNET_ATS_NET_LOOPBACK:
85 entry_out = "LOOPBACK_QUOTA_OUT";
86 entry_in = "LOOPBACK_QUOTA_IN";
88 case GNUNET_ATS_NET_LAN:
89 entry_out = "LAN_QUOTA_OUT";
90 entry_in = "LAN_QUOTA_IN";
92 case GNUNET_ATS_NET_WAN:
93 entry_out = "WAN_QUOTA_OUT";
94 entry_in = "WAN_QUOTA_IN";
96 case GNUNET_ATS_NET_WLAN:
97 entry_out = "WLAN_QUOTA_OUT";
98 entry_in = "WLAN_QUOTA_IN";
104 if ((entry_in == NULL) || (entry_out == NULL))
108 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
110 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
111 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
112 out_dest[c] = UINT32_MAX;
114 GNUNET_free (quota_out_str);
115 quota_out_str = NULL;
117 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
118 out_dest[c] = UINT32_MAX;
120 out_dest[c] = UINT32_MAX;
123 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
125 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
126 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
127 in_dest[c] = UINT32_MAX;
129 GNUNET_free (quota_in_str);
132 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
134 in_dest[c] = UINT32_MAX;
138 in_dest[c] = UINT32_MAX;
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
143 return GNUNET_ATS_NetworkTypeCount;
149 check (void *cls, char *const *args, const char *cfgfile,
150 const struct GNUNET_CONFIGURATION_Handle *cfg)
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
157 struct ATS_Address addr[10];
158 struct ATS_Address *res[10];
159 struct GAS_MLP_SolutionContext ctx;
160 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
161 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
162 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
165 stats = GNUNET_STATISTICS_create("ats", cfg);
167 addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
169 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
170 mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
171 mlp->auto_solve = GNUNET_NO;
173 struct GNUNET_PeerIdentity p[10];
175 /* Creating peer 1 */
176 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
177 /* Creating peer 2 */
178 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[1].hashPubKey);
180 /* Creating peer 1 address 1 */
181 addr[0].peer.hashPubKey = p[0].hashPubKey;
182 struct GNUNET_ATS_Information a1_ats[3];
183 set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
184 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
185 set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
186 create_address (&addr[0], "dummy", 3, &a1_ats[0]);
187 addr[0].atsp_network_type = GNUNET_ATS_NET_WAN;
189 /* Creating peer 1 address 2 */
190 addr[1].peer.hashPubKey = p[0].hashPubKey;
191 struct GNUNET_ATS_Information a2_ats[3];
192 set_ats (&a2_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
193 set_ats (&a2_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
194 set_ats (&a2_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
195 create_address (&addr[1], "dummy2", 3, &a2_ats[0]);
196 addr[1].atsp_network_type = GNUNET_ATS_NET_LAN;
198 /* Creating peer 2 address 1 */
199 addr[2].peer.hashPubKey = p[1].hashPubKey;
200 struct GNUNET_ATS_Information a3_ats[3];
201 set_ats (&a3_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
202 set_ats (&a3_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
203 set_ats (&a3_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
204 create_address (&addr[2], "dummy3", 3, &a3_ats[0]);
205 addr[2].atsp_network_type = GNUNET_ATS_NET_LAN;
207 GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
209 /* Add peer 1 address 1 */
210 GAS_mlp_address_update (mlp, addresses, &addr[0]);
212 GNUNET_assert (mlp != NULL);
213 GNUNET_assert (mlp->addr_in_problem == 1);
215 /* Update an peer 1 address 1 */
216 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
217 GAS_mlp_address_update (mlp, addresses, &addr[0]);
218 GNUNET_assert (mlp->addr_in_problem == 1);
220 /* Add peer 1 address 2 */
221 GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[1], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
222 GAS_mlp_address_update (mlp, addresses, &addr[1]);
223 GNUNET_assert (mlp->addr_in_problem == 2);
225 /* Add peer 2 address 1 */
226 GNUNET_CONTAINER_multihashmap_put(addresses, &addr[2].peer.hashPubKey, &addr[2], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
227 GAS_mlp_address_update (mlp, addresses, &addr[2]);
228 GNUNET_assert (mlp->addr_in_problem == 3);
230 GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
231 GNUNET_assert (GNUNET_OK == ctx.lp_result);
232 GNUNET_assert (GNUNET_OK == ctx.mlp_result);
234 res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[0]->plugin, res[0]->assigned_bw_out);
236 res[1] = GAS_mlp_get_preferred_address(mlp, addresses, &p[1]);
237 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[1]->plugin, res[1]->assigned_bw_out);
239 /* Delete an address */
240 GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
241 GAS_mlp_address_delete (mlp, addresses, &addr[0]);
242 GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[1].peer.hashPubKey, &addr[1]);
243 GAS_mlp_address_delete (mlp, addresses, &addr[1]);
244 GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[2].peer.hashPubKey, &addr[2]);
245 GAS_mlp_address_delete (mlp, addresses, &addr[2]);
247 GNUNET_assert (mlp->addr_in_problem == 0);
251 GNUNET_free (addr[0].plugin);
252 GNUNET_free (addr[1].plugin);
253 GNUNET_CONTAINER_multihashmap_destroy (addresses);
254 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
262 main (int argc, char *argv[])
265 static char *const argv2[] = { "test_ats_mlp",
272 static struct GNUNET_GETOPT_CommandLineOption options[] = {
273 GNUNET_GETOPT_OPTION_END
276 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
277 "test_ats_mlp", "nohelp", options,
284 /* end of file test_ats_api_bandwidth_consumption.c */