mod
[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 /**
56  * Peer
57  */
58 struct GNUNET_PeerIdentity p;
59
60 /**
61  * ATS Address
62  */
63 struct ATS_Address *address;
64
65 #if 0
66
67 #define MLP_MAX_EXEC_DURATION   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
68 #define MLP_MAX_ITERATIONS      INT_MAX
69
70 static void
71 set_ats (struct GNUNET_ATS_Information *ats, uint32_t type, uint32_t value)
72 {
73   ats->type = type;
74   ats->value = value;
75 }
76
77 #endif
78
79 int addr_it (void *cls,
80              const struct GNUNET_HashCode * key,
81              void *value)
82 {
83         GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
84         return GNUNET_OK;
85 }
86
87
88 static void
89 end_now (int res)
90 {
91   if (NULL != stats)
92   {
93           GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
94           stats = NULL;
95   }
96   if (NULL != mlp)
97   {
98                 GAS_mlp_done (mlp);
99                 mlp = NULL;
100   }
101   if (NULL != addresses)
102   {
103                 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
104                 GNUNET_CONTAINER_multihashmap_destroy (addresses);
105                 addresses = NULL ;
106   }
107   if (NULL != address)
108   {
109         GNUNET_free (address);
110         address = NULL;
111   }
112         ret = res;
113 }
114
115 static void
116 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
117 {
118
119 }
120
121 static void
122 check (void *cls, char *const *args, const char *cfgfile,
123        const struct GNUNET_CONFIGURATION_Handle *cfg)
124 {
125   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
126   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
127   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
128
129 #if !HAVE_LIBGLPK
130   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
131   ret = 1;
132   return;
133 #endif
134
135   stats = GNUNET_STATISTICS_create("ats", cfg);
136   if (NULL == stats)
137   {
138         GNUNET_break (0);
139     end_now (1);
140     return;
141   }
142
143
144   if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
145                         GNUNET_ATS_NetworkTypeCount))
146   {
147         GNUNET_break (0);
148       end_now (1);
149       return;
150   }
151
152   addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
153   mlp  = GAS_mlp_init (cfg, stats, quotas, quotas_out, quotas_in,
154                 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL);
155   if (NULL == mlp)
156   {
157         GNUNET_break (0);
158       end_now (1);
159       return;
160   }
161
162   /* Create peer */
163   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.hashPubKey))
164   {
165       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
166       end_now (1);
167       return;
168   }
169
170   /* Create address */
171   address = create_address (&p, "test_plugin", "test_addr", strlen("test_addr")+1, 0);
172   if (NULL == address)
173   {
174         GNUNET_break (0);
175       end_now (1);
176       return;
177   }
178   GNUNET_CONTAINER_multihashmap_put (addresses, &p.hashPubKey, address,
179                 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
180
181   GAS_mlp_address_add (mlp, addresses, address);
182
183
184   end_now (0);
185
186
187 #if 0
188   struct ATS_Address addr[10];
189   struct ATS_Address *res[10];
190   struct GAS_MLP_SolutionContext ctx;
191   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
192   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
193   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
194   int quota_count;
195
196
197
198
199
200   quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
201   mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
202   mlp->auto_solve = GNUNET_NO;
203
204   struct GNUNET_PeerIdentity p[10];
205
206   /* Creating peer 1 */
207   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
208   /* Creating peer 2 */
209   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[1].hashPubKey);
210
211   /* Creating peer 1 address 1 */
212   addr[0].peer.hashPubKey = p[0].hashPubKey;
213   struct GNUNET_ATS_Information a1_ats[3];
214   set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
215   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
216   set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
217   create_address (&addr[0], "dummy", 3, &a1_ats[0]);
218   addr[0].atsp_network_type = GNUNET_ATS_NET_WAN;
219
220   /* Creating peer 1  address 2 */
221   addr[1].peer.hashPubKey = p[0].hashPubKey;
222   struct GNUNET_ATS_Information a2_ats[3];
223   set_ats (&a2_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
224   set_ats (&a2_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
225   set_ats (&a2_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
226   create_address (&addr[1], "dummy2", 3, &a2_ats[0]);
227   addr[1].atsp_network_type = GNUNET_ATS_NET_LAN;
228
229   /* Creating peer 2  address 1 */
230   addr[2].peer.hashPubKey = p[1].hashPubKey;
231   struct GNUNET_ATS_Information a3_ats[3];
232   set_ats (&a3_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
233   set_ats (&a3_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
234   set_ats (&a3_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
235   create_address (&addr[2], "dummy3", 3, &a3_ats[0]);
236   addr[2].atsp_network_type = GNUNET_ATS_NET_LAN;
237
238   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
239
240   /* Add peer 1 address 1 */
241   GAS_mlp_address_update (mlp, addresses, &addr[0]);
242
243   GNUNET_assert (mlp != NULL);
244   GNUNET_assert (mlp->addr_in_problem == 1);
245
246   /* Update an peer 1 address 1  */
247   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
248   GAS_mlp_address_update (mlp, addresses, &addr[0]);
249   GNUNET_assert (mlp->addr_in_problem == 1);
250
251   /* Add peer 1 address 2 */
252   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[1], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
253   GAS_mlp_address_update (mlp, addresses, &addr[1]);
254   GNUNET_assert (mlp->addr_in_problem == 2);
255
256   /* Add peer 2 address 1 */
257   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[2].peer.hashPubKey, &addr[2], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
258   GAS_mlp_address_update (mlp, addresses, &addr[2]);
259   GNUNET_assert (mlp->addr_in_problem == 3);
260
261   GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
262   GNUNET_assert (GNUNET_OK == ctx.lp_result);
263   GNUNET_assert (GNUNET_OK == ctx.mlp_result);
264
265   res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
266   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[0]->plugin, res[0]->assigned_bw_out);
267   res[1] = GAS_mlp_get_preferred_address(mlp, addresses, &p[1]);
268   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[1]->plugin, res[1]->assigned_bw_out);
269
270   /* Delete an address */
271   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
272   GAS_mlp_address_delete (mlp, addresses, &addr[0]);
273   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[1].peer.hashPubKey, &addr[1]);
274   GAS_mlp_address_delete (mlp, addresses, &addr[1]);
275   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[2].peer.hashPubKey, &addr[2]);
276   GAS_mlp_address_delete (mlp, addresses, &addr[2]);
277
278   GNUNET_assert (mlp->addr_in_problem == 0);
279
280   GNUNET_free (addr[0].plugin);
281   GNUNET_free (addr[1].plugin);
282 #endif
283
284   ret = 0;
285   return;
286 }
287
288
289 int
290 main (int argc, char *argv[])
291 {
292
293   static char *const argv2[] = { "test_ats_mlp",
294     "-c",
295     "test_ats_mlp.conf",
296     "-L", "WARNING",
297     NULL
298   };
299
300   static struct GNUNET_GETOPT_CommandLineOption options[] = {
301     GNUNET_GETOPT_OPTION_END
302   };
303
304   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
305                       "test_ats_mlp", "nohelp", options,
306                       &check, NULL);
307
308
309   return ret;
310 }
311
312 /* end of file test_ats_api_bandwidth_consumption.c */