-fixing silly NPEs
[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 struct GNUNET_SCHEDULER_Task * die_task;
42
43 /**
44  * Statistics handle
45  */
46 static struct GNUNET_STATISTICS_Handle *stats;
47
48 /**
49  * Scheduling handle
50  */
51 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
52
53 /**
54  * Connectivity handle
55  */
56 static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
57
58 /**
59  * Performance handle
60  */
61 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
62
63 /**
64  * Return value
65  */
66 static int ret;
67
68 /**
69  * Test address
70  */
71 static struct Test_Address test_addr;
72
73 /**
74  * Test peer
75  */
76 static struct PeerContext p;
77
78 /**
79  * HELLO address
80  */
81 struct GNUNET_HELLO_Address test_hello_address;
82
83 /**
84  * Session
85  */
86 static void *test_session;
87
88 /**
89  * Test ats info
90  */
91 static struct GNUNET_ATS_Information test_ats_info[3];
92
93 /**
94  * Test ats count
95  */
96 static uint32_t test_ats_count;
97
98
99 static int
100 stat_cb (void *cls, const char *subsystem, const char *name, uint64_t value,
101     int is_persistent);
102
103
104 static void
105 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106 {
107   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done!\n");
108
109   if (die_task != NULL )
110   {
111     GNUNET_SCHEDULER_cancel (die_task);
112     die_task = NULL;
113   }
114
115   if (NULL != sched_ats)
116   {
117     GNUNET_ATS_scheduling_done (sched_ats);
118     sched_ats = NULL;
119   }
120   if (NULL != connect_ats)
121   {
122     GNUNET_ATS_connectivity_done (connect_ats);
123     connect_ats = NULL;
124   }
125   if (NULL != perf_ats)
126   {
127     GNUNET_ATS_performance_done (perf_ats);
128     perf_ats = NULL;
129   }
130
131   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# active performance clients", &stat_cb, NULL );
132   if (NULL != stats)
133   {
134     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
135     stats = NULL;
136   }
137
138   free_test_address (&test_addr);
139
140   ret = 0;
141 }
142
143
144 static void
145 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
146 {
147   die_task = NULL;
148   end (NULL, NULL );
149   ret = GNUNET_SYSERR;
150 }
151
152
153 static void
154 perf_info_cb (void *cls,
155     const struct GNUNET_HELLO_Address *address, int address_active,
156     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
157               struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
158               const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
159 {
160   if (NULL == address)
161     return;
162
163   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "ATS performance info: `%s'\n",
164       GNUNET_i2s (&address->peer));
165 }
166
167
168 static void
169 address_suggest_cb (void *cls,
170                     const struct GNUNET_PeerIdentity *peer,
171                     const struct GNUNET_HELLO_Address *address,
172                     struct Session *session,
173                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
174                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
175 {
176   int c;
177   double pref_val;
178   if (NULL == perf_ats)
179     return;
180   for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
181   {
182     pref_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 10);
183     GNUNET_ATS_performance_change_preference (perf_ats,
184         &test_hello_address.peer, GNUNET_ATS_PREFERENCE_LATENCY, pref_val,
185         GNUNET_ATS_PREFERENCE_END);
186   }
187 }
188
189
190 static int
191 stat_cb (void *cls, const char *subsystem, const char *name, uint64_t value,
192     int is_persistent)
193 {
194   static int last_value = 0;
195   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
196       subsystem, name, value);
197
198   if ((0 == last_value) && (1 == value))
199   {
200     if (perf_ats != NULL)
201     {
202       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Disconnecting performance client\n");
203       GNUNET_ATS_performance_done(perf_ats);
204       perf_ats = NULL;
205     }
206   }
207   if ((1 == last_value) && (0 == value))
208   {
209     GNUNET_SCHEDULER_add_now (&end, NULL);
210   }
211   last_value = value;
212
213   return GNUNET_OK;
214 }
215
216
217 static void
218 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
219     struct GNUNET_TESTING_Peer *peer)
220 {
221
222   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
223   stats = GNUNET_STATISTICS_create ("ats", mycfg);
224   GNUNET_STATISTICS_watch (stats, "ats", "# active performance clients", &stat_cb, NULL );
225
226   connect_ats = GNUNET_ATS_connectivity_init (mycfg);
227
228   /* Connect to ATS scheduling */
229   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL );
230   if (sched_ats == NULL )
231   {
232     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
233         "Could not connect to ATS scheduling!\n");
234     GNUNET_SCHEDULER_add_now (&end_badly, NULL );
235     return;
236   }
237
238   perf_ats = GNUNET_ATS_performance_init (mycfg, &perf_info_cb, NULL );
239   if (perf_ats == NULL )
240   {
241     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
242         "Could not connect to ATS performance!\n");
243     GNUNET_SCHEDULER_add_now (&end_badly, NULL );
244     return;
245   }
246
247   /* Set up peer */
248   memset (&p.id, '1', sizeof(p.id));
249
250   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
251       GNUNET_i2s_full (&p.id));
252
253   /* Prepare ATS Information */
254   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
255   test_ats_info[0].value = htonl (GNUNET_ATS_NET_WAN);
256   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
257   test_ats_info[1].value = htonl (1);
258   test_ats_info[2].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
259   test_ats_info[2].value = htonl (100);
260   test_ats_count = 3;
261
262   /* Adding address without session */
263   test_session = NULL;
264   create_test_address (&test_addr, "test", test_session, "test",
265       strlen ("test") + 1);
266   test_hello_address.peer = p.id;
267   test_hello_address.transport_name = test_addr.plugin;
268   test_hello_address.address = test_addr.addr;
269   test_hello_address.address_length = test_addr.addr_len;
270
271   /* Adding address */
272   GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session,
273                           test_ats_info, test_ats_count);
274   GNUNET_ATS_connectivity_suggest (connect_ats, &test_hello_address.peer);
275 }
276
277
278 int
279 main (int argc, char *argv[])
280 {
281   char *sep;
282   char *src_filename = GNUNET_strdup (__FILE__);
283   char *test_filename = GNUNET_strdup (argv[0]);
284   char *config_file;
285   char *solver;
286
287   ret = 0;
288
289   if (NULL == (sep = (strstr (src_filename, ".c"))))
290   {
291     GNUNET_break(0);
292     return -1;
293   }
294   sep[0] = '\0';
295
296   if (NULL != (sep = strstr (test_filename, ".exe")))
297     sep[0] = '\0';
298
299   if (NULL == (solver = strstr (test_filename, src_filename)))
300   {
301     GNUNET_break(0);
302     return -1;
303   }
304   solver += strlen (src_filename) + 1;
305
306   if (0 == strcmp (solver, "proportional"))
307   {
308     config_file = "test_ats_solver_proportional.conf";
309   }
310   else if (0 == strcmp (solver, "mlp"))
311   {
312     config_file = "test_ats_solver_mlp.conf";
313   }
314   else if ((0 == strcmp (solver, "ril")))
315   {
316     config_file = "test_ats_solver_ril.conf";
317   }
318   else
319   {
320     GNUNET_break(0);
321     GNUNET_free(src_filename);
322     GNUNET_free(test_filename);
323     return 1;
324   }
325
326   GNUNET_free(src_filename);
327   GNUNET_free(test_filename);
328
329   if (0
330       != GNUNET_TESTING_peer_run ("test-ats-solver", config_file, &run, NULL ))
331     return GNUNET_SYSERR;
332
333   return ret;
334 }
335
336 /* end of file test_ats_solver_preferences.c */