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