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->solver_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)
65 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
67 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
68 char * entry_in = NULL;
69 char * entry_out = NULL;
74 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
79 case GNUNET_ATS_NET_UNSPECIFIED:
80 entry_out = "UNSPECIFIED_QUOTA_OUT";
81 entry_in = "UNSPECIFIED_QUOTA_IN";
83 case GNUNET_ATS_NET_LOOPBACK:
84 entry_out = "LOOPBACK_QUOTA_OUT";
85 entry_in = "LOOPBACK_QUOTA_IN";
87 case GNUNET_ATS_NET_LAN:
88 entry_out = "LAN_QUOTA_OUT";
89 entry_in = "LAN_QUOTA_IN";
91 case GNUNET_ATS_NET_WAN:
92 entry_out = "WAN_QUOTA_OUT";
93 entry_in = "WAN_QUOTA_IN";
95 case GNUNET_ATS_NET_WLAN:
96 entry_out = "WLAN_QUOTA_OUT";
97 entry_in = "WLAN_QUOTA_IN";
103 if ((entry_in == NULL) || (entry_out == NULL))
107 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
109 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
110 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
111 out_dest[c] = UINT32_MAX;
113 GNUNET_free (quota_out_str);
114 quota_out_str = NULL;
116 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
117 out_dest[c] = UINT32_MAX;
119 out_dest[c] = UINT32_MAX;
122 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
124 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
125 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
126 in_dest[c] = UINT32_MAX;
128 GNUNET_free (quota_in_str);
131 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
133 in_dest[c] = UINT32_MAX;
137 in_dest[c] = UINT32_MAX;
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
142 return GNUNET_ATS_NetworkTypeCount;
146 check (void *cls, char *const *args, const char *cfgfile,
147 const struct GNUNET_CONFIGURATION_Handle *cfg)
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
154 struct ATS_Address addr[10];
155 struct ATS_Address *res[10];
156 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
157 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
158 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
160 // struct MLP_information *mlpi;
161 struct GAS_MLP_SolutionContext ctx;
163 stats = GNUNET_STATISTICS_create("ats", cfg);
165 addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
167 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
168 mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
169 mlp->auto_solve = GNUNET_NO;
171 struct GNUNET_PeerIdentity p[10];
173 /* Creating peer 1 */
174 GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
176 /* Creating peer 1 address 1 */
177 addr[0].peer.hashPubKey = p[0].hashPubKey;
178 struct GNUNET_ATS_Information a1_ats[3];
179 set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
180 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 0);
181 set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
182 create_address (&addr[0], "dummy", 3, &a1_ats[0]);
183 addr[0].atsp_network_type = GNUNET_ATS_NET_LAN;
185 GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
187 /* Add peer 1 address 1 */
188 GAS_mlp_address_update (mlp, addresses, &addr[0]);
189 // mlpi = addr[0].mlp_information;
191 GNUNET_assert (mlp != NULL);
192 GNUNET_assert (mlp->addresses_in_problem == 1);
194 /* Update an peer 1 address 1 */
195 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 20);
196 GAS_mlp_address_update (mlp, addresses, &addr[0]);
197 GNUNET_assert (mlp->addresses_in_problem == 1);
200 /* Update an peer 1 address 1 */
201 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 10);
202 GAS_mlp_address_update (mlp, addresses, &addr[0]);
203 GNUNET_assert (mlp->addresses_in_problem == 1);
205 /* Update an peer 1 address 1 */
206 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 10);
207 GAS_mlp_address_update (mlp, addresses, &addr[0]);
208 GNUNET_assert (mlp->addresses_in_problem == 1);
210 /* Update an peer 1 address 1 */
211 set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 30);
212 GAS_mlp_address_update (mlp, addresses, &addr[0]);
213 GNUNET_assert (mlp->addresses_in_problem == 1);
216 GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
217 GNUNET_assert (GNUNET_OK == ctx.lp_result);
218 GNUNET_assert (GNUNET_OK == ctx.mlp_result);
220 res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
221 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
222 "Preferred address `%s' outbound bandwidth: %u Bps\n",
224 (unsigned int) ntohl (res[0]->assigned_bw_out.value__));
226 /* Delete an address */
227 GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
228 GAS_mlp_address_delete (mlp, addresses, &addr[0]);
230 GNUNET_assert (mlp->addresses_in_problem == 0);
234 GNUNET_free (addr[0].plugin);
235 GNUNET_CONTAINER_multihashmap_destroy (addresses);
236 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
243 main (int argc, char *argv[])
246 static char *const argv2[] = { "test_ats_mlp",
253 static struct GNUNET_GETOPT_CommandLineOption options[] = {
254 GNUNET_GETOPT_OPTION_END
257 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
258 "test_ats_mlp", "nohelp", options,
265 /* end of file test_ats_api_bandwidth_consumption.c */