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