minor beautifications
[oweals/gnunet.git] / src / ats / test_ats_solver_request_and_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_request_and_delete_address.c
22  * @brief solver test:  add address, request address, delete address and wait for disconnect
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  * 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  * Session
74  */
75 static void *test_session;
76
77 /**
78  * Test ats info
79  */
80 struct GNUNET_ATS_Information test_ats_info[2];
81
82 /**
83  * Test ats count
84  */
85 static uint32_t test_ats_count;
86
87 /**
88  * The address we will delete.
89  */
90 static struct GNUNET_ATS_AddressRecord *ar;
91
92 static int address_deleted = GNUNET_NO;
93
94
95 static int
96 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
97         int is_persistent);
98
99
100 static void
101 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
104   if (die_task != NULL)
105   {
106     GNUNET_SCHEDULER_cancel (die_task);
107     die_task = NULL;
108   }
109
110   if (NULL != sched_ats)
111   {
112     GNUNET_ATS_scheduling_done (sched_ats);
113     sched_ats = NULL;
114   }
115
116   if (NULL != connect_ats)
117   {
118     GNUNET_ATS_connectivity_done (connect_ats);
119     connect_ats = NULL;
120   }
121   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
122   if (NULL != stats)
123   {
124     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
125     stats = NULL;
126   }
127   free_test_address (&test_addr);
128   ret = 0;
129 }
130
131
132 static void
133 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135   die_task = NULL;
136   end (NULL, NULL);
137   ret = GNUNET_SYSERR;
138 }
139
140
141 static void
142 address_suggest_cb (void *cls,
143                     const struct GNUNET_PeerIdentity *peer,
144                     const struct GNUNET_HELLO_Address *address,
145                     struct Session *session,
146                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
147                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
148 {
149   if (GNUNET_NO == address_deleted)
150   {
151     /* Expected address suggestion */
152     GNUNET_assert (NULL != address);
153     GNUNET_assert (NULL == session);
154     GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
155     GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
156
157     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
158         "Received sugggestion for peer `%s', deleting address\n",
159         GNUNET_i2s (&address->peer));
160     address_deleted = GNUNET_YES;
161     /* Destroying address and wait for disconnect suggestion */
162     GNUNET_ATS_address_destroy (ar);
163     ar = NULL;
164   }
165   else
166   {
167     /* Expecting disconnect */
168
169     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
170                 "Received sugggestion for peer `%s': %u %u\n",
171                 GNUNET_i2s (&address->peer),
172                 (unsigned int) ntohl (bandwidth_in.value__),
173                 (unsigned int) ntohl (bandwidth_out.value__));
174
175     if ((ntohl(bandwidth_in.value__) == 0) &&
176         (ntohl(bandwidth_out.value__) == 0))
177     {
178       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
179       GNUNET_SCHEDULER_add_now (&end, NULL);
180     }
181   }
182 }
183
184
185 static int
186 stat_cb (void *cls, const char *subsystem,
187          const char *name, uint64_t value,
188          int is_persistent)
189 {
190   static struct GNUNET_ATS_ConnectivitySuggestHandle *sh;
191
192   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
193               "ATS statistics: `%s' `%s' %llu\n",
194               subsystem,
195               name,
196               value);
197   if (NULL == sh)
198     sh = GNUNET_ATS_connectivity_suggest (connect_ats, &p.id);
199   return GNUNET_OK;
200 }
201
202
203 static void
204 run (void *cls,
205      const struct GNUNET_CONFIGURATION_Handle *mycfg,
206      struct GNUNET_TESTING_Peer *peer)
207 {
208   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
209   stats = GNUNET_STATISTICS_create ("ats", mycfg);
210   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
211
212   connect_ats = GNUNET_ATS_connectivity_init (mycfg);
213
214   /* Connect to ATS scheduling */
215   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
216   if (sched_ats == NULL)
217   {
218     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
219     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
220     return;
221   }
222
223   /* Set up peer */
224   memset (&p.id, '1', sizeof (p.id));
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
226               GNUNET_i2s_full(&p.id));
227
228   /* Prepare ATS Information */
229   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
230   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
231   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
232   test_ats_info[1].value = htonl(1);
233   test_ats_count = 2;
234
235   /* Adding address without session */
236   test_session = NULL;
237   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
238   test_hello_address.peer = p.id;
239   test_hello_address.transport_name = test_addr.plugin;
240   test_hello_address.address = test_addr.addr;
241   test_hello_address.address_length = test_addr.addr_len;
242
243   /* Adding address */
244   ar = GNUNET_ATS_address_add (sched_ats, &test_hello_address,
245                                NULL,
246                                test_ats_info, test_ats_count);
247 }
248
249
250 int
251 main (int argc, char *argv[])
252 {
253   char *sep;
254   char *src_filename = GNUNET_strdup (__FILE__);
255   char *test_filename = GNUNET_strdup (argv[0]);
256   char *config_file;
257   char *solver;
258
259   ret = 0;
260
261   if (NULL == (sep  = (strstr (src_filename,".c"))))
262   {
263     GNUNET_break (0);
264     return -1;
265   }
266   sep[0] = '\0';
267
268   if (NULL != (sep = strstr (test_filename, ".exe")))
269     sep[0] = '\0';
270
271   if (NULL == (solver = strstr (test_filename, src_filename)))
272   {
273     GNUNET_break (0);
274     return -1;
275   }
276   solver += strlen (src_filename) +1;
277
278   if (0 == strcmp(solver, "proportional"))
279   {
280     config_file = "test_ats_solver_proportional.conf";
281   }
282   else if (0 == strcmp(solver, "mlp"))
283   {
284     config_file = "test_ats_solver_mlp.conf";
285   }
286   else if ((0 == strcmp(solver, "ril")))
287   {
288     config_file = "test_ats_solver_ril.conf";
289   }
290   else
291   {
292     GNUNET_break (0);
293     GNUNET_free (src_filename);
294     GNUNET_free (test_filename);
295     return 1;
296   }
297
298   GNUNET_free (src_filename);
299   GNUNET_free (test_filename);
300
301   if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
302       config_file, &run, NULL ))
303     return GNUNET_SYSERR;
304
305   return ret;
306 }
307
308 /* end of file test_ats_solver_request_and_delete_address_proportional.c */