ea1e5076a22f993d4f4ac5647d295a2c406b5a99
[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-solver_mlp.h"
32 #include "gnunet-service-ats_normalization.h"
33 #include "gnunet_ats_service.h"
34 #include "test_ats_api_common.h"
35
36 /**
37  * Return value
38  */
39 static int ret;
40
41 /**
42  * MLP solver handle
43  */
44 struct GAS_MLP_Handle *mlp;
45
46
47 /**
48  * Statistics handle
49  */
50 struct GNUNET_STATISTICS_Handle * stats;
51
52 /**
53  * Hashmap containing addresses
54  */
55 struct GNUNET_CONTAINER_MultiHashMap * addresses;
56
57 /**
58  * Peer
59  */
60 struct GNUNET_PeerIdentity p[2];
61
62 /**
63  * ATS Address
64  */
65 struct ATS_Address *address[3];
66
67 /**
68  * Timeout task
69  */
70 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
71
72
73 int addr_it (void *cls,
74              const struct GNUNET_HashCode * key,
75              void *value)
76 {
77         struct ATS_Address *address = (struct ATS_Address *) value;
78         GAS_mlp_address_delete (mlp, address, GNUNET_NO);
79         GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
80   GNUNET_free (address);
81         return GNUNET_OK;
82 }
83
84
85 static void
86 end_now (int res)
87 {
88         if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
89         {
90                         GNUNET_SCHEDULER_cancel (timeout_task);
91                         timeout_task = GNUNET_SCHEDULER_NO_TASK;
92         }
93   if (NULL != stats)
94   {
95           GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
96           stats = NULL;
97   }
98   if (NULL != addresses)
99   {
100                 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
101                 GNUNET_CONTAINER_multihashmap_destroy (addresses);
102                 addresses = NULL ;
103   }
104   if (NULL != mlp)
105   {
106                 GAS_mlp_done (mlp);
107                 mlp = NULL;
108   }
109   GAS_normalization_stop ();
110         ret = res;
111 }
112
113 static void
114 end_correctly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 {
116   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Test ending with success\n"));
117         end_now (0);
118 }
119
120 static void
121 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 {
123         GNUNET_break (0);
124         timeout_task = GNUNET_SCHEDULER_NO_TASK;
125   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Test ending with timeout\n"));
126         end_now (1);
127 }
128
129
130 static const double *
131 get_preferences_cb (void *cls, const struct GNUNET_PeerIdentity *id)
132 {
133         return GAS_normalization_get_preferences (id);
134 }
135
136 const double *
137 get_property_cb (void *cls, const struct ATS_Address *address)
138 {
139         return GAS_normalization_get_properties ((struct ATS_Address *) address);
140 }
141
142
143 static void
144 normalized_property_changed_cb (void *cls,
145                                                                                                                  const struct ATS_Address *peer,
146                                                                                                                  uint32_t type,
147                                                                                                                  double prop_rel)
148 {
149          /* TODO */
150 }
151
152
153 static void
154 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
155 {
156         static int cb_p0 = GNUNET_NO;
157         static int cb_p1 = GNUNET_NO;
158
159         unsigned long long in = ntohl(address->assigned_bw_in.value__);
160         unsigned long long out = ntohl(address->assigned_bw_out.value__);
161
162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP suggests for peer `%s' address `%s':`%s' in %llu out %llu \n",
163                 GNUNET_i2s(&address->peer),
164                 address->plugin,
165                 address->addr,
166                 in, out);
167
168   if ((in > 0) && (out > 0) &&
169                 (0 == memcmp(&p[0], &address->peer, sizeof (address->peer))))
170         cb_p0 ++;
171
172   if ((in > 0) && (out > 0) &&
173                 (0 == memcmp(&p[1], &address->peer, sizeof (address->peer))))
174         cb_p1 ++;
175
176   if ((1 == cb_p0) && (1 == cb_p1))
177                 GNUNET_SCHEDULER_add_now (&end_correctly, NULL);
178   else if ((1 < cb_p0) || (1 < cb_p1))
179   {
180                 GNUNET_break (0);
181                 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
182   }
183 }
184
185
186 static void
187 check (void *cls, char *const *args, const char *cfgfile,
188        const struct GNUNET_CONFIGURATION_Handle *cfg)
189 {
190   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
191   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
192   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
193   struct GNUNET_ATS_Information ats;
194
195 #if !HAVE_LIBGLPK
196   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
197   ret = 1;
198   return;
199 #endif
200
201   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
202
203   stats = GNUNET_STATISTICS_create("ats", cfg);
204   if (NULL == stats)
205   {
206         GNUNET_break (0);
207     end_now (1);
208     return;
209   }
210
211   /* Load quotas */
212   if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
213                         GNUNET_ATS_NetworkTypeCount))
214   {
215         GNUNET_break (0);
216       end_now (1);
217       return;
218   }
219   GAS_normalization_start (NULL, NULL, &normalized_property_changed_cb, NULL);
220   /* Setup address hashmap */
221   addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
222
223   /* Init MLP solver */
224   mlp  = GAS_mlp_init (cfg, stats, addresses,
225                 quotas, quotas_out, quotas_in,
226                 GNUNET_ATS_NetworkTypeCount,
227                 &bandwidth_changed_cb, NULL,
228                 &get_preferences_cb, NULL,
229                 &get_property_cb, NULL);
230   if (NULL == mlp)
231   {
232         GNUNET_break (0);
233       end_now (1);
234       return;
235   }
236   mlp->mlp_auto_solve = GNUNET_NO;
237
238   /* Create peer 0 */
239   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].hashPubKey))
240   {
241       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
242       end_now (1);
243       return;
244   }
245
246   /* Create peer 1 */
247   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].hashPubKey))
248   {
249       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
250       end_now (1);
251       return;
252   }
253
254   /* Create address 0 */
255   address[0] = create_address (&p[0], "test_plugin0", "test_addr0", strlen("test_addr0")+1, 0);
256   if (NULL == address[0])
257   {
258         GNUNET_break (0);
259       end_now (1);
260       return;
261   }
262   GNUNET_CONTAINER_multihashmap_put (addresses, &p[0].hashPubKey, address[0],
263                 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
264   /* Adding address 0 */
265   GAS_mlp_address_add (mlp, address[0], GNUNET_ATS_NET_UNSPECIFIED);
266
267   /* Create address 1 */
268   address[1] = create_address (&p[0], "test_plugin1", "test_addr1", strlen("test_addr1")+1, 0);
269   if (NULL == address[1])
270   {
271         GNUNET_break (0);
272       end_now (1);
273       return;
274   }
275   GNUNET_CONTAINER_multihashmap_put (addresses, &p[0].hashPubKey, address[1],
276                 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
277   /* Adding address 1*/
278   GAS_mlp_address_add (mlp, address[1], GNUNET_ATS_NET_UNSPECIFIED);
279
280
281   /* Create address 3 */
282   address[2] = create_address (&p[1], "test_plugin2", "test_addr2", strlen("test_addr2")+1, 0);
283   if (NULL == address[2])
284   {
285         GNUNET_break (0);
286       end_now (1);
287       return;
288   }
289   GNUNET_CONTAINER_multihashmap_put (addresses, &p[1].hashPubKey, address[2],
290                 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
291   /* Adding address 3*/
292   GAS_mlp_address_add (mlp, address[2], GNUNET_ATS_NET_UNSPECIFIED);
293
294
295   /* Updating address 0*/
296   ats.type =  htonl (GNUNET_ATS_NETWORK_TYPE);
297   ats.value = htonl (GNUNET_ATS_NET_WAN);
298   GAS_mlp_address_update (mlp, address[0], 1, GNUNET_NO, &ats, 1);
299
300   /* Retrieving preferred address for peer and wait for callback */
301   GAS_mlp_get_preferred_address (mlp, &p[0]);
302   GAS_mlp_get_preferred_address (mlp, &p[1]);
303
304   mlp->write_mip_mps = GNUNET_NO;
305   mlp->write_mip_sol = GNUNET_NO;
306
307   GAS_mlp_solve_problem (mlp);
308 }
309
310
311 int
312 main (int argc, char *argv[])
313 {
314
315   static char *const argv2[] = { "test_ats_mlp",
316     "-c",
317     "test_ats_mlp.conf",
318     "-L", "WARNING",
319     NULL
320   };
321
322   static struct GNUNET_GETOPT_CommandLineOption options[] = {
323     GNUNET_GETOPT_OPTION_END
324   };
325
326   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
327                       "test_ats_mlp", "nohelp", options,
328                       &check, NULL);
329
330
331   return ret;
332 }
333
334 /* end of file test_ats_api_bandwidth_consumption.c */