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