modifications
[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 basic 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 #include "test_ats_api_common.h"
33
34 /**
35  * Return value
36  */
37 static int ret;
38
39 /**
40  * MLP solver handle
41  */
42 struct GAS_MLP_Handle *mlp;
43
44
45 /**
46  * Statistics handle
47  */
48 struct GNUNET_STATISTICS_Handle * stats;
49
50 /**
51  * Hashmap containing addresses
52  */
53 struct GNUNET_CONTAINER_MultiHashMap * addresses;
54
55 #if 0
56
57 #define MLP_MAX_EXEC_DURATION   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
58 #define MLP_MAX_ITERATIONS      INT_MAX
59
60
61
62
63
64
65
66
67
68
69
70 static void
71 create_address (struct ATS_Address *addr, char * plugin, int ats_count, struct GNUNET_ATS_Information *ats)
72 {
73   addr->solver_information = NULL;
74   addr->next = NULL;
75   addr->prev = NULL;
76   addr->plugin = GNUNET_strdup (plugin);
77   addr->ats_count = ats_count;
78   addr->ats = ats;
79 }
80
81 static void
82 set_ats (struct GNUNET_ATS_Information *ats, uint32_t type, uint32_t value)
83 {
84   ats->type = type;
85   ats->value = value;
86 }
87
88
89 unsigned int
90 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
91 {
92   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
93   char * entry_in = NULL;
94   char * entry_out = NULL;
95   char * quota_out_str;
96   char * quota_in_str;
97   int c;
98
99   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
100   {
101     in_dest[c] = 0;
102     out_dest[c] = 0;
103     switch (quotas[c]) {
104       case GNUNET_ATS_NET_UNSPECIFIED:
105         entry_out = "UNSPECIFIED_QUOTA_OUT";
106         entry_in = "UNSPECIFIED_QUOTA_IN";
107         break;
108       case GNUNET_ATS_NET_LOOPBACK:
109         entry_out = "LOOPBACK_QUOTA_OUT";
110         entry_in = "LOOPBACK_QUOTA_IN";
111         break;
112       case GNUNET_ATS_NET_LAN:
113         entry_out = "LAN_QUOTA_OUT";
114         entry_in = "LAN_QUOTA_IN";
115         break;
116       case GNUNET_ATS_NET_WAN:
117         entry_out = "WAN_QUOTA_OUT";
118         entry_in = "WAN_QUOTA_IN";
119         break;
120       case GNUNET_ATS_NET_WLAN:
121         entry_out = "WLAN_QUOTA_OUT";
122         entry_in = "WLAN_QUOTA_IN";
123         break;
124       default:
125         break;
126     }
127
128     if ((entry_in == NULL) || (entry_out == NULL))
129       continue;
130
131     /* quota out */
132     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
133     {
134       if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
135           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
136         out_dest[c] = UINT32_MAX;
137
138       GNUNET_free (quota_out_str);
139       quota_out_str = NULL;
140     }
141     else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
142       out_dest[c] = UINT32_MAX;
143     else
144       out_dest[c] = UINT32_MAX;
145
146     /* quota in */
147     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
148     {
149       if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
150           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
151         in_dest[c] = UINT32_MAX;
152
153       GNUNET_free (quota_in_str);
154       quota_in_str = NULL;
155     }
156     else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
157     {
158       in_dest[c] = UINT32_MAX;
159     }
160     else
161     {
162         in_dest[c] = UINT32_MAX;
163     }
164     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
165
166   }
167   return GNUNET_ATS_NetworkTypeCount;
168 }
169 #endif
170
171 int addr_it (void *cls,
172              const struct GNUNET_HashCode * key,
173              void *value)
174 {
175         GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
176         return GNUNET_OK;
177 }
178
179
180 static void
181 end_now (int res)
182 {
183   if (NULL != stats)
184   {
185           GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
186           stats = NULL;
187   }
188   if (NULL != mlp)
189   {
190                 GAS_mlp_done (mlp);
191                 mlp = NULL;
192   }
193   if (NULL != addresses)
194   {
195                 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
196                 GNUNET_CONTAINER_multihashmap_destroy (addresses);
197                 addresses = NULL ;
198   }
199
200         ret = res;
201 }
202
203 static void
204 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
205 {
206
207 }
208
209 static void
210 check (void *cls, char *const *args, const char *cfgfile,
211        const struct GNUNET_CONFIGURATION_Handle *cfg)
212 {
213   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
214   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
215   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
216
217 #if !HAVE_LIBGLPK
218   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
219   ret = 1;
220   return;
221 #endif
222
223   stats = GNUNET_STATISTICS_create("ats", cfg);
224   if (NULL == stats)
225   {
226         GNUNET_break (0);
227     end_now (1);
228     return;
229   }
230
231
232   if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
233                         GNUNET_ATS_NetworkTypeCount))
234   {
235         GNUNET_break (0);
236       end_now (1);
237       return;
238   }
239
240   addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
241
242   mlp  = GAS_mlp_init (cfg, stats, quotas, quotas_out, quotas_in,
243                 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL);
244   if (NULL == mlp)
245   {
246         GNUNET_break (0);
247       end_now (1);
248       return;
249   }
250
251
252
253   end_now (0);
254
255
256 #if 0
257   struct ATS_Address addr[10];
258   struct ATS_Address *res[10];
259   struct GAS_MLP_SolutionContext ctx;
260   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
261   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
262   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
263   int quota_count;
264
265
266
267
268
269   quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
270   mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
271   mlp->auto_solve = GNUNET_NO;
272
273   struct GNUNET_PeerIdentity p[10];
274
275   /* Creating peer 1 */
276   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
277   /* Creating peer 2 */
278   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[1].hashPubKey);
279
280   /* Creating peer 1 address 1 */
281   addr[0].peer.hashPubKey = p[0].hashPubKey;
282   struct GNUNET_ATS_Information a1_ats[3];
283   set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
284   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
285   set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
286   create_address (&addr[0], "dummy", 3, &a1_ats[0]);
287   addr[0].atsp_network_type = GNUNET_ATS_NET_WAN;
288
289   /* Creating peer 1  address 2 */
290   addr[1].peer.hashPubKey = p[0].hashPubKey;
291   struct GNUNET_ATS_Information a2_ats[3];
292   set_ats (&a2_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
293   set_ats (&a2_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
294   set_ats (&a2_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
295   create_address (&addr[1], "dummy2", 3, &a2_ats[0]);
296   addr[1].atsp_network_type = GNUNET_ATS_NET_LAN;
297
298   /* Creating peer 2  address 1 */
299   addr[2].peer.hashPubKey = p[1].hashPubKey;
300   struct GNUNET_ATS_Information a3_ats[3];
301   set_ats (&a3_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
302   set_ats (&a3_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
303   set_ats (&a3_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
304   create_address (&addr[2], "dummy3", 3, &a3_ats[0]);
305   addr[2].atsp_network_type = GNUNET_ATS_NET_LAN;
306
307   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
308
309   /* Add peer 1 address 1 */
310   GAS_mlp_address_update (mlp, addresses, &addr[0]);
311
312   GNUNET_assert (mlp != NULL);
313   GNUNET_assert (mlp->addr_in_problem == 1);
314
315   /* Update an peer 1 address 1  */
316   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
317   GAS_mlp_address_update (mlp, addresses, &addr[0]);
318   GNUNET_assert (mlp->addr_in_problem == 1);
319
320   /* Add peer 1 address 2 */
321   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[1], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
322   GAS_mlp_address_update (mlp, addresses, &addr[1]);
323   GNUNET_assert (mlp->addr_in_problem == 2);
324
325   /* Add peer 2 address 1 */
326   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[2].peer.hashPubKey, &addr[2], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
327   GAS_mlp_address_update (mlp, addresses, &addr[2]);
328   GNUNET_assert (mlp->addr_in_problem == 3);
329
330   GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
331   GNUNET_assert (GNUNET_OK == ctx.lp_result);
332   GNUNET_assert (GNUNET_OK == ctx.mlp_result);
333
334   res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
335   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[0]->plugin, res[0]->assigned_bw_out);
336   res[1] = GAS_mlp_get_preferred_address(mlp, addresses, &p[1]);
337   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[1]->plugin, res[1]->assigned_bw_out);
338
339   /* Delete an address */
340   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
341   GAS_mlp_address_delete (mlp, addresses, &addr[0]);
342   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[1].peer.hashPubKey, &addr[1]);
343   GAS_mlp_address_delete (mlp, addresses, &addr[1]);
344   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[2].peer.hashPubKey, &addr[2]);
345   GAS_mlp_address_delete (mlp, addresses, &addr[2]);
346
347   GNUNET_assert (mlp->addr_in_problem == 0);
348
349   GNUNET_free (addr[0].plugin);
350   GNUNET_free (addr[1].plugin);
351 #endif
352
353   ret = 0;
354   return;
355 }
356
357
358 int
359 main (int argc, char *argv[])
360 {
361
362   static char *const argv2[] = { "test_ats_mlp",
363     "-c",
364     "test_ats_mlp.conf",
365     "-L", "WARNING",
366     NULL
367   };
368
369   static struct GNUNET_GETOPT_CommandLineOption options[] = {
370     GNUNET_GETOPT_OPTION_END
371   };
372
373   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
374                       "test_ats_mlp", "nohelp", options,
375                       &check, NULL);
376
377
378   return ret;
379 }
380
381 /* end of file test_ats_api_bandwidth_consumption.c */