9682b8dc985e15848c6d8a7b487fa4633ec1827c
[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 monitoring
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, 20)
32 #define SHUTDOWN_CORRECT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
33
34 #define ATS_COUNT 3
35
36 static GNUNET_SCHEDULER_TaskIdentifier die_task;
37 static GNUNET_SCHEDULER_TaskIdentifier stage_task;
38
39 struct GNUNET_CONFIGURATION_Handle *cfg;
40
41 static struct GNUNET_ATS_SchedulingHandle *sh;
42
43 static struct GNUNET_ATS_PerformanceHandle *ph;
44
45 static struct GNUNET_HELLO_Address addr;
46
47 static struct GNUNET_ATS_Information atsi[ATS_COUNT];
48
49 static int ret;
50
51 static void cleanup_addresses ()
52 {
53         GNUNET_ATS_address_destroyed (sh, &addr, NULL);
54 }
55
56 static void setup_addresses ()
57 {
58         memset (&addr.peer,'\0', sizeof (addr.peer));
59         addr.transport_name = "test";
60         addr.address = NULL;
61         addr.address_length = 0;
62         atsi[0].type = htonl(GNUNET_ATS_NETWORK_TYPE);
63         atsi[0].value = htonl(GNUNET_ATS_NET_LAN);
64
65         atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
66         atsi[1].value = htonl(100);
67
68         atsi[2].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
69         atsi[2].value = htonl(5);
70
71         GNUNET_ATS_address_add (sh, &addr, NULL, atsi, ATS_COUNT);
72 }
73
74
75 static void
76 end_now (int res)
77 {
78         if (GNUNET_SCHEDULER_NO_TASK != stage_task)
79         {
80                         GNUNET_SCHEDULER_cancel (stage_task);
81                         stage_task = GNUNET_SCHEDULER_NO_TASK;
82         }
83         if (GNUNET_SCHEDULER_NO_TASK != die_task)
84         {
85                         GNUNET_SCHEDULER_cancel (die_task);
86                         die_task = GNUNET_SCHEDULER_NO_TASK;
87         }
88   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
89
90         cleanup_addresses ();
91
92         if (NULL != ph)
93         {
94                 GNUNET_ATS_performance_done (ph);
95                 ph = NULL;
96         }
97
98         if (NULL != sh)
99         {
100                 GNUNET_ATS_scheduling_done (sh);
101                 sh = NULL;
102         }
103         ret = res;
104 }
105
106 static void
107 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 {
109   die_task = GNUNET_SCHEDULER_NO_TASK;
110   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout\n");
111   end_now (1);
112 }
113
114 static void
115 next_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117         static int stage_counter = 0;
118
119         stage_task = GNUNET_SCHEDULER_NO_TASK;
120         if (0 == stage_counter)
121         {
122                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stop performance monitoring\n");
123
124                 stage_task = GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &next_stage, NULL);
125                 stage_counter++;
126                 return;
127         }
128         else
129         {
130                         end_now (0);
131         }
132 }
133
134 static void end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Success\n");
137   end_now (0);
138 }
139
140
141 static void
142 perf_mon_cb (void *cls,
143                                                 const struct GNUNET_PeerIdentity *peer,
144                                                 const struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
145                                                 const struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
146                                                 const struct GNUNET_ATS_Information *ats,
147                                                 uint32_t ats_count)
148 {
149         static int stage_counter = 0;
150         int c1;
151         int c2;
152         int c3;
153
154         if (0 == stage_counter)
155         {
156                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received initial callback for peer `%s' with %u information\n",
157                                 GNUNET_i2s (&addr.peer), ats_count);
158                 if ((0 != memcmp (peer, &addr.peer, sizeof (addr.peer))) ||
159                                 (ats_count < ATS_COUNT))
160                 {
161                                 GNUNET_break (0);
162                                 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
163                                 return;
164                 }
165                 c3 = 0;
166                 for (c1 = 0; c1 < ats_count; c1++)
167                 {
168                                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS information [%u] %u : %u \n", c1, ntohl (ats[c1].type), ntohl (ats[c1].value));
169                                 for (c2 = 0; c2 < ATS_COUNT; c2++)
170                                 {
171                                         if (ats[c1].type == atsi[c2].type)
172                                         {
173                                                 if (ats[c1].value == atsi[c2].value)
174                                                 {
175                                                                 c3++;
176                                                 }
177                                                 else
178                                                 {
179                                                                 GNUNET_break (0);
180                                                 }
181                                         }
182                                 }
183                 }
184
185                 if (ATS_COUNT != c3)
186                 {
187                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received only %u correct ATS information \n", c3);
188                         GNUNET_break (0);
189                         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
190                         return;
191                 }
192                 stage_counter ++;
193                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u correct ATS information \n", c3);
194
195                 atsi[0].type = htonl(GNUNET_ATS_NETWORK_TYPE);
196                 atsi[0].value = htonl(GNUNET_ATS_NET_WAN);
197
198                 atsi[1].type = htonl(GNUNET_ATS_QUALITY_NET_DELAY);
199                 atsi[1].value = htonl(1000);
200
201                 atsi[2].type = htonl(GNUNET_ATS_QUALITY_NET_DISTANCE);
202                 atsi[2].value = htonl(50);
203
204                 GNUNET_ATS_address_update (sh, &addr, NULL, atsi, ATS_COUNT);
205         }
206         else if (1 == stage_counter)
207         {
208                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received updated callback for peer `%s' with %u information\n",
209                                         GNUNET_i2s (&addr.peer), ats_count);
210
211                         if ((0 != memcmp (peer, &addr.peer, sizeof (addr.peer))) ||
212                                         (ats_count < ATS_COUNT))
213                         {
214                                         GNUNET_break (0);
215                                         GNUNET_SCHEDULER_add_now (&end_badly, NULL);
216                                         return;
217                         }
218                         c3 = 0;
219                         for (c1 = 0; c1 < ats_count; c1++)
220                         {
221                                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS information [%u] %u : %u \n", c1, ntohl (ats[c1].type), ntohl (ats[c1].value));
222                                         for (c2 = 0; c2 < ATS_COUNT; c2++)
223                                         {
224                                                 if (ats[c1].type == atsi[c2].type)
225                                                 {
226                                                         if (ats[c1].value == atsi[c2].value)
227                                                         {
228                                                                         c3++;
229                                                         }
230                                                         else
231                                                         {
232                                                                         GNUNET_break (0);
233                                                         }
234                                                 }
235                                         }
236                         }
237
238                         if (ATS_COUNT != c3)
239                         {
240                           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received only %u correct ATS information \n", c3);
241                                 GNUNET_break (0);
242                                 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
243                                 return;
244                         }
245                         stage_counter ++;
246                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %u correct ATS information, shutdown... \n", c3);
247                         GNUNET_SCHEDULER_add_now (&end, NULL);
248                         return;
249         }
250 }
251
252
253 static void
254 run (void *cls, 
255      const struct GNUNET_CONFIGURATION_Handle *mycfg,
256      struct GNUNET_TESTING_Peer *peer)
257 {
258   ret = 1;
259   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
260   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
261
262   sh = GNUNET_ATS_scheduling_init (cfg, NULL, NULL);
263   GNUNET_assert (NULL != sh);
264
265   setup_addresses ();
266
267   ph = GNUNET_ATS_performance_init (cfg, &perf_mon_cb, &ret, NULL, NULL);
268   GNUNET_assert (NULL != ph);
269
270
271   stage_task = GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &next_stage, NULL);
272 }
273
274
275 int
276 main (int argc, char *argv[])
277 {
278   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance_monitor",
279                                     "test_ats_api.conf",
280                                     &run, NULL))
281     return 1;
282   return ret;
283 }
284
285 /* end of file test_ats_api_performance_monitor.c */