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 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 static GNUNET_SCHEDULER_TaskIdentifier die_task;
35 static GNUNET_SCHEDULER_TaskIdentifier stage_task;
36
37 struct GNUNET_CONFIGURATION_Handle *cfg;
38
39 static struct GNUNET_ATS_SchedulingHandle *sh;
40
41 static struct GNUNET_ATS_PerformanceHandle *ph;
42
43 static struct GNUNET_ATS_PerformanceMonitorHandle *phm;
44
45 static struct GNUNET_HELLO_Address addr;
46
47 static struct GNUNET_ATS_Information atsi[3];
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, 3);
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 != phm)
93         {
94                 GNUNET_ATS_performance_monitor_stop (phm);
95                 phm = NULL;
96         }
97
98         if (NULL != ph)
99         {
100                 GNUNET_ATS_performance_done (ph);
101                 ph = NULL;
102         }
103
104         if (NULL != sh)
105         {
106                 GNUNET_ATS_scheduling_done (sh);
107                 sh = NULL;
108         }
109         ret = res;
110 }
111
112 static void
113 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 {
115   die_task = GNUNET_SCHEDULER_NO_TASK;
116   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout\n");
117   end_now (1);
118 }
119
120 static void
121 next_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 {
123         static int stage_counter = 0;
124
125         stage_task = GNUNET_SCHEDULER_NO_TASK;
126         if (0 == stage_counter)
127         {
128                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stop performance monitoring\n");
129
130                 GNUNET_ATS_performance_monitor_stop (phm);
131                 phm = NULL;
132
133                 stage_task = GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &next_stage, NULL);
134                 stage_counter++;
135                 return;
136         }
137         else
138         {
139                         end_now (0);
140         }
141 }
142
143
144
145
146 static void
147 perf_mon_cb (void *cls,
148                                                 struct GNUNET_PeerIdentity *peer,
149                                                 struct GNUNET_ATS_Information *ats,
150                                                 uint32_t ats_count)
151 {
152
153 }
154
155
156 static void
157 run (void *cls, 
158      const struct GNUNET_CONFIGURATION_Handle *mycfg,
159      struct GNUNET_TESTING_Peer *peer)
160 {
161   ret = 1;
162   cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
163   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
164
165   sh = GNUNET_ATS_scheduling_init (cfg, NULL, NULL);
166   GNUNET_assert (NULL != sh);
167
168   setup_addresses ();
169
170   ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
171   GNUNET_assert (NULL != ph);
172
173   phm = GNUNET_ATS_performance_monitor_start (ph, &perf_mon_cb, &ret);
174   GNUNET_assert (NULL != phm);
175
176   stage_task = GNUNET_SCHEDULER_add_delayed (SHUTDOWN_CORRECT, &next_stage, NULL);
177 }
178
179
180 int
181 main (int argc, char *argv[])
182 {
183   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance_monitor",
184                                     "test_ats_api.conf",
185                                     &run, NULL))
186     return 1;
187   return ret;
188 }
189
190 /* end of file test_ats_api_performance_monitor.c */