separate ATS connectivity suggestions from ATS scheduling API
[oweals/gnunet.git] / src / ats / test_ats_solver_alternative_after_delete_address.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_alternative_after_delete_address.c
22  * @brief solver test:  add 2 addresses, request address, delete, expect alternative
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "test_ats_api_common.h"
31
32 /**
33  * Timeout task
34  */
35 static struct GNUNET_SCHEDULER_Task * die_task;
36
37 /**
38  * Statistics handle
39  */
40 static struct GNUNET_STATISTICS_Handle *stats;
41
42 /**
43  * Scheduling handle
44  */
45 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
46
47 /**
48  * Connectivity handle
49  */
50 static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
51
52 /**
53  * Return value
54  */
55 static int ret;
56
57 /**
58  * Test address
59  */
60 static struct Test_Address test_addr;
61
62 /**
63  * Alternative test address
64  */
65 static struct Test_Address alt_test_addr;
66
67 /**
68  * Test peer
69  */
70 static struct PeerContext p;
71
72 /**
73  * HELLO address
74  */
75 static struct GNUNET_HELLO_Address test_hello_address;
76
77 /**
78  * HELLO address
79  */
80 static struct GNUNET_HELLO_Address alt_test_hello_address;
81
82 /**
83  * Session
84  */
85 static void *test_session;
86
87 /**
88  * Test ats info
89  */
90 static struct GNUNET_ATS_Information test_ats_info[2];
91
92 /**
93  * Test ats count
94  */
95 static uint32_t test_ats_count;
96
97 /**
98  * Test state
99  */
100 static int addresses_added;
101
102 static int first_address_suggested;
103
104 static int first_address_deleted;
105
106 static int second_address_deleted;
107
108 static int second_address_suggested = GNUNET_YES;
109
110 static struct GNUNET_HELLO_Address *first_suggestion;
111
112 static struct GNUNET_HELLO_Address *second_suggestion;
113
114 /**
115  * 1st Address we will destroy.
116  */
117 static struct GNUNET_ATS_AddressRecord *ar;
118
119 /**
120  * 2nd Address we will destroy.
121  */
122 static struct GNUNET_ATS_AddressRecord *ar2;
123
124
125 static int
126 stat_cb (void *cls, const char *subsystem, const char *name, uint64_t value,
127          int is_persistent);
128
129
130 static void
131 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132 {
133   if (NULL != die_task)
134   {
135     GNUNET_SCHEDULER_cancel (die_task);
136     die_task = NULL;
137   }
138   if (NULL != connect_ats)
139   {
140     GNUNET_ATS_connectivity_done (connect_ats);
141     connect_ats = NULL;
142   }
143   if (NULL != sched_ats)
144   {
145     GNUNET_ATS_scheduling_done (sched_ats);
146     sched_ats = NULL;
147   }
148
149   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
150   if (NULL != stats)
151   {
152     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
153     stats = NULL;
154   }
155
156   free_test_address (&test_addr);
157   GNUNET_free_non_null (first_suggestion);
158   GNUNET_free_non_null (second_suggestion);
159   ret = 0;
160 }
161
162
163 static void
164 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 {
166
167   die_task = NULL;
168   end ( NULL, NULL);
169   ret = GNUNET_SYSERR;
170 }
171
172
173 static void
174 end_badly_now ()
175 {
176   if (NULL != die_task)
177   {
178     GNUNET_SCHEDULER_cancel (die_task);
179     die_task = NULL;
180   }
181   GNUNET_SCHEDULER_add_now (&end_badly, NULL);
182 }
183
184
185 static void
186 address_suggest_cb (void *cls,
187                     const struct GNUNET_PeerIdentity *peer,
188                     const struct GNUNET_HELLO_Address *address,
189                     struct Session *session,
190                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
191                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
192 {
193   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
194               "Received a sugggestion for peer `%s' : `%s'\n",
195     GNUNET_i2s (&address->peer), (char *) address->address);
196
197   if (GNUNET_NO == first_address_suggested)
198   {
199     if  (NULL == first_suggestion)
200     {
201       if ((NULL == address) || (NULL != session))
202       {
203         GNUNET_break (0);
204         end_badly_now ();
205         return;
206       }
207       if ((ntohl(bandwidth_in.value__) == 0) ||
208           (ntohl(bandwidth_out.value__) == 0))
209       {
210         GNUNET_break (0);
211         end_badly_now ();
212         return;
213       }
214
215       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
216                   "Received 1st sugggestion for peer `%s' : `%s'\n",
217         GNUNET_i2s (&address->peer), (char *) address->address);
218
219       first_suggestion = GNUNET_HELLO_address_copy (address);
220       first_address_suggested = GNUNET_YES;
221
222
223       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
224                   "Deleting 1st address for peer `%s' : `%s'\n",
225                   GNUNET_i2s (&address->peer),
226                   (char *) address->address);
227       if (0 == (strcmp ((char *) address->address,
228                         "test")))
229       {
230         GNUNET_assert (NULL != ar);
231         GNUNET_ATS_address_destroy (ar);
232         ar = NULL;
233       }
234       else
235       {
236         GNUNET_assert (NULL != ar2);
237         GNUNET_ATS_address_destroy (ar2);
238         ar2 = NULL;
239       }
240
241       first_address_deleted = GNUNET_YES;
242
243       return;
244     }
245   }
246   if (GNUNET_YES == first_address_deleted)
247   {
248     if (NULL == second_suggestion)
249     {
250       if ((NULL == address) || (NULL != session))
251       {
252         GNUNET_break (0);
253         end_badly_now ();
254         return;
255       }
256
257       if (0 != memcmp (address->address, first_suggestion->address,
258           (first_suggestion->address_length < address->address_length) ? first_suggestion->address_length : address->address_length))
259       {
260         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
261                     "Received 2nd sugggestion for peer `%s' : `%s'\n",
262                     GNUNET_i2s (&address->peer),
263                     (char *) address->address);
264         second_suggestion = GNUNET_HELLO_address_copy (address);
265         second_address_suggested = GNUNET_YES;
266
267         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
268                     "Deleting 2nd address for peer `%s' : `%s'\n",
269                     GNUNET_i2s (&address->peer),
270                     (char *) address->address);
271         if (NULL != ar)
272         {
273           GNUNET_assert (NULL == ar2);
274           GNUNET_ATS_address_destroy (ar);
275           ar = NULL;
276         }
277         else
278         {
279           GNUNET_assert (NULL != ar2);
280           GNUNET_ATS_address_destroy (ar2);
281           ar2 = NULL;
282         }
283         second_address_deleted = GNUNET_YES;
284         return;
285       }
286     }
287   }
288   if (GNUNET_YES == second_address_deleted)
289   {
290     /* Expecting disconnect */
291     if ((ntohl(bandwidth_in.value__) == 0) &&
292         (ntohl(bandwidth_out.value__) == 0))
293     {
294       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
295                   "ATS tells me to disconnect\n");
296       GNUNET_SCHEDULER_add_now (&end, NULL);
297       return;
298     }
299     else
300     {
301       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
302                   "Expected disconnect but received address `%s' with bandwidth \n",
303                   (char *) address->address);
304     }
305   }
306 }
307
308
309 static int
310 stat_cb(void *cls, const char *subsystem,
311         const char *name, uint64_t value,
312         int is_persistent)
313 {
314   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
315               "ATS statistics: `%s' `%s' %llu\n",
316               subsystem,
317               name,
318               value);
319   if ((GNUNET_NO == addresses_added) && (value == 2))
320   {
321     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
322                 "All addresses added, requesting....\n");
323     /* We have 2 addresses, so we can request */
324     addresses_added = GNUNET_YES;
325     GNUNET_ATS_connectivity_suggest (connect_ats, &p.id);
326   }
327   return GNUNET_OK;
328 }
329
330
331 static void
332 run (void *cls,
333      const struct GNUNET_CONFIGURATION_Handle *mycfg,
334      struct GNUNET_TESTING_Peer *peer)
335 {
336   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
337   stats = GNUNET_STATISTICS_create ("ats", mycfg);
338   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
339
340   connect_ats = GNUNET_ATS_connectivity_init (mycfg);
341
342   /* Connect to ATS scheduling */
343   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
344   if (sched_ats == NULL)
345   {
346     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
347                 "Could not connect to ATS scheduling!\n");
348     end_badly_now ();
349     return;
350   }
351
352   /* Set up peer */
353   memset (&p.id, '1', sizeof (p.id));
354   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
355               GNUNET_i2s_full(&p.id));
356
357   /* Prepare ATS Information */
358   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
359   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
360   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
361   test_ats_info[1].value = htonl(1);
362   test_ats_count = 2;
363
364   /* Adding address without session */
365   test_session = NULL;
366   create_test_address (&test_addr, "test", test_session,
367                        "test", strlen ("test") + 1);
368   test_hello_address.peer = p.id;
369   test_hello_address.transport_name = test_addr.plugin;
370   test_hello_address.address = test_addr.addr;
371   test_hello_address.address_length = test_addr.addr_len;
372
373   /* Adding alternative address without session */
374   test_session = NULL;
375   create_test_address (&alt_test_addr, "test", test_session,
376                        "alt_test", strlen ("alt_test") + 1);
377   alt_test_hello_address.peer = p.id;
378   alt_test_hello_address.transport_name = alt_test_addr.plugin;
379   alt_test_hello_address.address = alt_test_addr.addr;
380   alt_test_hello_address.address_length = alt_test_addr.addr_len;
381
382
383   /* Adding address */
384   ar = GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL,
385                                test_ats_info, test_ats_count);
386   /* Adding alternative address */
387   ar2 = GNUNET_ATS_address_add (sched_ats, &alt_test_hello_address, NULL,
388                                 test_ats_info, test_ats_count);
389 }
390
391
392 int
393 main (int argc, char *argv[])
394 {
395   char *sep;
396   char *src_filename = GNUNET_strdup (__FILE__);
397   char *test_filename = GNUNET_strdup (argv[0]);
398   const char *config_file;
399   char *solver;
400
401   ret = 0;
402
403   if (NULL == (sep  = (strstr (src_filename, ".c"))))
404   {
405     GNUNET_break (0);
406     return -1;
407   }
408   sep[0] = '\0';
409
410   if (NULL != (sep = strstr (test_filename, ".exe")))
411     sep[0] = '\0';
412
413   if (NULL == (solver = strstr (test_filename, src_filename)))
414   {
415     GNUNET_break (0);
416     return -1;
417   }
418   solver += strlen (src_filename) +1;
419
420   if (0 == strcmp(solver, "proportional"))
421   {
422     config_file = "test_ats_solver_proportional.conf";
423   }
424   else if (0 == strcmp(solver, "mlp"))
425   {
426     config_file = "test_ats_solver_mlp.conf";
427   }
428   else if ((0 == strcmp(solver, "ril")))
429   {
430     config_file = "test_ats_solver_ril.conf";
431   }
432   else
433   {
434     GNUNET_break (0);
435     GNUNET_free (src_filename);
436     GNUNET_free (test_filename);
437     return 1;
438   }
439
440   GNUNET_free (src_filename);
441   GNUNET_free (test_filename);
442
443   if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
444       config_file, &run, NULL ))
445     return GNUNET_SYSERR;
446
447   return ret;
448 }
449
450 /* end of file test_ats_solver_alternative_after_delete_address.c */