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