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;
59
60 /**
61  * ATS Address
62  */
63 struct ATS_Address *address;
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         GNUNET_CONTAINER_multihashmap_remove (addresses, key, value);
90         return GNUNET_OK;
91 }
92
93
94 static void
95 end_now (int res)
96 {
97         if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
98         {
99                         GNUNET_SCHEDULER_cancel (timeout_task);
100                         timeout_task = GNUNET_SCHEDULER_NO_TASK;
101         }
102   if (NULL != stats)
103   {
104           GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
105           stats = NULL;
106   }
107   if (NULL != mlp)
108   {
109                 GAS_mlp_done (mlp);
110                 mlp = NULL;
111   }
112   if (NULL != addresses)
113   {
114                 GNUNET_CONTAINER_multihashmap_iterate (addresses, &addr_it, NULL);
115                 GNUNET_CONTAINER_multihashmap_destroy (addresses);
116                 addresses = NULL ;
117   }
118   if (NULL != address)
119   {
120         GNUNET_free (address);
121         address = NULL;
122   }
123         ret = res;
124 }
125
126 static void
127 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
128 {
129   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "bandwidth_changed_cb\n");
130         end_now (0);
131 }
132
133 static void
134 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136         timeout_task = GNUNET_SCHEDULER_NO_TASK;
137   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Test failed: timeout\n"));
138         end_now (1);
139 }
140
141 static void
142 check (void *cls, char *const *args, const char *cfgfile,
143        const struct GNUNET_CONFIGURATION_Handle *cfg)
144 {
145   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
146   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
147   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
148
149 #if !HAVE_LIBGLPK
150   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
151   ret = 1;
152   return;
153 #endif
154
155   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
156
157   stats = GNUNET_STATISTICS_create("ats", cfg);
158   if (NULL == stats)
159   {
160         GNUNET_break (0);
161     end_now (1);
162     return;
163   }
164
165   /* Load quotas */
166   if (GNUNET_ATS_NetworkTypeCount != load_quotas (cfg, quotas_out, quotas_in,
167                         GNUNET_ATS_NetworkTypeCount))
168   {
169         GNUNET_break (0);
170       end_now (1);
171       return;
172   }
173
174   /* Setup address hashmap */
175   addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
176
177   /* Init MLP solver */
178   mlp  = GAS_mlp_init (cfg, stats, quotas, quotas_out, quotas_in,
179                 GNUNET_ATS_NetworkTypeCount, &bandwidth_changed_cb, NULL);
180   if (NULL == mlp)
181   {
182         GNUNET_break (0);
183       end_now (1);
184       return;
185   }
186
187   /* Create peer */
188   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.hashPubKey))
189   {
190       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
191       end_now (1);
192       return;
193   }
194
195   /* Create address */
196   address = create_address (&p, "test_plugin", "test_addr", strlen("test_addr")+1, 0);
197   if (NULL == address)
198   {
199         GNUNET_break (0);
200       end_now (1);
201       return;
202   }
203   GNUNET_CONTAINER_multihashmap_put (addresses, &p.hashPubKey, address,
204                 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
205
206   /* Adding address */
207   GAS_mlp_address_add (mlp, addresses, address);
208
209   /* Retrieving preferred address for peer and wait for callback */
210   GAS_mlp_get_preferred_address (mlp, addresses, &p);
211
212   /* Shutdown */
213   GAS_mlp_done (mlp);
214   mlp = NULL;
215   end_now (0);
216 }
217
218
219 int
220 main (int argc, char *argv[])
221 {
222
223   static char *const argv2[] = { "test_ats_mlp",
224     "-c",
225     "test_ats_mlp.conf",
226     "-L", "WARNING",
227     NULL
228   };
229
230   static struct GNUNET_GETOPT_CommandLineOption options[] = {
231     GNUNET_GETOPT_OPTION_END
232   };
233
234   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
235                       "test_ats_mlp", "nohelp", options,
236                       &check, NULL);
237
238
239   return ret;
240 }
241
242 /* end of file test_ats_api_bandwidth_consumption.c */