memory leak
[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[2];
59
60 /**
61  * ATS Address
62  */
63 struct ATS_Address *address[3];
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         struct ATS_Address *address = (struct ATS_Address *) value;
90         GAS_mlp_address_delete (mlp, addresses, address, GNUNET_NO);
91         GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
92   GNUNET_free (address);
93         return GNUNET_OK;
94 }
95
96
97 static void
98 end_now (int res)
99 {
100         if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
101         {
102                         GNUNET_SCHEDULER_cancel (timeout_task);
103                         timeout_task = GNUNET_SCHEDULER_NO_TASK;
104         }
105   if (NULL != stats)
106   {
107           GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
108           stats = NULL;
109   }
110   if (NULL != addresses)
111   {
112                 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
113                 GNUNET_CONTAINER_multihashmap_destroy (addresses);
114                 addresses = NULL ;
115   }
116   if (NULL != mlp)
117   {
118                 GAS_mlp_done (mlp);
119                 mlp = NULL;
120   }
121
122         ret = res;
123 }
124
125 static void
126 end_correctly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
127 {
128   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Test ending with success\n"));
129         end_now (0);
130 }
131
132 static void
133 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135         timeout_task = GNUNET_SCHEDULER_NO_TASK;
136   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Test ending with timeout\n"));
137         end_now (1);
138 }
139
140
141 static void
142 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
143 {
144         static int cb_p0 = GNUNET_NO;
145         static int cb_p1 = GNUNET_NO;
146
147         unsigned long long in = ntohl(address->assigned_bw_in.value__);
148         unsigned long long out = ntohl(address->assigned_bw_out.value__);
149
150   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP suggests for peer `%s' address `%s':`%s' in %llu out %llu \n",
151                 GNUNET_i2s(&address->peer),
152                 address->plugin,
153                 address->addr,
154                 in, out);
155
156   if ((in > 0) && (out > 0) &&
157                 (0 == memcmp(&p[0], &address->peer, sizeof (address->peer))))
158         cb_p0 ++;
159
160   if ((in > 0) && (out > 0) &&
161                 (0 == memcmp(&p[1], &address->peer, sizeof (address->peer))))
162         cb_p1 ++;
163
164   if ((1 == cb_p0) && (1 == cb_p1))
165                 GNUNET_SCHEDULER_add_now (&end_correctly, NULL);
166   else if ((1 > cb_p0) || (1 > cb_p1))
167                 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
168 }
169
170
171 static void
172 check (void *cls, char *const *args, const char *cfgfile,
173        const struct GNUNET_CONFIGURATION_Handle *cfg)
174 {
175   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
176   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
177   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
178   struct GNUNET_ATS_Information ats;
179
180 #if !HAVE_LIBGLPK
181   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
182   ret = 1;
183   return;
184 #endif
185
186   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
187
188   stats = GNUNET_STATISTICS_create("ats", cfg);
189   if (NULL == stats)
190   {
191         GNUNET_break (0);
192     end_now (1);
193     return;
194   }
195
196   /* Load quotas */
197   if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
198                         GNUNET_ATS_NetworkTypeCount))
199   {
200         GNUNET_break (0);
201       end_now (1);
202       return;
203   }
204
205   /* Setup address hashmap */
206   addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
207
208   /* Init MLP solver */
209   mlp  = GAS_mlp_init (cfg, stats, quotas, quotas_out, quotas_in,
210                 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL);
211   if (NULL == mlp)
212   {
213         GNUNET_break (0);
214       end_now (1);
215       return;
216   }
217   mlp->mlp_auto_solve = GNUNET_NO;
218
219   /* Create peer 0 */
220   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].hashPubKey))
221   {
222       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
223       end_now (1);
224       return;
225   }
226
227   /* Create peer 1 */
228   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].hashPubKey))
229   {
230       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
231       end_now (1);
232       return;
233   }
234
235   /* Create address 0 */
236   address[0] = create_address (&p[0], "test_plugin0", "test_addr0", strlen("test_addr0")+1, 0);
237   if (NULL == address[0])
238   {
239         GNUNET_break (0);
240       end_now (1);
241       return;
242   }
243   GNUNET_CONTAINER_multihashmap_put (addresses, &p[0].hashPubKey, address[0],
244                 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
245   /* Adding address 0 */
246   GAS_mlp_address_add (mlp, addresses, address[0]);
247
248   /* Create address 1 */
249   address[1] = create_address (&p[0], "test_plugin1", "test_addr1", strlen("test_addr1")+1, 0);
250   if (NULL == address[1])
251   {
252         GNUNET_break (0);
253       end_now (1);
254       return;
255   }
256   GNUNET_CONTAINER_multihashmap_put (addresses, &p[0].hashPubKey, address[1],
257                 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
258   /* Adding address 1*/
259   GAS_mlp_address_add (mlp, addresses, address[1]);
260
261
262   /* Create address 3 */
263   address[2] = create_address (&p[1], "test_plugin2", "test_addr2", strlen("test_addr2")+1, 0);
264   if (NULL == address[2])
265   {
266         GNUNET_break (0);
267       end_now (1);
268       return;
269   }
270   GNUNET_CONTAINER_multihashmap_put (addresses, &p[1].hashPubKey, address[2],
271                 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
272   /* Adding address 3*/
273   GAS_mlp_address_add (mlp, addresses, address[2]);
274
275
276   /* Updating address 0*/
277   ats.type =  htonl (GNUNET_ATS_NETWORK_TYPE);
278   ats.value = htonl (GNUNET_ATS_NET_WAN);
279   GAS_mlp_address_update (mlp, addresses, address[0], 1, GNUNET_NO, &ats, 1);
280
281   /* Retrieving preferred address for peer and wait for callback */
282   GAS_mlp_get_preferred_address (mlp, addresses, &p[0]);
283   GAS_mlp_get_preferred_address (mlp, addresses, &p[1]);
284
285   GAS_mlp_solve_problem (mlp, addresses);
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 */