use -Wl on -no-undefined as it is a linker option:
[oweals/gnunet.git] / src / ats / test_ats_solver_preferences.c
1 /*
2  if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL)))
3  {
4  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5  "Failed to connect to performance API\n");
6  GNUNET_SCHEDULER_add_now (end_badly, NULL);
7  }
8  This file is part of GNUnet.
9  (C) 2010-2013 Christian Grothoff (and other contributing authors)
10
11  GNUnet is free software; you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published
13  by the Free Software Foundation; either version 3, or (at your
14  option) any later version.
15
16  GNUnet is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with GNUnet; see the file COPYING.  If not, write to the
23  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  Boston, MA 02111-1307, USA.
25  */
26 /**
27  * @file ats/test_ats_solver_preferences.c
28  * @brief solver test: preference client handling
29  * @author Christian Grothoff
30  * @author Matthias Wachs
31  */
32 #include "platform.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_testbed_service.h"
35 #include "gnunet_ats_service.h"
36 #include "test_ats_api_common.h"
37
38 /**
39  * Timeout task
40  */
41 static GNUNET_SCHEDULER_TaskIdentifier die_task;
42
43 /**
44  * Statistics handle
45  */
46 struct GNUNET_STATISTICS_Handle *stats;
47
48 /**
49  * Scheduling handle
50  */
51 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
52
53 /**
54  * Scheduling handle
55  */
56 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
57
58 /**
59  * Return value
60  */
61 static int ret;
62
63 /**
64  * Test address
65  */
66 static struct Test_Address test_addr;
67
68 /**
69  * Test peer
70  */
71 static struct PeerContext p;
72
73 /**
74  * HELLO address
75  */
76 struct GNUNET_HELLO_Address test_hello_address;
77
78 /**
79  * Session
80  */
81 static void *test_session;
82
83 /**
84  * Test ats info
85  */
86 struct GNUNET_ATS_Information test_ats_info[3];
87
88 /**
89  * Test ats count
90  */
91 uint32_t test_ats_count;
92
93 static int
94 stat_cb (void *cls, const char *subsystem, const char *name, uint64_t value,
95     int is_persistent);
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 (die_task != GNUNET_SCHEDULER_NO_TASK )
103   {
104     GNUNET_SCHEDULER_cancel (die_task);
105     die_task = GNUNET_SCHEDULER_NO_TASK;
106   }
107
108   if (NULL != sched_ats)
109   {
110     GNUNET_ATS_scheduling_done (sched_ats);
111     sched_ats = NULL;
112   }
113
114   if (NULL != perf_ats)
115   {
116     GNUNET_ATS_performance_done (perf_ats);
117     perf_ats = NULL;
118   }
119
120   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# active performance clients", &stat_cb, NULL );
121   if (NULL != stats)
122   {
123     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
124     stats = NULL;
125   }
126
127   free_test_address (&test_addr);
128
129   ret = 0;
130 }
131
132 static void
133 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135   die_task = GNUNET_SCHEDULER_NO_TASK;
136   end (NULL, NULL );
137   ret = GNUNET_SYSERR;
138 }
139
140
141 static void
142 perf_info_cb (void *cls,
143     const struct GNUNET_HELLO_Address *address, int address_active,
144     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
145     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
146     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
147 {
148   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "ATS performance info: `%s'\n",
149       GNUNET_i2s (&address->peer));
150 }
151
152 static void
153 address_suggest_cb (void *cls,
154                     const struct GNUNET_PeerIdentity *peer,
155                     const struct GNUNET_HELLO_Address *address,
156                     struct Session *session,
157                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
158                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
159                     const struct GNUNET_ATS_Information *atsi,
160                     uint32_t ats_count)
161 {
162   int c;
163   double pref_val;
164   if (NULL == perf_ats)
165     return;
166   for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
167   {
168     pref_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 10);
169     GNUNET_ATS_performance_change_preference (perf_ats,
170         &test_hello_address.peer, GNUNET_ATS_PREFERENCE_LATENCY, pref_val,
171         GNUNET_ATS_PREFERENCE_END);
172   }
173 }
174
175 static int
176 stat_cb (void *cls, const char *subsystem, const char *name, uint64_t value,
177     int is_persistent)
178 {
179   static int last_value = 0;
180   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
181       subsystem, name, value);
182
183   if ((0 == last_value) && (1 == value))
184   {
185     if (perf_ats != NULL)
186     {
187       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Disconnecting performance client\n");
188       GNUNET_ATS_performance_done(perf_ats);
189       perf_ats = NULL;
190     }
191   }
192   if ((1 == last_value) && (0 == value))
193   {
194     GNUNET_SCHEDULER_add_now (&end, NULL);
195   }
196   last_value = value;
197
198   return GNUNET_OK;
199 }
200
201 static void
202 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
203     struct GNUNET_TESTING_Peer *peer)
204 {
205
206   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
207   stats = GNUNET_STATISTICS_create ("ats", mycfg);
208   GNUNET_STATISTICS_watch (stats, "ats", "# active performance clients", &stat_cb, NULL );
209
210   /* Connect to ATS scheduling */
211   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL );
212   if (sched_ats == NULL )
213   {
214     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
215         "Could not connect to ATS scheduling!\n");
216     GNUNET_SCHEDULER_add_now (&end_badly, NULL );
217     return;
218   }
219
220   perf_ats = GNUNET_ATS_performance_init (mycfg, &perf_info_cb, NULL );
221   if (perf_ats == NULL )
222   {
223     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
224         "Could not connect to ATS performance!\n");
225     GNUNET_SCHEDULER_add_now (&end_badly, NULL );
226     return;
227   }
228
229   /* Set up peer */
230   memset (&p.id, '1', sizeof(p.id));
231
232   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
233       GNUNET_i2s_full (&p.id));
234
235   /* Prepare ATS Information */
236   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
237   test_ats_info[0].value = htonl (GNUNET_ATS_NET_WAN);
238   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
239   test_ats_info[1].value = htonl (1);
240   test_ats_info[2].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
241   test_ats_info[2].value = htonl (100);
242   test_ats_count = 3;
243
244   /* Adding address without session */
245   test_session = NULL;
246   create_test_address (&test_addr, "test", test_session, "test",
247       strlen ("test") + 1);
248   test_hello_address.peer = p.id;
249   test_hello_address.transport_name = test_addr.plugin;
250   test_hello_address.address = test_addr.addr;
251   test_hello_address.address_length = test_addr.addr_len;
252
253   /* Adding address */
254   GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session,
255       test_ats_info, test_ats_count);
256   GNUNET_ATS_suggest_address(sched_ats, &test_hello_address.peer, NULL, NULL);
257 }
258
259 int
260 main (int argc, char *argv[])
261 {
262   char *sep;
263   char *src_filename = GNUNET_strdup (__FILE__);
264   char *test_filename = GNUNET_strdup (argv[0]);
265   char *config_file;
266   char *solver;
267
268   ret = 0;
269
270   if (NULL == (sep = (strstr (src_filename, ".c"))))
271   {
272     GNUNET_break(0);
273     return -1;
274   }
275   sep[0] = '\0';
276
277   if (NULL != (sep = strstr (test_filename, ".exe")))
278     sep[0] = '\0';
279
280   if (NULL == (solver = strstr (test_filename, src_filename)))
281   {
282     GNUNET_break(0);
283     return -1;
284   }
285   solver += strlen (src_filename) + 1;
286
287   if (0 == strcmp (solver, "proportional"))
288   {
289     config_file = "test_ats_solver_proportional.conf";
290   }
291   else if (0 == strcmp (solver, "mlp"))
292   {
293     config_file = "test_ats_solver_mlp.conf";
294   }
295   else if ((0 == strcmp (solver, "ril")))
296   {
297     config_file = "test_ats_solver_ril.conf";
298   }
299   else
300   {
301     GNUNET_break(0);
302     GNUNET_free(src_filename);
303     GNUNET_free(test_filename);
304     return 1;
305   }
306
307   GNUNET_free(src_filename);
308   GNUNET_free(test_filename);
309
310   if (0
311       != GNUNET_TESTING_peer_run ("test-ats-solver", config_file, &run, NULL ))
312     return GNUNET_SYSERR;
313
314   return ret;
315 }
316
317 /* end of file test_ats_solver_preferences.c */