fixing doxygen
[oweals/gnunet.git] / src / ats / test_ats_solver_convergence.c
1 /*
2  This file is part of GNUnet.
3  (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_add_address.c
22  * @brief solver test:  add address, request address and wait for suggests, write data to file
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  * @author Fabian Oehlmann
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_ats_service.h"
31 #include "test_ats_api_common.h"
32
33 /**
34  * Timeout task
35  */
36 static GNUNET_SCHEDULER_TaskIdentifier end_task;
37
38 /**
39  * Statistics handle
40  */
41 struct GNUNET_STATISTICS_Handle *stats;
42
43 /**
44  * Scheduling handle
45  */
46 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
47
48 /**
49  * Return value
50  */
51 static int ret;
52
53 /**
54  * Test address
55  */
56 static struct Test_Address test_addr;
57
58 /**
59  * Test peer
60  */
61 static struct PeerContext p;
62
63 /**
64  * HELLO address
65  */
66 struct GNUNET_HELLO_Address test_hello_address;
67
68 /**
69  * Session
70  */
71 static void *test_session;
72
73 /**
74  * Test ats info
75  */
76 struct GNUNET_ATS_Information test_ats_info[2];
77
78 /**
79  * Test ats count
80  */
81 uint32_t test_ats_count;
82
83 /**
84  * Seconds to run the test
85  */
86 static unsigned int seconds;
87
88 /**
89  * When the test starts
90  */
91 static struct GNUNET_TIME_Absolute time_start;
92
93 /**
94  * Whether to write a data file
95  */
96 static int write_data_file;
97
98 /**
99  * File name
100  */
101 static char *data_file_name;
102
103 /**
104  * Run name
105  */
106 static char *run_name;
107
108 static int
109 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
110         int is_persistent);
111
112 static void
113 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 {
115   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
116   if (end_task == GNUNET_SCHEDULER_NO_TASK)
117   {
118     GNUNET_SCHEDULER_cancel (end_task);
119     end_task = GNUNET_SCHEDULER_NO_TASK;
120   }
121
122   if (NULL != sched_ats)
123   {
124     GNUNET_ATS_scheduling_done (sched_ats);
125     sched_ats = NULL;
126   }
127
128   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
129   if (NULL != stats)
130   {
131     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
132     stats = NULL;
133   }
134
135   /* Close data file */
136   if (write_data_file)
137   {
138     GNUNET_free_non_null(data_file_name);
139   }
140
141   free_test_address (&test_addr);
142
143   ret = 0;
144 }
145
146
147 static void
148 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 {
150   end (NULL, NULL);
151   ret = GNUNET_SYSERR;
152 }
153
154 static void
155 address_suggest_cb (void *cls,
156                     const struct GNUNET_PeerIdentity *peer,
157                     const struct GNUNET_HELLO_Address *address,
158                     struct Session *session,
159                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
160                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
161                     const struct GNUNET_ATS_Information *atsi,
162                     uint32_t ats_count)
163 {
164   struct GNUNET_TIME_Relative time_delta;
165   char *data;
166   struct GNUNET_DISK_FileHandle *data_file_handle;
167
168   GNUNET_assert (NULL != address);
169   GNUNET_assert (NULL == session);
170   GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
171   GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
172
173   time_delta = GNUNET_TIME_absolute_get_difference(time_start, GNUNET_TIME_absolute_get());
174
175   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
176               "Received suggestion for peer '%s': IN %u kb/s - OUT %u kb/s\n",
177               GNUNET_i2s (&address->peer),
178               (unsigned int) ntohl (bandwidth_in.value__)/1024,
179               (unsigned int) ntohl (bandwidth_out.value__)/1024);
180
181   if (write_data_file)
182   {
183     GNUNET_asprintf(&data,"%f\tIN %u\tOUT %u\n",
184         (double) time_delta.rel_value_us / 1000000.,
185               ntohl(bandwidth_in.value__)/1024,
186               ntohl(bandwidth_out.value__)/1024);
187     data_file_handle = GNUNET_DISK_file_open (data_file_name,
188         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_APPEND,
189         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
190     if (NULL == data_file_handle)
191     {
192       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
193                   "Cannot write data to file `%s'\n",
194                   data_file_name);
195     }
196     else
197     {
198       if (GNUNET_SYSERR == GNUNET_DISK_file_write(data_file_handle, data, strlen(data)))
199         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200                     "Cannot write data to file `%s'\n",
201                     data_file_name);
202       if (GNUNET_SYSERR == GNUNET_DISK_file_close (data_file_handle))
203         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204                     "Cannot close log file '%s'\n",
205                     data_file_name);
206     }
207
208     GNUNET_free(data);
209   }
210 }
211
212
213 static int
214 stat_cb(void *cls, const char *subsystem,
215         const char *name, uint64_t value,
216         int is_persistent)
217 {
218   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
219               "ATS statistics: `%s' `%s' %llu\n",
220               subsystem,name,
221               (unsigned long long) value);
222   GNUNET_ATS_suggest_address (sched_ats, &p.id, NULL, NULL);
223   return GNUNET_OK;
224 }
225
226
227 static void
228 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
229     struct GNUNET_TESTING_Peer *peer)
230 {
231   struct GNUNET_DISK_FileHandle *data_file_handle;
232
233   stats = GNUNET_STATISTICS_create ("ats", mycfg);
234   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
235
236   /* Connect to ATS scheduling */
237   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
238   if (sched_ats == NULL)
239   {
240     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
241     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
242     return;
243   }
244
245   /* Create or truncate file */
246   if (write_data_file)
247   {
248     GNUNET_asprintf (&data_file_name, "test_convergence_%s_s%d.data", run_name, seconds);
249     data_file_handle = GNUNET_DISK_file_open (data_file_name,
250         GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
251         GNUNET_DISK_PERM_USER_EXEC | GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
252     if (NULL == data_file_handle)
253     {
254       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open data file\n");
255       GNUNET_SCHEDULER_add_now (&end_badly, NULL);
256       return;
257     }
258     if (GNUNET_SYSERR == GNUNET_DISK_file_close (data_file_handle))
259     {
260       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Cannot close log file '%s'\n",
261               data_file_name);
262       GNUNET_SCHEDULER_add_now (&end_badly, NULL);
263       return;
264     }
265   }
266
267   /* Set up peer */
268   memset (&p.id, '1', sizeof (p.id));
269   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
270               GNUNET_i2s_full(&p.id));
271
272   /* Prepare ATS Information */
273   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
274   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
275   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
276   test_ats_info[1].value = htonl(1);
277   test_ats_count = 2;
278
279   /* Adding address without session */
280   test_session = NULL;
281   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
282   test_hello_address.peer = p.id;
283   test_hello_address.transport_name = test_addr.plugin;
284   test_hello_address.address = test_addr.addr;
285   test_hello_address.address_length = test_addr.addr_len;
286
287   /* Adding address */
288   GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
289   time_start = GNUNET_TIME_absolute_get();
290
291   end_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_relative_get_second_(), seconds), &end, NULL);
292 }
293
294
295 void
296 test_run (void *cls, char *const *args,
297     const char *cfgfile,
298     const struct GNUNET_CONFIGURATION_Handle *
299     cfg)
300 {
301   char *sep;
302   char *src_filename = GNUNET_strdup (__FILE__);
303   char *test_filename = cls;
304   char *config_file = "none";
305   char *solver;
306
307   ret = 0;
308
309   if (NULL == (sep  = (strstr (src_filename,".c"))))
310   {
311     GNUNET_break (0);
312     ret = -1;
313     //return -1;
314   }
315   sep[0] = '\0';
316
317   if (NULL != (sep = strstr (test_filename, ".exe")))
318     sep[0] = '\0';
319
320   if (NULL == (solver = strstr (test_filename, src_filename)))
321   {
322     GNUNET_break (0);
323     ret = -1;
324   }
325   solver += strlen (src_filename) +1;
326
327   if (0 == strcmp(solver, "proportional"))
328   {
329     config_file = "test_ats_solver_proportional.conf";
330   }
331   else if (0 == strcmp(solver, "mlp"))
332   {
333     config_file = "test_ats_solver_mlp.conf";
334   }
335   else if ((0 == strcmp(solver, "ril")))
336   {
337     config_file = "test_ats_solver_ril.conf";
338   }
339   else
340   {
341     GNUNET_break (0);
342     GNUNET_free (src_filename);
343     ret = 1;
344   }
345
346   GNUNET_free (src_filename);
347
348   if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
349       config_file, &run, NULL ))
350     ret = GNUNET_SYSERR;
351 }
352
353 int
354 main (int argc, char *argv[])
355 {
356   seconds = 5;
357   run_name = NULL;
358
359   static struct GNUNET_GETOPT_CommandLineOption options[] = {
360       { 's', "seconds", NULL,
361           gettext_noop ("seconds to run the test"),
362           1, &GNUNET_GETOPT_set_uint, &seconds },
363       { 'd', "data-file", NULL,
364           gettext_noop ("generate data file"),
365           0, &GNUNET_GETOPT_set_one, &write_data_file},
366       { 'r', "run-name", "NAME",
367           gettext_noop ("will be part of the data file name"),
368           1, &GNUNET_GETOPT_set_string, &run_name},
369       GNUNET_GETOPT_OPTION_END
370   };
371
372   GNUNET_PROGRAM_run2 (argc, argv, argv[0], NULL, options, &test_run, argv[0], GNUNET_YES);
373
374   return ret;
375 }
376
377 /* end of file test_ats_solver_convergence.c */