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