added simulation
[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, const struct GNUNET_HELLO_Address *address,
154     struct Session *session, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
155     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
156     const struct GNUNET_ATS_Information *atsi, uint32_t ats_count)
157 {
158   int c;
159   double pref_val;
160   if (NULL == perf_ats)
161     return;
162   for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
163   {
164     pref_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 10);
165     GNUNET_ATS_performance_change_preference (perf_ats,
166         &test_hello_address.peer, GNUNET_ATS_PREFERENCE_LATENCY, pref_val,
167         GNUNET_ATS_PREFERENCE_END);
168   }
169 }
170
171 static int
172 stat_cb (void *cls, const char *subsystem, const char *name, uint64_t value,
173     int is_persistent)
174 {
175   static int last_value = 0;
176   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
177       subsystem, name, value);
178
179   if ((0 == last_value) && (1 == value))
180   {
181     if (perf_ats != NULL)
182     {
183       GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Disconnecting performance client\n");
184       GNUNET_ATS_performance_done(perf_ats);
185       perf_ats = NULL;
186     }
187   }
188   if ((1 == last_value) && (0 == value))
189   {
190     GNUNET_SCHEDULER_add_now (&end, NULL);
191   }
192   last_value = value;
193
194   return GNUNET_OK;
195 }
196
197 static void
198 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
199     struct GNUNET_TESTING_Peer *peer)
200 {
201
202   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
203   stats = GNUNET_STATISTICS_create ("ats", mycfg);
204   GNUNET_STATISTICS_watch (stats, "ats", "# active performance clients", &stat_cb, NULL );
205
206   /* Connect to ATS scheduling */
207   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL );
208   if (sched_ats == NULL )
209   {
210     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
211         "Could not connect to ATS scheduling!\n");
212     GNUNET_SCHEDULER_add_now (&end_badly, NULL );
213     return;
214   }
215
216   perf_ats = GNUNET_ATS_performance_init (mycfg, &perf_info_cb, NULL );
217   if (perf_ats == NULL )
218   {
219     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
220         "Could not connect to ATS performance!\n");
221     GNUNET_SCHEDULER_add_now (&end_badly, NULL );
222     return;
223   }
224
225   /* Set up peer */
226   memset (&p.id, '1', sizeof(p.id));
227
228   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
229       GNUNET_i2s_full (&p.id));
230
231   /* Prepare ATS Information */
232   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
233   test_ats_info[0].value = htonl (GNUNET_ATS_NET_WAN);
234   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
235   test_ats_info[1].value = htonl (1);
236   test_ats_info[2].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
237   test_ats_info[2].value = htonl (100);
238   test_ats_count = 3;
239
240   /* Adding address without session */
241   test_session = NULL;
242   create_test_address (&test_addr, "test", test_session, "test",
243       strlen ("test") + 1);
244   test_hello_address.peer = p.id;
245   test_hello_address.transport_name = test_addr.plugin;
246   test_hello_address.address = test_addr.addr;
247   test_hello_address.address_length = test_addr.addr_len;
248
249   /* Adding address */
250   GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session,
251       test_ats_info, test_ats_count);
252   GNUNET_ATS_suggest_address(sched_ats, &test_hello_address.peer);
253 }
254
255 int
256 main (int argc, char *argv[])
257 {
258   char *sep;
259   char *src_filename = GNUNET_strdup (__FILE__);
260   char *test_filename = GNUNET_strdup (argv[0]);
261   char *config_file;
262   char *solver;
263
264   ret = 0;
265
266   if (NULL == (sep = (strstr (src_filename, ".c"))))
267   {
268     GNUNET_break(0);
269     return -1;
270   }
271   sep[0] = '\0';
272
273   if (NULL != (sep = strstr (test_filename, ".exe")))
274     sep[0] = '\0';
275
276   if (NULL == (solver = strstr (test_filename, src_filename)))
277   {
278     GNUNET_break(0);
279     return -1;
280   }
281   solver += strlen (src_filename) + 1;
282
283   if (0 == strcmp (solver, "proportional"))
284   {
285     config_file = "test_ats_solver_proportional.conf";
286   }
287   else if (0 == strcmp (solver, "mlp"))
288   {
289     config_file = "test_ats_solver_mlp.conf";
290   }
291   else if ((0 == strcmp (solver, "ril")))
292   {
293     config_file = "test_ats_solver_ril.conf";
294   }
295   else
296   {
297     GNUNET_break(0);
298     GNUNET_free(src_filename);
299     GNUNET_free(test_filename);
300     return 1;
301   }
302
303   GNUNET_free(src_filename);
304   GNUNET_free(test_filename);
305
306   if (0
307       != GNUNET_TESTING_peer_run ("test-ats-solver", config_file, &run, NULL ))
308     return GNUNET_SYSERR;
309
310   return ret;
311 }
312
313 /* end of file test_ats_solver_preferences.c */