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