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