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