2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @brief automatic transport selection API
23 * @author Christian Grothoff
24 * @author Matthias Wachs
28 * - extend API to get performance data
29 * - implement simplistic strategy based on say 'lowest latency' or strict ordering
30 * - extend API to get peer preferences, implement proportional bandwidth assignment
31 * - re-implement API against a real ATS service (!)
34 #include "gnunet_ats_service.h"
35 #include "gnunet_transport_service.h"
37 #define START_ARM GNUNET_YES
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
41 #define VALID GNUNET_TIME_absolute_get_forever ()
43 static struct GNUNET_ATS_SchedulingHandle *ats;
45 static struct GNUNET_ATS_SuggestionContext *asc;
47 static struct GNUNET_PeerIdentity peer;
49 static GNUNET_SCHEDULER_TaskIdentifier end_task;
51 static struct AllocationRecord *ar;
57 int expected_ats_count;
59 int expected_ats_type;
61 int expected_ats_value;
63 int expected_in_index;
66 struct AllocationRecord
70 * Performance information associated with this address (array).
72 struct GNUNET_ATS_Information *ats;
80 * Address this record represents, allocated at the end of this struct.
82 const void *plugin_addr;
85 * Session associated with this record.
87 struct Session *session;
90 * Number of bytes in plugin_addr.
92 size_t plugin_addr_len;
95 * Number of entries in 'ats'.
101 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutdown\n");
106 GNUNET_ATS_suggest_address_cancel (asc);
109 GNUNET_ATS_shutdown (ats);
111 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
116 suggest_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
117 const char *plugin_name, const void *plugin_addr,
118 size_t plugin_addr_len, struct Session *session,
119 struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
120 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
122 struct ExpectedValues *ex = cls;
124 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
125 "ATS suggested address for peer `%s': `%s' `%s'\n",
126 GNUNET_i2s (peer), plugin_name, plugin_addr);
127 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS count %u\n", ats_count);
131 while (c < ats_count)
133 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ats[%u]: type %u value %u\n", c,
134 ntohl (ats[c].type), ntohl (ats[c].value));
139 if (ex->expected_ats_count != GNUNET_SYSERR)
140 GNUNET_assert (ex->expected_ats_count == ats_count);
142 if ((ex->expected_ats_value != GNUNET_SYSERR) &&
143 (ex->expected_in_index != GNUNET_SYSERR))
144 GNUNET_assert (ex->expected_ats_value ==
145 ntohl (ats[ex->expected_in_index].value));
147 if ((ex->expected_ats_type != GNUNET_SYSERR) &&
148 (ex->expected_in_index != GNUNET_SYSERR))
149 GNUNET_assert (ex->expected_ats_type ==
150 ntohl (ats[ex->expected_in_index].type));
156 check (void *cls, char *const *args, const char *cfgfile,
157 const struct GNUNET_CONFIGURATION_Handle *cfg)
159 struct ExpectedValues ex;
161 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
163 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Created peer identity `%s'\n",
166 ats = GNUNET_ATS_init (cfg, NULL, NULL);
167 GNUNET_assert (ats != NULL);
169 end_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end, NULL);
171 ar = GNUNET_malloc (sizeof (struct AllocationRecord));
173 ar->plugin_name = "test";
175 ar->plugin_addr = "address1";
176 ar->plugin_addr_len = strlen (ar->plugin_addr) + 1;
177 ar->ats = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information));
179 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initial ATS information\n");
181 ar->ats[0].type = htonl (0);
182 ar->ats[0].value = htonl (0);
184 ex.expected_ats_count = 1;
185 ex.expected_ats_type = 0;
186 ex.expected_ats_value = 0;
187 ex.expected_in_index = 0;
189 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
190 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
192 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
194 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending empty ATS information\n");
196 GNUNET_array_grow (ar->ats, ar->ats_count, ar->ats_count + 1);
197 ar->ats[0].type = htonl (1);
198 ar->ats[0].value = htonl (1);
199 ar->ats[1].type = htonl (0);
200 ar->ats[1].value = htonl (0);
202 ex.expected_ats_count = 2;
203 ex.expected_ats_type = 1;
204 ex.expected_ats_value = 1;
205 ex.expected_in_index = 0;
207 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
208 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
210 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
212 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
214 ar->ats[0].type = htonl (1);
215 ar->ats[0].value = htonl (2);
216 ar->ats[1].type = htonl (0);
217 ar->ats[1].value = htonl (0);
219 ex.expected_ats_count = 2;
220 ex.expected_ats_type = 1;
221 ex.expected_ats_value = 2;
222 ex.expected_in_index = 0;
224 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
225 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
227 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
229 /* Extending existing ATS information */
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending existing ATS information\n");
233 ar->ats[0].type = htonl (2);
234 ar->ats[0].value = htonl (2);
235 ar->ats[1].type = htonl (0);
236 ar->ats[1].value = htonl (0);
238 ex.expected_ats_count = 3;
239 ex.expected_ats_type = 2;
240 ex.expected_ats_value = 2;
241 ex.expected_in_index = 1;
243 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
244 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
246 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
250 ar->ats[0].type = htonl (2);
251 ar->ats[0].value = htonl (3);
252 ar->ats[1].type = htonl (0);
253 ar->ats[1].value = htonl (0);
255 ex.expected_ats_count = 3;
256 ex.expected_ats_type = 2;
257 ex.expected_ats_value = 3;
258 ex.expected_in_index = 1;
260 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
261 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
263 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
265 if (end_task != GNUNET_SCHEDULER_NO_TASK)
266 GNUNET_SCHEDULER_cancel (end_task);
267 end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
271 main (int argc, char *argv1[])
273 static char *const argv[] = { "test_ats_api_update_address",
280 static struct GNUNET_GETOPT_CommandLineOption options[] = {
281 GNUNET_GETOPT_OPTION_END
284 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
285 "test_ats_api_update_address", "nohelp", options, &check,