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