-fix config, shutdown issue
[oweals/gnunet.git] / src / ats / test_ats_solver_preferences.c
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010-2013 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_solver_preferences.c
22  * @brief solver test: preference client handling
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  *
26  * FIXME: This test merely calls some of the API
27  *        functions, it fails to check that
28  *        preferences actually achieve anything.
29  */
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_testbed_service.h"
33 #include "gnunet_ats_service.h"
34 #include "test_ats_api_common.h"
35
36 /**
37  * Timeout task
38  */
39 static struct GNUNET_SCHEDULER_Task * die_task;
40
41 /**
42  * Task to terminate the test
43  */
44 static struct GNUNET_SCHEDULER_Task *end_task;
45
46 /**
47  * Scheduling handle
48  */
49 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
50
51 /**
52  * Connectivity handle
53  */
54 static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
55
56 /**
57  * Performance handle
58  */
59 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
60
61 /**
62  * Return value
63  */
64 static int ret;
65
66 /**
67  * Test address
68  */
69 static struct Test_Address test_addr;
70
71 /**
72  * Test peer
73  */
74 static struct PeerContext p;
75
76 /**
77  * HELLO address
78  */
79 struct GNUNET_HELLO_Address test_hello_address;
80
81 /**
82  * Session
83  */
84 static void *test_session;
85
86 /**
87  * Test ats info
88  */
89 static struct GNUNET_ATS_Information test_ats_info[3];
90
91 /**
92  * Test ats count
93  */
94 static uint32_t test_ats_count;
95
96
97 static void
98 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done!\n");
101
102   if (perf_ats != NULL)
103   {
104     GNUNET_ATS_performance_done (perf_ats);
105     perf_ats = NULL;
106   }
107
108   if (die_task != NULL )
109   {
110     GNUNET_SCHEDULER_cancel (die_task);
111     die_task = NULL;
112   }
113
114   if (NULL != sched_ats)
115   {
116     GNUNET_ATS_scheduling_done (sched_ats);
117     sched_ats = NULL;
118   }
119   if (NULL != connect_ats)
120   {
121     GNUNET_ATS_connectivity_done (connect_ats);
122     connect_ats = NULL;
123   }
124   if (NULL != perf_ats)
125   {
126     GNUNET_ATS_performance_done (perf_ats);
127     perf_ats = NULL;
128   }
129   free_test_address (&test_addr);
130   ret = 0;
131 }
132
133
134 static void
135 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 {
137   die_task = NULL;
138   end (NULL, NULL );
139   ret = GNUNET_SYSERR;
140 }
141
142
143 static void
144 perf_info_cb (void *cls,
145               const struct GNUNET_HELLO_Address *address, int address_active,
146               struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
147               struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
148               const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
149 {
150   if (NULL == address)
151     return;
152   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
153              "ATS performance info: `%s'\n",
154              GNUNET_i2s (&address->peer));
155 }
156
157
158 static void
159 address_suggest_cb (void *cls,
160                     const struct GNUNET_PeerIdentity *peer,
161                     const struct GNUNET_HELLO_Address *address,
162                     struct Session *session,
163                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
164                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
165 {
166   int c;
167   double pref_val;
168
169   if (NULL == perf_ats)
170     return;
171   for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
172   {
173     pref_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 10);
174     GNUNET_ATS_performance_change_preference (perf_ats,
175                                               &test_hello_address.peer,
176                                               GNUNET_ATS_PREFERENCE_LATENCY, pref_val,
177                                               GNUNET_ATS_PREFERENCE_END);
178   }
179   if (NULL == end_task)
180     end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
181                                              &end, NULL);
182
183 }
184
185
186
187 static void
188 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
189     struct GNUNET_TESTING_Peer *peer)
190 {
191
192   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
193   connect_ats = GNUNET_ATS_connectivity_init (mycfg);
194
195   /* Connect to ATS scheduling */
196   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL );
197   if (sched_ats == NULL )
198   {
199     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
200         "Could not connect to ATS scheduling!\n");
201     GNUNET_SCHEDULER_add_now (&end_badly, NULL );
202     return;
203   }
204
205   perf_ats = GNUNET_ATS_performance_init (mycfg, &perf_info_cb, NULL);
206   if (perf_ats == NULL)
207   {
208     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
209                "Could not connect to ATS performance!\n");
210     GNUNET_SCHEDULER_add_now (&end_badly, NULL );
211     return;
212   }
213
214   /* Set up peer */
215   memset (&p.id, '1', sizeof(p.id));
216
217   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
218              "Created peer `%s'\n",
219              GNUNET_i2s_full (&p.id));
220
221   /* Prepare ATS Information */
222   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
223   test_ats_info[0].value = htonl (GNUNET_ATS_NET_WAN);
224   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
225   test_ats_info[1].value = htonl (1);
226   test_ats_info[2].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
227   test_ats_info[2].value = htonl (100);
228   test_ats_count = 3;
229
230   /* Adding address without session */
231   test_session = NULL;
232   create_test_address (&test_addr, "test",
233                        test_session, "test",
234                        strlen ("test") + 1);
235   test_hello_address.peer = p.id;
236   test_hello_address.transport_name = test_addr.plugin;
237   test_hello_address.address = test_addr.addr;
238   test_hello_address.address_length = test_addr.addr_len;
239
240   /* Adding address */
241   GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session,
242                           test_ats_info, test_ats_count);
243   GNUNET_ATS_connectivity_suggest (connect_ats, &test_hello_address.peer);
244 }
245
246
247 int
248 main (int argc, char *argv[])
249 {
250   char *sep;
251   char *src_filename = GNUNET_strdup (__FILE__);
252   char *test_filename = GNUNET_strdup (argv[0]);
253   char *config_file;
254   char *solver;
255
256   ret = 0;
257
258   if (NULL == (sep = (strstr (src_filename, ".c"))))
259   {
260     GNUNET_break(0);
261     return -1;
262   }
263   sep[0] = '\0';
264
265   if (NULL != (sep = strstr (test_filename, ".exe")))
266     sep[0] = '\0';
267
268   if (NULL == (solver = strstr (test_filename, src_filename)))
269   {
270     GNUNET_break(0);
271     return -1;
272   }
273   solver += strlen (src_filename) + 1;
274
275   if (0 == strcmp (solver, "proportional"))
276   {
277     config_file = "test_ats_solver_proportional.conf";
278   }
279   else if (0 == strcmp (solver, "mlp"))
280   {
281     config_file = "test_ats_solver_mlp.conf";
282   }
283   else if ((0 == strcmp (solver, "ril")))
284   {
285     config_file = "test_ats_solver_ril.conf";
286   }
287   else
288   {
289     GNUNET_break(0);
290     GNUNET_free(src_filename);
291     GNUNET_free(test_filename);
292     return 1;
293   }
294
295   GNUNET_free(src_filename);
296   GNUNET_free(test_filename);
297
298   if (0
299       != GNUNET_TESTING_peer_run ("test-ats-solver", config_file, &run, NULL ))
300     return GNUNET_SYSERR;
301
302   return ret;
303 }
304
305 /* end of file test_ats_solver_preferences.c */