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