-fix config, shutdown issue
[oweals/gnunet.git] / src / ats / test_ats_api_performance_list_all_addresses_active.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 struct Address
60 {
61   char *plugin;
62   size_t plugin_len;
63
64   void *addr;
65   size_t addr_len;
66
67   struct GNUNET_ATS_Information *ats;
68   int ats_count;
69
70   void *session;
71 };
72
73 struct PeerContext
74 {
75   struct GNUNET_PeerIdentity id;
76
77   struct Address *addr;
78 };
79
80 static struct PeerContext p[2];
81
82 static struct Address p0_addresses[2];
83 static struct Address p1_addresses[2];
84
85 static struct GNUNET_HELLO_Address p0_ha[2];
86 static struct GNUNET_HELLO_Address p1_ha[2];
87
88
89 static void
90 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
91
92 static void
93 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
94
95
96 static void
97 ats_perf_cb (void *cls,
98             const struct GNUNET_HELLO_Address *address,
99             int address_active,
100             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
101             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
102             const struct GNUNET_ATS_Information *ats,
103             uint32_t ats_count)
104 {
105   static int counter = 0;
106   if (NULL == address)
107   {
108     phal = 0;
109     if (0 == counter)
110     {
111       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
112       GNUNET_SCHEDULER_add_now (end, NULL);
113     }
114     else
115     {
116       GNUNET_break (0);
117       GNUNET_SCHEDULER_add_now (end_badly, NULL);
118     }
119     return;
120   }
121   else
122   {
123     if (0 != memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
124     {
125       GNUNET_break (0);
126       GNUNET_SCHEDULER_add_now (end_badly, NULL);
127     }
128   }
129   counter ++;
130 }
131
132
133 static int
134 stat_cb(void *cls, const char *subsystem,
135         const char *name, uint64_t value,
136         int is_persistent)
137 {
138
139   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
140       subsystem,name, value);
141   if (4 == value)
142   {
143     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
144         "All addresses added, connecting to performance\n");
145     if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL)))
146     {
147       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148           "Failed to connect to performance API\n");
149       GNUNET_SCHEDULER_add_now (end_badly, NULL);
150     }
151     phal = GNUNET_ATS_performance_list_addresses (perf_ats, &p[0].id, GNUNET_NO, ats_perf_cb, NULL);
152   }
153   return GNUNET_OK;
154 }
155
156 static void
157 address_suggest_cb (void *cls,
158                     const struct GNUNET_PeerIdentity *peer,
159                     const struct GNUNET_HELLO_Address *address,
160                     struct Session *session,
161                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
162                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
163 {
164   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
165   GNUNET_SCHEDULER_add_now (&end_badly, NULL);
166 }
167
168
169 static void
170 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 {
172   die_task = NULL;
173   end ( NULL, NULL);
174   ret = GNUNET_SYSERR;
175 }
176
177 static void
178 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
179 {
180   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
181   if (die_task != NULL )
182   {
183     GNUNET_SCHEDULER_cancel (die_task);
184     die_task = NULL;
185   }
186
187   if (NULL != sched_ats)
188   {
189     GNUNET_ATS_scheduling_done (sched_ats);
190     sched_ats = NULL;
191   }
192
193   if (phal != NULL )
194   {
195     GNUNET_ATS_performance_list_addresses_cancel (phal);
196     phal = NULL;
197   }
198
199   if (perf_ats != NULL )
200   {
201     GNUNET_ATS_performance_done (perf_ats);
202     perf_ats = NULL;
203   }
204
205   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
206   if (NULL != stats)
207   {
208     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
209     stats = NULL;
210   }
211   GNUNET_free_non_null(p0_addresses[0].addr);
212   GNUNET_free_non_null(p0_addresses[1].addr);
213   GNUNET_free_non_null(p1_addresses[0].addr);
214   GNUNET_free_non_null(p1_addresses[1].addr);
215
216   ret = 0;
217 }
218
219
220
221 static void
222 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
223     struct GNUNET_TESTING_Peer *peer)
224 {
225   ret = 1;
226   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
227   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
228
229   stats = GNUNET_STATISTICS_create ("ats", cfg);
230   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
231
232   /* set up peer 0 */
233   memset (&p[0].id, '1', sizeof (p[0].id));
234   p0_addresses[0].plugin = "test";
235   p0_addresses[0].session = NULL;
236   p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
237   p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
238
239   p0_ha[0].address = p0_addresses[0].addr;
240   p0_ha[0].address_length = p0_addresses[0].addr_len;
241   p0_ha[0].peer = p[0].id;
242   p0_ha[0].transport_name = p0_addresses[0].plugin;
243
244   p0_addresses[1].plugin = "test";
245   p0_addresses[1].session = NULL;
246   p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
247   p0_addresses[1].addr_len = strlen (p0_addresses[1].addr) + 1;
248
249   p0_ha[1].address = p0_addresses[1].addr;
250   p0_ha[1].address_length = p0_addresses[1].addr_len;
251   p0_ha[1].peer = p[0].id;
252   p0_ha[1].transport_name = p0_addresses[1].plugin;
253
254   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
255       GNUNET_i2s (&p[0].id));
256
257   memset (&p[1].id, '2', sizeof (p[1].id));
258   p1_addresses[0].plugin = "test";
259   p1_addresses[0].session = NULL;
260   p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
261   p1_addresses[0].addr_len = strlen (p1_addresses[0].addr) + 1;
262
263   p1_ha[0].address = p1_addresses[0].addr;
264   p1_ha[0].address_length = p1_addresses[0].addr_len;
265   p1_ha[0].peer = p[1].id;
266   p1_ha[0].transport_name = p1_addresses[0].plugin;
267
268   p1_addresses[1].plugin = "test";
269   p1_addresses[1].session = NULL;
270   p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
271   p1_addresses[1].addr_len = strlen (p1_addresses[1].addr) + 1;
272
273   p1_ha[1].address = p1_addresses[1].addr;
274   p1_ha[1].address_length = p1_addresses[1].addr_len;
275   p1_ha[1].peer = p[1].id;
276   p1_ha[1].transport_name = p1_addresses[1].plugin;
277
278   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Created peer 1: `%s'\n",
279       GNUNET_i2s (&p[1].id));
280
281   /* Add addresses */
282   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL );
283   if (sched_ats == NULL )
284   {
285     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
286     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
287     return;
288   }
289
290   GNUNET_ATS_address_add (sched_ats, &p0_ha[0], NULL, NULL, 0);
291   GNUNET_ATS_address_add (sched_ats, &p0_ha[1], NULL, NULL, 0);
292
293   GNUNET_ATS_address_add (sched_ats, &p1_ha[0], NULL, NULL, 0);
294   GNUNET_ATS_address_add (sched_ats, &p1_ha[1], NULL, NULL, 0);
295 }
296
297 int
298 main (int argc, char *argv[])
299 {
300   if (0
301       != GNUNET_TESTING_peer_run ("test_ats_api_performance",
302           "test_ats_api.conf", &run, NULL ))
303     return 1;
304   return ret;
305 }
306
307 /* end of file test_ats_api_performance_list_addresses.c */