ATS test: Give ATS time to come up with suggestion
[oweals/gnunet.git] / src / ats / test_ats2_lib.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2015 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file ats/test_ats2_lib.c
20  * @brief test ATS library with a generic interpreter for running ATS tests
21  * @author Julius Bünger
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_ats_application_service.h"
26 #include "gnunet_ats_transport_service.h"
27 #include "gnunet_testing_lib.h"
28
29 /**
30  * @brief Indicates the success of the whole test
31  */
32 static int ret;
33
34
35 /**
36  * @brief The time available until the test shuts down
37  */
38 static struct GNUNET_TIME_Relative timeout;
39
40
41 /**
42  * @brief ATS Application Handle
43  *
44  * Handle to the application-side of ATS.
45  */
46 static struct GNUNET_ATS_ApplicationHandle *ah;
47
48 /**
49  * @brief ATS Transport Handle
50  *
51  * Handle to the transport-side of ATS.
52  */
53 static struct GNUNET_ATS_TransportHandle *th;
54
55 /**
56  * @brief Another (dummy) peer.
57  *
58  * Used as the peer ATS shall allocate bandwidth to.
59  */
60 static struct GNUNET_PeerIdentity other_peer;
61
62 /**
63  * @brief Handle to the session record
64  */
65 static struct GNUNET_ATS_SessionRecord *sr;
66
67 /**
68  * @brief Called whenever allocation changed
69  *
70  * Implements #GNUNET_ATS_AllocationCallback
71  *
72  * @param cls
73  * @param session
74  * @param bandwidth_out
75  * @param bandwidth_in
76  *
77  * @return 
78  */
79 static void
80 allocation_cb (void *cls,
81                struct GNUNET_ATS_Session *session,
82                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
83                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
84 {
85   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "allocation_cb() called\n");
86 }
87
88
89 /**
90  * @brief Called whenever suggestion is made
91  *
92  * Implements #GNUNET_ATS_SuggestionCallback
93  *
94  * @param cls
95  * @param pid
96  * @param address
97  */
98 static void
99 suggestion_cb (void *cls,
100                const struct GNUNET_PeerIdentity *pid,
101                const char *address)
102 {
103   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggestion_cb() called\n");
104   ret = 0;
105 }
106
107
108 /**
109  * @brief Initialise both 'sides' of ATS
110  *
111  * Initialises the application and transportation side of ATS.
112  */
113 static void
114 init_both (const struct GNUNET_CONFIGURATION_Handle *cfg)
115 {
116   ah = GNUNET_ATS_application_init (cfg);
117   GNUNET_assert (NULL != ah);
118   th = GNUNET_ATS_transport_init (cfg,
119                                   allocation_cb,
120                                   NULL,
121                                   suggestion_cb,
122                                   NULL);
123   GNUNET_assert (NULL != ah);
124 }
125
126
127 /**
128  * @brief Disconnect both 'sides' of ATS
129  */
130 static void
131 finish_both (void)
132 {
133   GNUNET_ATS_application_done (ah);
134   ah = NULL;
135   GNUNET_ATS_transport_done (th);
136   th = NULL;
137 }
138
139
140 /**
141  * @brief Provide information about the start of an imaginary connection
142  */
143 static void
144 provide_info_start (void)
145 {
146   struct GNUNET_ATS_Properties prop =
147   {
148     .delay = GNUNET_TIME_UNIT_FOREVER_REL,
149     .goodput_out = 1048576,
150     .goodput_in = 1048576,
151     .utilization_out = 0,
152     .utilization_in = 0,
153     .distance = 0,
154     .mtu = UINT32_MAX,
155     .nt = GNUNET_NT_UNSPECIFIED,
156     .cc = GNUNET_TRANSPORT_CC_UNKNOWN,
157   };
158
159   sr = GNUNET_ATS_session_add (th,
160                                &other_peer,
161                                "test-address",
162                                NULL,
163                                &prop);
164   GNUNET_assert (NULL != sr);
165 }
166
167
168 /**
169  * @brief Provide information about the end of an imaginary connection
170  */
171 static void
172 provide_info_end (void)
173 {
174   GNUNET_ATS_session_del (sr);
175 }
176
177
178 /**
179  * @brief Inform ATS about the need of a connection towards a peer
180  */
181 static void
182 get_suggestion (void)
183 {
184   struct GNUNET_ATS_ApplicationSuggestHandle *ash;
185
186   ash = GNUNET_ATS_application_suggest (ah,
187                                         &other_peer,
188                                         GNUNET_MQ_PREFERENCE_NONE,
189                                         GNUNET_BANDWIDTH_VALUE_MAX);
190   GNUNET_assert (NULL != ash);
191 }
192
193
194 static void
195 on_shutdown (void *cls)
196 {
197   provide_info_end ();
198   finish_both ();
199   GNUNET_SCHEDULER_shutdown ();
200 }
201
202
203 /**
204  * Function run once the ATS service has been started.
205  *
206  * @param cls NULL
207  * @param cfg configuration for the testcase
208  * @param peer handle to the peer
209  */
210 static void
211 run (void *cls,
212      const struct GNUNET_CONFIGURATION_Handle *cfg,
213      struct GNUNET_TESTING_Peer *peer)
214 {
215   init_both (cfg);
216   provide_info_start ();
217   get_suggestion ();
218   (void) GNUNET_SCHEDULER_add_delayed (timeout, &on_shutdown, NULL);
219 }
220
221
222 /**
223  * @brief Starts the gnunet-testing peer
224  *
225  * @param argc
226  * @param argv[]
227  *
228  * @return 
229  */
230 int
231 main (int argc,
232       char *argv[])
233 {
234   ret = 1;
235   memset (&other_peer, 0, sizeof (struct GNUNET_PeerIdentity));
236   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
237   if (0 != GNUNET_TESTING_peer_run ("test-ats2-lib",
238                                     "test_ats2_lib.conf",
239                                     &run, NULL))
240   {
241     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running the testing peer failed.\n");
242     return 1;
243   }
244   return ret;
245 }
246
247
248
249 /* end of test_ats2_lib.c */