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