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