-fix config, shutdown issue
[oweals/gnunet.git] / src / ats / test_ats_api_performance_list_peer_addresses.c
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010,2011 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_api_performance_list_addresses.c
22  * @brief test performance API's address listing feature
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
29 #include "ats.h"
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32
33 static struct GNUNET_SCHEDULER_Task * die_task;
34
35 /**
36  * Statistics handle
37  */
38 static struct GNUNET_STATISTICS_Handle *stats;
39
40 /**
41  * Configuration handle
42  */
43 static struct GNUNET_CONFIGURATION_Handle *cfg;
44
45 /**
46  * ATS scheduling handle
47  */
48 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
49
50 /**
51  * ATS performance handle
52  */
53 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
54
55 static struct GNUNET_ATS_AddressListHandle* phal;
56
57 static int ret;
58
59
60 struct Address
61 {
62   char *plugin;
63   size_t plugin_len;
64
65   void *addr;
66   size_t addr_len;
67
68   struct GNUNET_ATS_Information *ats;
69   int ats_count;
70
71   void *session;
72 };
73
74
75 struct PeerContext
76 {
77   struct GNUNET_PeerIdentity id;
78
79   struct Address *addr;
80 };
81
82
83 static struct PeerContext p[2];
84
85 static struct Address p0_addresses[2];
86 static struct Address p1_addresses[2];
87
88 static struct GNUNET_HELLO_Address p0_ha[2];
89 static struct GNUNET_HELLO_Address p1_ha[2];
90
91
92 static void
93 end (void *cls,
94      const struct GNUNET_SCHEDULER_TaskContext *tc);
95
96
97 static void
98 ats_perf_cb (void *cls,
99             const struct GNUNET_HELLO_Address *address,
100             int address_active,
101             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
102             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
103             const struct GNUNET_ATS_Information *ats,
104             uint32_t ats_count)
105 {
106   static int counter = 0;
107
108   if (NULL == address)
109   {
110     phal = NULL;
111     if (2 == counter)
112     {
113       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
114                   "Done\n");
115       GNUNET_SCHEDULER_add_now (&end,
116                                 NULL);
117     }
118     else
119     {
120       fprintf (stderr,
121                "Expected 2 addresses, got %d\n",
122                counter);
123       GNUNET_break (0);
124       GNUNET_SCHEDULER_shutdown ();
125     }
126     return;
127   }
128   else
129   {
130     if (0 != memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
131     {
132       GNUNET_break (0);
133       GNUNET_SCHEDULER_shutdown ();
134       return;
135     }
136   }
137   counter ++;
138 }
139
140
141 static int
142 stat_cb (void *cls, const char *subsystem,
143          const char *name, uint64_t value,
144          int is_persistent)
145 {
146
147   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
148               "ATS statistics: `%s' `%s' %llu\n",
149               subsystem,
150               name,
151               value);
152   if (4 == value)
153   {
154     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
155                 "All addresses added, connecting to performance\n");
156     if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL)))
157     {
158       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159                   "Failed to connect to performance API\n");
160       GNUNET_SCHEDULER_shutdown ();
161       return GNUNET_SYSERR;
162     }
163     phal = GNUNET_ATS_performance_list_addresses (perf_ats,
164                                                   &p[0].id,
165                                                   GNUNET_YES,
166                                                   &ats_perf_cb, NULL);
167   }
168   return GNUNET_OK;
169 }
170
171
172 static void
173 end (void *cls,
174      const struct GNUNET_SCHEDULER_TaskContext *tc)
175 {
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177               "Shutting down\n");
178   if (die_task != NULL )
179   {
180     GNUNET_SCHEDULER_cancel (die_task);
181     die_task = NULL;
182   }
183   if (NULL != sched_ats)
184   {
185     GNUNET_ATS_scheduling_done (sched_ats);
186     sched_ats = NULL;
187   }
188   if (NULL != phal)
189   {
190     GNUNET_ATS_performance_list_addresses_cancel (phal);
191     phal = NULL;
192   }
193   if (NULL != perf_ats)
194   {
195     GNUNET_ATS_performance_done (perf_ats);
196     perf_ats = NULL;
197   }
198
199   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
200   if (NULL != stats)
201   {
202     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
203     stats = NULL;
204   }
205   GNUNET_free_non_null (p0_addresses[0].addr);
206   GNUNET_free_non_null (p0_addresses[1].addr);
207   GNUNET_free_non_null (p1_addresses[0].addr);
208   GNUNET_free_non_null (p1_addresses[1].addr);
209
210   ret = 0;
211 }
212
213
214 static void
215 end_badly (void *cls,
216            const struct GNUNET_SCHEDULER_TaskContext *tc)
217 {
218   die_task = NULL;
219   end (NULL, NULL);
220   ret = GNUNET_SYSERR;
221 }
222
223
224 static void
225 address_suggest_cb (void *cls,
226                     const struct GNUNET_PeerIdentity *peer,
227                     const struct GNUNET_HELLO_Address *address,
228                     struct Session *session,
229                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
230                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
231 {
232   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233               "Did not expect suggestion callback!\n");
234   GNUNET_SCHEDULER_shutdown ();
235 }
236
237
238 static void
239 run (void *cls,
240      const struct GNUNET_CONFIGURATION_Handle *mycfg,
241      struct GNUNET_TESTING_Peer *peer)
242 {
243   ret = 1;
244   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
245   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
246
247   stats = GNUNET_STATISTICS_create ("ats", cfg);
248   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
249
250   /* set up peer 0 */
251   memset (&p[0].id, '1', sizeof (p[0].id));
252   p0_addresses[0].plugin = "test";
253   p0_addresses[0].session = NULL;
254   p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
255   p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
256
257   p0_ha[0].address = p0_addresses[0].addr;
258   p0_ha[0].address_length = p0_addresses[0].addr_len;
259   p0_ha[0].peer = p[0].id;
260   p0_ha[0].transport_name = p0_addresses[0].plugin;
261
262   p0_addresses[1].plugin = "test";
263   p0_addresses[1].session = NULL;
264   p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
265   p0_addresses[1].addr_len = strlen (p0_addresses[1].addr) + 1;
266
267   p0_ha[1].address = p0_addresses[1].addr;
268   p0_ha[1].address_length = p0_addresses[1].addr_len;
269   p0_ha[1].peer = p[0].id;
270   p0_ha[1].transport_name = p0_addresses[1].plugin;
271
272   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
273              "Created peer 0: `%s'\n",
274              GNUNET_i2s (&p[0].id));
275
276   memset (&p[1].id, '2', sizeof (p[1].id));
277   p1_addresses[0].plugin = "test";
278   p1_addresses[0].session = NULL;
279   p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
280   p1_addresses[0].addr_len = strlen (p1_addresses[0].addr) + 1;
281
282   p1_ha[0].address = p1_addresses[0].addr;
283   p1_ha[0].address_length = p1_addresses[0].addr_len;
284   p1_ha[0].peer = p[1].id;
285   p1_ha[0].transport_name = p1_addresses[0].plugin;
286
287   p1_addresses[1].plugin = "test";
288   p1_addresses[1].session = NULL;
289   p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
290   p1_addresses[1].addr_len = strlen (p1_addresses[1].addr) + 1;
291
292   p1_ha[1].address = p1_addresses[1].addr;
293   p1_ha[1].address_length = p1_addresses[1].addr_len;
294   p1_ha[1].peer = p[1].id;
295   p1_ha[1].transport_name = p1_addresses[1].plugin;
296
297   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
298              "Created peer 1: `%s'\n",
299              GNUNET_i2s (&p[1].id));
300
301   /* Add addresses */
302   sched_ats = GNUNET_ATS_scheduling_init (cfg,
303                                           &address_suggest_cb, NULL);
304   if (NULL == sched_ats)
305   {
306     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307                 "Could not setup peer!\n");
308     GNUNET_SCHEDULER_shutdown ();
309     return;
310   }
311
312   GNUNET_ATS_address_add (sched_ats, &p0_ha[0], NULL, NULL, 0);
313   GNUNET_ATS_address_add (sched_ats, &p0_ha[1], NULL, NULL, 0);
314
315   GNUNET_ATS_address_add (sched_ats, &p1_ha[0], NULL, NULL, 0);
316   GNUNET_ATS_address_add (sched_ats, &p1_ha[1], NULL, NULL, 0);
317 }
318
319
320 int
321 main (int argc, char *argv[])
322 {
323   if (0 !=
324       GNUNET_TESTING_peer_run ("test_ats_api_performance",
325                                "test_ats_api.conf",
326                                &run, NULL))
327     return 1;
328   return ret;
329 }
330
331 /* end of file test_ats_api_performance_list_addresses.c */