preliminary change
[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:
23  *                              Add an address for a peer and request it. We expect an monitor callback
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, 20)
33 #define SHUTDOWN_CORRECT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
34
35 #define ATS_COUNT 2
36
37 static GNUNET_SCHEDULER_TaskIdentifier die_task;
38 static GNUNET_SCHEDULER_TaskIdentifier stage_task;
39
40 struct GNUNET_CONFIGURATION_Handle *cfg;
41
42 static struct GNUNET_ATS_SchedulingHandle *sh;
43
44 static struct GNUNET_ATS_PerformanceHandle *ph;
45
46 static struct GNUNET_HELLO_Address addr[2];
47
48 static struct GNUNET_ATS_Information atsi[ATS_COUNT];
49
50 static int ret;
51
52 static int res_suggest_cb_p0;
53 static int res_suggest_cb_p1;
54
55 static int res_perf_cb_p0;
56 static int res_perf_cb_p1;
57
58 /**
59  * Stage 0: Init, request address and wait for peer0 suggest cb
60  * Stage 1: Got peer0 suggest cb, expect monitoring cb
61  * Stage 2: Got peer0 monitoring cb, update address and expect monitor cb
62  * Stage 3: Got 2nd peer0 monitoring cb, shutdown
63  */
64
65 static int stage;
66
67
68 static void cleanup_addresses ()
69 {
70         GNUNET_ATS_address_destroyed (sh, &addr[0], NULL);
71         GNUNET_ATS_address_destroyed (sh, &addr[1], NULL);
72 }
73
74 static void setup_addresses ()
75 {
76         memset (&addr[0].peer,'\0', sizeof (addr[0].peer));
77         addr[0].transport_name = "test0";
78         addr[0].address = "test_addr0";
79         addr[0].address_length = strlen ("test_addr0") + 1;
80         atsi[0].type = htonl(GNUNET_ATS_NETWORK_TYPE);
81         atsi[0].value = htonl(GNUNET_ATS_NET_LAN);
82
83         atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
84         atsi[1].value = htonl(100);
85
86         atsi[2].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
87         atsi[2].value = htonl(5);
88
89         GNUNET_ATS_address_add (sh, &addr[0], NULL, atsi, ATS_COUNT);
90
91         memset (&addr[1].peer,'\1', sizeof (addr[1].peer));
92         addr[1].transport_name = "test1";
93         addr[1].address = "test_addr1";
94         addr[1].address_length = strlen ("test_addr1") + 1;
95
96         GNUNET_ATS_address_add (sh, &addr[1], NULL, atsi, ATS_COUNT);
97 }
98
99
100 static void
101 end_now (int res)
102 {
103         if (GNUNET_SCHEDULER_NO_TASK != stage_task)
104         {
105                         GNUNET_SCHEDULER_cancel (stage_task);
106                         stage_task = GNUNET_SCHEDULER_NO_TASK;
107         }
108         if (GNUNET_SCHEDULER_NO_TASK != die_task)
109         {
110                         GNUNET_SCHEDULER_cancel (die_task);
111                         die_task = GNUNET_SCHEDULER_NO_TASK;
112         }
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
114
115         cleanup_addresses ();
116
117         if (NULL != ph)
118         {
119                 GNUNET_ATS_performance_done (ph);
120                 ph = NULL;
121         }
122
123         if (NULL != sh)
124         {
125                 GNUNET_ATS_scheduling_done (sh);
126                 sh = NULL;
127         }
128         ret = res;
129 }
130
131 static void
132 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {
134   die_task = GNUNET_SCHEDULER_NO_TASK;
135   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout\n");
136   end_now (1);
137 }
138
139 static void
140 next_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
141 {
142         static int stage_counter = 0;
143
144         stage_task = GNUNET_SCHEDULER_NO_TASK;
145         if (0 == stage_counter)
146         {
147                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stop performance monitoring\n");
148
149                 stage_task = GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &next_stage, NULL);
150                 stage_counter++;
151                 return;
152         }
153         if (1 == stage_counter)
154         {
155
156         }
157         else
158         {
159                         end_now (1);
160         }
161 }
162
163 static void end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 {
165   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Success\n");
166   end_now (0);
167 }
168
169
170 static void
171 perf_mon_cb (void *cls,
172                                                 const struct GNUNET_PeerIdentity *peer,
173                                                 const struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
174                                                 const struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
175                                                 const struct GNUNET_ATS_Information *ats,
176                                                 uint32_t ats_count)
177 {
178         int c1;
179         int c2;
180         int c3;
181
182         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183                         "ATS notifies about perfomance change for peer `%s'\n", GNUNET_i2s (peer));
184
185         if ((1 != stage) || (2 == stage))
186         {
187                 GNUNET_break (0);
188                 end_now(1);
189                 return;
190         }
191
192         if (1 == stage)
193         {
194                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received 1st callback for peer `%s' with %u information\n",
195                                         GNUNET_i2s (&addr[0].peer), ats_count);
196                         if ((0 != memcmp (peer, &addr[0].peer, sizeof (addr[0].peer))) ||
197                                         (ats_count < ATS_COUNT))
198                         {
199                                         GNUNET_break (0);
200                                         if (GNUNET_SCHEDULER_NO_TASK != die_task)
201                                                 GNUNET_SCHEDULER_cancel (die_task);
202                                         die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
203                                         return;
204                         }
205
206                         c3 = 0;
207                         for (c1 = 0; c1 < ats_count; c1++)
208                         {
209                                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS information [%u] %u : %u \n",
210                                                 c1, ntohl (ats[c1].type), ntohl (ats[c1].value));
211                                 for (c2 = 0; c2 < ATS_COUNT; c2++)
212                                 {
213                                         if (ats[c1].type == atsi[c2].type)
214                                         {
215                                                 if (ats[c1].value == atsi[c2].value)
216                                                                 c3++;
217                                                 else
218                                                                 GNUNET_break (0);
219                                         }
220                                 }
221                         }
222                         if (ATS_COUNT != c3)
223                         {
224                                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received only %u correct ATS information \n", c3);
225                                 if (GNUNET_SCHEDULER_NO_TASK != die_task)
226                                         GNUNET_SCHEDULER_cancel (die_task);
227                                 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
228                                 return;
229                         }
230
231                         /* Everything OK */
232                         stage ++;
233                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u correct ATS information \n", c3);
234
235                         atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
236                         atsi[1].value = htonl(1000);
237
238                         atsi[2].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
239                         atsi[2].value = htonl(50);
240
241                         GNUNET_ATS_address_update (sh, &addr[0], NULL, atsi, ATS_COUNT);
242         }
243         if (1 == stage)
244         {
245                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received 2nd callback for peer `%s' with %u information\n",
246                                         GNUNET_i2s (&addr[0].peer), ats_count);
247         }
248
249
250 #if 0
251         static int stage_counter = 0;
252
253
254
255         if (0 == stage_counter)
256         {
257
258                 c3 = 0;
259
260
261                 stage_counter ++;
262
263         }
264         else if (1 == stage_counter)
265         {
266                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received updated callback for peer `%s' with %u information\n",
267                                         GNUNET_i2s (&addr.peer), ats_count);
268
269                         if ((0 != memcmp (peer, &addr.peer, sizeof (addr.peer))) ||
270                                         (ats_count < ATS_COUNT))
271                         {
272                                         GNUNET_break (0);
273                                         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
274                                         return;
275                         }
276                         c3 = 0;
277                         for (c1 = 0; c1 < ats_count; c1++)
278                         {
279                                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS information [%u] %u : %u \n", c1, ntohl (ats[c1].type), ntohl (ats[c1].value));
280                                         for (c2 = 0; c2 < ATS_COUNT; c2++)
281                                         {
282                                                 if (ats[c1].type == atsi[c2].type)
283                                                 {
284                                                         if (ats[c1].value == atsi[c2].value)
285                                                         {
286                                                                         c3++;
287                                                         }
288                                                         else
289                                                         {
290                                                                         GNUNET_break (0);
291                                                         }
292                                                 }
293                                         }
294                         }
295
296                         if (ATS_COUNT != c3)
297                         {
298                           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received only %u correct ATS information \n", c3);
299                                 GNUNET_break (0);
300                                 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
301                                 return;
302                         }
303                         stage_counter ++;
304                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u correct ATS information, shutdown... \n", c3);
305                         GNUNET_SCHEDULER_add_now (&end, NULL);
306                         return;
307         }
308 #endif
309 }
310
311 void ats_suggest_cb (void *cls,
312                                                                                 const struct GNUNET_HELLO_Address * address,
313                                                                                 struct Session * session,
314                                                                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
315                                                                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
316                                                                                 const struct GNUNET_ATS_Information *ats,
317                                                                                 uint32_t ats_count)
318 {
319         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320                         "ATS is suggesting address for peer `%s'\n", GNUNET_i2s (&address->peer));
321
322         if (0 != stage)
323         {
324                 GNUNET_break (0);
325                 end_now(1);
326                 return;
327         }
328
329         if (0 == memcmp (&addr[0].peer, &address->peer, sizeof (address->peer)))
330         {
331                 res_suggest_cb_p0 = GNUNET_YES;
332                 stage = 1;
333         }
334         if (0 == memcmp (&addr[1].peer, &address->peer, sizeof (address->peer)))
335                 res_suggest_cb_p1 = GNUNET_YES;
336 }
337
338
339 static void
340 run (void *cls, 
341      const struct GNUNET_CONFIGURATION_Handle *mycfg,
342      struct GNUNET_TESTING_Peer *peer)
343 {
344   ret = 1;
345   stage = 0;
346   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
347   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
348
349   sh = GNUNET_ATS_scheduling_init (cfg, &ats_suggest_cb, NULL);
350   GNUNET_assert (NULL != sh);
351
352   ph = GNUNET_ATS_performance_init (cfg, &perf_mon_cb, &ret, NULL, NULL);
353   GNUNET_assert (NULL != ph);
354
355   /* Add addresses */
356   setup_addresses ();
357
358   /* Get an active address for peer0 and expect callback */
359   GNUNET_ATS_suggest_address (sh, &addr[0].peer);
360 }
361
362
363 int
364 main (int argc, char *argv[])
365 {
366   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance_monitor",
367                                     "test_ats_api.conf",
368                                     &run, NULL))
369     return 1;
370   return ret;
371 }
372
373 /* end of file test_ats_api_performance_monitor.c */