mod 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
130 }
131
132 static void
133 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135         end_now (1);
136 }
137
138 static void
139 check (void *cls, char *const *args, const char *cfgfile,
140        const struct GNUNET_CONFIGURATION_Handle *cfg)
141 {
142   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
143   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
144   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
145
146 #if !HAVE_LIBGLPK
147   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
148   ret = 1;
149   return;
150 #endif
151
152   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
153
154   stats = GNUNET_STATISTICS_create("ats", cfg);
155   if (NULL == stats)
156   {
157         GNUNET_break (0);
158     end_now (1);
159     return;
160   }
161
162
163   if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
164                         GNUNET_ATS_NetworkTypeCount))
165   {
166         GNUNET_break (0);
167       end_now (1);
168       return;
169   }
170
171   addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
172   mlp  = GAS_mlp_init (cfg, stats, quotas, quotas_out, quotas_in,
173                 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL);
174   if (NULL == mlp)
175   {
176         GNUNET_break (0);
177       end_now (1);
178       return;
179   }
180
181   /* Create peer */
182   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.hashPubKey))
183   {
184       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
185       end_now (1);
186       return;
187   }
188
189   /* Create address */
190   address = create_address (&p, "test_plugin", "test_addr", strlen("test_addr")+1, 0);
191   if (NULL == address)
192   {
193         GNUNET_break (0);
194       end_now (1);
195       return;
196   }
197   GNUNET_CONTAINER_multihashmap_put (addresses, &p.hashPubKey, address,
198                 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
199
200   GAS_mlp_address_add (mlp, addresses, address);
201   end_now (0);
202
203
204 #if 0
205   struct ATS_Address addr[10];
206   struct ATS_Address *res[10];
207   struct GAS_MLP_SolutionContext ctx;
208   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
209   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
210   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
211   int quota_count;
212
213
214
215
216
217   quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
218   mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
219   mlp->auto_solve = GNUNET_NO;
220
221   struct GNUNET_PeerIdentity p[10];
222
223   /* Creating peer 1 */
224   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
225   /* Creating peer 2 */
226   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[1].hashPubKey);
227
228   /* Creating peer 1 address 1 */
229   addr[0].peer.hashPubKey = p[0].hashPubKey;
230   struct GNUNET_ATS_Information a1_ats[3];
231   set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
232   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
233   set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
234   create_address (&addr[0], "dummy", 3, &a1_ats[0]);
235   addr[0].atsp_network_type = GNUNET_ATS_NET_WAN;
236
237   /* Creating peer 1  address 2 */
238   addr[1].peer.hashPubKey = p[0].hashPubKey;
239   struct GNUNET_ATS_Information a2_ats[3];
240   set_ats (&a2_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
241   set_ats (&a2_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
242   set_ats (&a2_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
243   create_address (&addr[1], "dummy2", 3, &a2_ats[0]);
244   addr[1].atsp_network_type = GNUNET_ATS_NET_LAN;
245
246   /* Creating peer 2  address 1 */
247   addr[2].peer.hashPubKey = p[1].hashPubKey;
248   struct GNUNET_ATS_Information a3_ats[3];
249   set_ats (&a3_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
250   set_ats (&a3_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
251   set_ats (&a3_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
252   create_address (&addr[2], "dummy3", 3, &a3_ats[0]);
253   addr[2].atsp_network_type = GNUNET_ATS_NET_LAN;
254
255   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
256
257   /* Add peer 1 address 1 */
258   GAS_mlp_address_update (mlp, addresses, &addr[0]);
259
260   GNUNET_assert (mlp != NULL);
261   GNUNET_assert (mlp->addr_in_problem == 1);
262
263   /* Update an peer 1 address 1  */
264   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
265   GAS_mlp_address_update (mlp, addresses, &addr[0]);
266   GNUNET_assert (mlp->addr_in_problem == 1);
267
268   /* Add peer 1 address 2 */
269   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[1], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
270   GAS_mlp_address_update (mlp, addresses, &addr[1]);
271   GNUNET_assert (mlp->addr_in_problem == 2);
272
273   /* Add peer 2 address 1 */
274   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[2].peer.hashPubKey, &addr[2], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
275   GAS_mlp_address_update (mlp, addresses, &addr[2]);
276   GNUNET_assert (mlp->addr_in_problem == 3);
277
278   GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
279   GNUNET_assert (GNUNET_OK == ctx.lp_result);
280   GNUNET_assert (GNUNET_OK == ctx.mlp_result);
281
282   res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
283   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[0]->plugin, res[0]->assigned_bw_out);
284   res[1] = GAS_mlp_get_preferred_address(mlp, addresses, &p[1]);
285   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[1]->plugin, res[1]->assigned_bw_out);
286
287   /* Delete an address */
288   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
289   GAS_mlp_address_delete (mlp, addresses, &addr[0]);
290   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[1].peer.hashPubKey, &addr[1]);
291   GAS_mlp_address_delete (mlp, addresses, &addr[1]);
292   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[2].peer.hashPubKey, &addr[2]);
293   GAS_mlp_address_delete (mlp, addresses, &addr[2]);
294
295   GNUNET_assert (mlp->addr_in_problem == 0);
296
297   GNUNET_free (addr[0].plugin);
298   GNUNET_free (addr[1].plugin);
299 #endif
300
301   ret = 0;
302   return;
303 }
304
305
306 int
307 main (int argc, char *argv[])
308 {
309
310   static char *const argv2[] = { "test_ats_mlp",
311     "-c",
312     "test_ats_mlp.conf",
313     "-L", "WARNING",
314     NULL
315   };
316
317   static struct GNUNET_GETOPT_CommandLineOption options[] = {
318     GNUNET_GETOPT_OPTION_END
319   };
320
321   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
322                       "test_ats_mlp", "nohelp", options,
323                       &check, NULL);
324
325
326   return ret;
327 }
328
329 /* end of file test_ats_api_bandwidth_consumption.c */