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 VERBOSE GNUNET_NO
39 #define VERBOSE_ARM GNUNET_NO
41 #define START_ARM GNUNET_YES
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
45 #define VALID GNUNET_TIME_absolute_get_forever ()
47 static struct GNUNET_ATS_Handle *ats;
49 static struct GNUNET_ATS_SuggestionContext *asc;
51 static struct GNUNET_PeerIdentity peer;
53 static GNUNET_SCHEDULER_TaskIdentifier end_task;
55 static struct AllocationRecord *ar;
61 int expected_ats_count;
63 int expected_ats_type;
65 int expected_ats_value;
67 int expected_in_index;
70 struct AllocationRecord
74 * Performance information associated with this address (array).
76 struct GNUNET_TRANSPORT_ATS_Information *ats;
84 * Address this record represents, allocated at the end of this struct.
86 const void *plugin_addr;
89 * Session associated with this record.
91 struct Session *session;
94 * Number of bytes in plugin_addr.
96 size_t plugin_addr_len;
99 * Number of entries in 'ats'.
105 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutdown\n");
110 GNUNET_ATS_suggest_address_cancel (asc);
113 GNUNET_ATS_shutdown (ats);
115 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
120 suggest_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
121 const char *plugin_name, const void *plugin_addr,
122 size_t plugin_addr_len, struct Session *session,
123 struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
124 const struct GNUNET_TRANSPORT_ATS_Information *ats,
127 struct ExpectedValues *ex = cls;
129 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
130 "ATS suggested address for peer `%s': `%s' `%s'\n",
131 GNUNET_i2s (peer), plugin_name, plugin_addr);
132 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS count %u\n", ats_count);
136 while (c < ats_count)
138 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ats[%u]: type %u value %u\n", c,
139 ntohl (ats[c].type), ntohl (ats[c].value));
144 if (ex->expected_ats_count != GNUNET_SYSERR)
145 GNUNET_assert (ex->expected_ats_count == ats_count);
147 if ((ex->expected_ats_value != GNUNET_SYSERR) &&
148 (ex->expected_in_index != GNUNET_SYSERR))
149 GNUNET_assert (ex->expected_ats_value ==
150 ntohl (ats[ex->expected_in_index].value));
152 if ((ex->expected_ats_type != GNUNET_SYSERR) &&
153 (ex->expected_in_index != GNUNET_SYSERR))
154 GNUNET_assert (ex->expected_ats_type ==
155 ntohl (ats[ex->expected_in_index].type));
161 check (void *cls, char *const *args, const char *cfgfile,
162 const struct GNUNET_CONFIGURATION_Handle *cfg)
164 struct ExpectedValues ex;
166 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
168 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Created peer identity `%s'\n",
171 ats = GNUNET_ATS_init (cfg, NULL, NULL);
172 GNUNET_assert (ats != NULL);
174 end_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end, NULL);
176 ar = GNUNET_malloc (sizeof (struct AllocationRecord));
178 ar->plugin_name = "test";
180 ar->plugin_addr = "address1";
181 ar->plugin_addr_len = strlen (ar->plugin_addr) + 1;
182 ar->ats = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_ATS_Information));
184 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initial ATS information\n");
186 ar->ats[0].type = htonl (0);
187 ar->ats[0].value = htonl (0);
189 ex.expected_ats_count = 1;
190 ex.expected_ats_type = 0;
191 ex.expected_ats_value = 0;
192 ex.expected_in_index = 0;
194 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
195 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
197 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending empty ATS information\n");
201 GNUNET_array_grow (ar->ats, ar->ats_count, ar->ats_count + 1);
202 ar->ats[0].type = htonl (1);
203 ar->ats[0].value = htonl (1);
204 ar->ats[1].type = htonl (0);
205 ar->ats[1].value = htonl (0);
207 ex.expected_ats_count = 2;
208 ex.expected_ats_type = 1;
209 ex.expected_ats_value = 1;
210 ex.expected_in_index = 0;
212 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
213 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
215 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
217 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
219 ar->ats[0].type = htonl (1);
220 ar->ats[0].value = htonl (2);
221 ar->ats[1].type = htonl (0);
222 ar->ats[1].value = htonl (0);
224 ex.expected_ats_count = 2;
225 ex.expected_ats_type = 1;
226 ex.expected_ats_value = 2;
227 ex.expected_in_index = 0;
229 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
230 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
232 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
234 /* Extending existing ATS information */
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending existing ATS information\n");
238 ar->ats[0].type = htonl (2);
239 ar->ats[0].value = htonl (2);
240 ar->ats[1].type = htonl (0);
241 ar->ats[1].value = htonl (0);
243 ex.expected_ats_count = 3;
244 ex.expected_ats_type = 2;
245 ex.expected_ats_value = 2;
246 ex.expected_in_index = 1;
248 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
249 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
251 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
255 ar->ats[0].type = htonl (2);
256 ar->ats[0].value = htonl (3);
257 ar->ats[1].type = htonl (0);
258 ar->ats[1].value = htonl (0);
260 ex.expected_ats_count = 3;
261 ex.expected_ats_type = 2;
262 ex.expected_ats_value = 3;
263 ex.expected_in_index = 1;
265 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
266 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
268 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
270 if (end_task != GNUNET_SCHEDULER_NO_TASK)
271 GNUNET_SCHEDULER_cancel (end_task);
272 end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
276 main (int argc, char *argv1[])
278 static char *const argv[] = { "test_ats_api_update_address",
289 static struct GNUNET_GETOPT_CommandLineOption options[] = {
290 GNUNET_GETOPT_OPTION_END
293 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
294 "test_ats_api_update_address", "nohelp", options, &check,