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