active test
[oweals/gnunet.git] / src / ats / test_ats_mlp.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file ats/test_ats_mlp.c
22  * @brief test for the MLP solver
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25
26  */
27 #include "platform.h"
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
33 static int ret;
34
35 #if 0
36
37 #define MLP_MAX_EXEC_DURATION   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
38 #define MLP_MAX_ITERATIONS      INT_MAX
39
40
41
42
43 struct GNUNET_STATISTICS_Handle * stats;
44
45 struct GNUNET_CONTAINER_MultiHashMap * addresses;
46
47 struct GAS_MLP_Handle *mlp;
48
49
50 static void
51 create_address (struct ATS_Address *addr, char * plugin, int ats_count, struct GNUNET_ATS_Information *ats)
52 {
53   addr->solver_information = NULL;
54   addr->next = NULL;
55   addr->prev = NULL;
56   addr->plugin = GNUNET_strdup (plugin);
57   addr->ats_count = ats_count;
58   addr->ats = ats;
59 }
60
61 static void
62 set_ats (struct GNUNET_ATS_Information *ats, uint32_t type, uint32_t value)
63 {
64   ats->type = type;
65   ats->value = value;
66 }
67
68
69 unsigned int
70 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
71 {
72   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
73   char * entry_in = NULL;
74   char * entry_out = NULL;
75   char * quota_out_str;
76   char * quota_in_str;
77   int c;
78
79   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
80   {
81     in_dest[c] = 0;
82     out_dest[c] = 0;
83     switch (quotas[c]) {
84       case GNUNET_ATS_NET_UNSPECIFIED:
85         entry_out = "UNSPECIFIED_QUOTA_OUT";
86         entry_in = "UNSPECIFIED_QUOTA_IN";
87         break;
88       case GNUNET_ATS_NET_LOOPBACK:
89         entry_out = "LOOPBACK_QUOTA_OUT";
90         entry_in = "LOOPBACK_QUOTA_IN";
91         break;
92       case GNUNET_ATS_NET_LAN:
93         entry_out = "LAN_QUOTA_OUT";
94         entry_in = "LAN_QUOTA_IN";
95         break;
96       case GNUNET_ATS_NET_WAN:
97         entry_out = "WAN_QUOTA_OUT";
98         entry_in = "WAN_QUOTA_IN";
99         break;
100       case GNUNET_ATS_NET_WLAN:
101         entry_out = "WLAN_QUOTA_OUT";
102         entry_in = "WLAN_QUOTA_IN";
103         break;
104       default:
105         break;
106     }
107
108     if ((entry_in == NULL) || (entry_out == NULL))
109       continue;
110
111     /* quota out */
112     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
113     {
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;
117
118       GNUNET_free (quota_out_str);
119       quota_out_str = NULL;
120     }
121     else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
122       out_dest[c] = UINT32_MAX;
123     else
124       out_dest[c] = UINT32_MAX;
125
126     /* quota in */
127     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
128     {
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;
132
133       GNUNET_free (quota_in_str);
134       quota_in_str = NULL;
135     }
136     else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
137     {
138       in_dest[c] = UINT32_MAX;
139     }
140     else
141     {
142         in_dest[c] = UINT32_MAX;
143     }
144     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
145
146   }
147   return GNUNET_ATS_NetworkTypeCount;
148 }
149 #endif
150
151 static void
152 check (void *cls, char *const *args, const char *cfgfile,
153        const struct GNUNET_CONFIGURATION_Handle *cfg)
154 {
155
156
157 #if !HAVE_LIBGLPK
158   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
159   ret = 1;
160   return;
161 #endif
162
163 #if 0
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];
170   int quota_count;
171
172   stats = GNUNET_STATISTICS_create("ats", cfg);
173
174   addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
175
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;
179
180   struct GNUNET_PeerIdentity p[10];
181
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);
186
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;
195
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;
204
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;
213
214   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
215
216   /* Add peer 1 address 1 */
217   GAS_mlp_address_update (mlp, addresses, &addr[0]);
218
219   GNUNET_assert (mlp != NULL);
220   GNUNET_assert (mlp->addr_in_problem == 1);
221
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);
226
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);
231
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);
236
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);
240
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);
245
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]);
253
254   GNUNET_assert (mlp->addr_in_problem == 0);
255
256   GAS_mlp_done (mlp);
257
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);
262 #endif
263
264   ret = 0;
265   return;
266 }
267
268
269 int
270 main (int argc, char *argv[])
271 {
272
273   static char *const argv2[] = { "test_ats_mlp",
274     "-c",
275     "test_ats_mlp.conf",
276     "-L", "WARNING",
277     NULL
278   };
279
280   static struct GNUNET_GETOPT_CommandLineOption options[] = {
281     GNUNET_GETOPT_OPTION_END
282   };
283
284   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
285                       "test_ats_mlp", "nohelp", options,
286                       &check, NULL);
287
288
289   return ret;
290 }
291
292 /* end of file test_ats_api_bandwidth_consumption.c */