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"
37 #define MLP_MAX_EXEC_DURATION GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
38 #define MLP_MAX_ITERATIONS INT_MAX
43 struct GNUNET_STATISTICS_Handle * stats;
45 struct GNUNET_CONTAINER_MultiHashMap * addresses;
47 struct GAS_MLP_Handle *mlp;
51 create_address (struct ATS_Address *addr, char * plugin, int ats_count, struct GNUNET_ATS_Information *ats)
53 addr->solver_information = NULL;
56 addr->plugin = GNUNET_strdup (plugin);
57 addr->ats_count = ats_count;
62 set_ats (struct GNUNET_ATS_Information *ats, uint32_t type, uint32_t value)
70 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
72 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
73 char * entry_in = NULL;
74 char * entry_out = NULL;
79 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
84 case GNUNET_ATS_NET_UNSPECIFIED:
85 entry_out = "UNSPECIFIED_QUOTA_OUT";
86 entry_in = "UNSPECIFIED_QUOTA_IN";
88 case GNUNET_ATS_NET_LOOPBACK:
89 entry_out = "LOOPBACK_QUOTA_OUT";
90 entry_in = "LOOPBACK_QUOTA_IN";
92 case GNUNET_ATS_NET_LAN:
93 entry_out = "LAN_QUOTA_OUT";
94 entry_in = "LAN_QUOTA_IN";
96 case GNUNET_ATS_NET_WAN:
97 entry_out = "WAN_QUOTA_OUT";
98 entry_in = "WAN_QUOTA_IN";
100 case GNUNET_ATS_NET_WLAN:
101 entry_out = "WLAN_QUOTA_OUT";
102 entry_in = "WLAN_QUOTA_IN";
108 if ((entry_in == NULL) || (entry_out == NULL))
112 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
114 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
115 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
116 out_dest[c] = UINT32_MAX;
118 GNUNET_free (quota_out_str);
119 quota_out_str = NULL;
121 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
122 out_dest[c] = UINT32_MAX;
124 out_dest[c] = UINT32_MAX;
127 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
129 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
130 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
131 in_dest[c] = UINT32_MAX;
133 GNUNET_free (quota_in_str);
136 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
138 in_dest[c] = UINT32_MAX;
142 in_dest[c] = UINT32_MAX;
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
147 return GNUNET_ATS_NetworkTypeCount;
152 check (void *cls, char *const *args, const char *cfgfile,
153 const struct GNUNET_CONFIGURATION_Handle *cfg)
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
164 struct ATS_Address addr[10];
165 struct ATS_Address *res[10];
166 struct GAS_MLP_SolutionContext ctx;
167 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
168 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
169 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
172 stats = GNUNET_STATISTICS_create("ats", cfg);
174 addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
176 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
177 mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
178 mlp->auto_solve = GNUNET_NO;
180 struct GNUNET_PeerIdentity p[10];
182 /* Creating peer 1 */
183 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
184 /* Creating peer 2 */
185 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[1].hashPubKey);
187 /* Creating peer 1 address 1 */
188 addr[0].peer.hashPubKey = p[0].hashPubKey;
189 struct GNUNET_ATS_Information a1_ats[3];
190 set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
191 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
192 set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
193 create_address (&addr[0], "dummy", 3, &a1_ats[0]);
194 addr[0].atsp_network_type = GNUNET_ATS_NET_WAN;
196 /* Creating peer 1 address 2 */
197 addr[1].peer.hashPubKey = p[0].hashPubKey;
198 struct GNUNET_ATS_Information a2_ats[3];
199 set_ats (&a2_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
200 set_ats (&a2_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
201 set_ats (&a2_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
202 create_address (&addr[1], "dummy2", 3, &a2_ats[0]);
203 addr[1].atsp_network_type = GNUNET_ATS_NET_LAN;
205 /* Creating peer 2 address 1 */
206 addr[2].peer.hashPubKey = p[1].hashPubKey;
207 struct GNUNET_ATS_Information a3_ats[3];
208 set_ats (&a3_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
209 set_ats (&a3_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
210 set_ats (&a3_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
211 create_address (&addr[2], "dummy3", 3, &a3_ats[0]);
212 addr[2].atsp_network_type = GNUNET_ATS_NET_LAN;
214 GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
216 /* Add peer 1 address 1 */
217 GAS_mlp_address_update (mlp, addresses, &addr[0]);
219 GNUNET_assert (mlp != NULL);
220 GNUNET_assert (mlp->addr_in_problem == 1);
222 /* Update an peer 1 address 1 */
223 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
224 GAS_mlp_address_update (mlp, addresses, &addr[0]);
225 GNUNET_assert (mlp->addr_in_problem == 1);
227 /* Add peer 1 address 2 */
228 GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[1], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
229 GAS_mlp_address_update (mlp, addresses, &addr[1]);
230 GNUNET_assert (mlp->addr_in_problem == 2);
232 /* Add peer 2 address 1 */
233 GNUNET_CONTAINER_multihashmap_put(addresses, &addr[2].peer.hashPubKey, &addr[2], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
234 GAS_mlp_address_update (mlp, addresses, &addr[2]);
235 GNUNET_assert (mlp->addr_in_problem == 3);
237 GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
238 GNUNET_assert (GNUNET_OK == ctx.lp_result);
239 GNUNET_assert (GNUNET_OK == ctx.mlp_result);
241 res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
242 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[0]->plugin, res[0]->assigned_bw_out);
243 res[1] = GAS_mlp_get_preferred_address(mlp, addresses, &p[1]);
244 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[1]->plugin, res[1]->assigned_bw_out);
246 /* Delete an address */
247 GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
248 GAS_mlp_address_delete (mlp, addresses, &addr[0]);
249 GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[1].peer.hashPubKey, &addr[1]);
250 GAS_mlp_address_delete (mlp, addresses, &addr[1]);
251 GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[2].peer.hashPubKey, &addr[2]);
252 GAS_mlp_address_delete (mlp, addresses, &addr[2]);
254 GNUNET_assert (mlp->addr_in_problem == 0);
258 GNUNET_free (addr[0].plugin);
259 GNUNET_free (addr[1].plugin);
260 GNUNET_CONTAINER_multihashmap_destroy (addresses);
261 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
270 main (int argc, char *argv[])
273 static char *const argv2[] = { "test_ats_mlp",
280 static struct GNUNET_GETOPT_CommandLineOption options[] = {
281 GNUNET_GETOPT_OPTION_END
284 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
285 "test_ats_mlp", "nohelp", options,
292 /* end of file test_ats_api_bandwidth_consumption.c */