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