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"
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
40 #define VALID GNUNET_TIME_UNIT_FOREVER_ABS
42 static struct GNUNET_ATS_SchedulingHandle *ats;
44 static struct GNUNET_ATS_SuggestionContext *asc;
46 static struct GNUNET_PeerIdentity peer;
48 static GNUNET_SCHEDULER_TaskIdentifier end_task;
50 static struct AllocationRecord *ar;
56 int expected_ats_count;
58 int expected_ats_type;
60 int expected_ats_value;
62 int expected_in_index;
65 struct AllocationRecord
69 * Performance information associated with this address (array).
71 struct GNUNET_ATS_Information *ats;
79 * Address this record represents, allocated at the end of this struct.
81 const void *plugin_addr;
84 * Session associated with this record.
86 struct Session *session;
89 * Number of bytes in plugin_addr.
91 size_t plugin_addr_len;
94 * Number of entries in 'ats'.
100 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutdown\n");
105 GNUNET_ATS_suggest_address_cancel (asc);
108 GNUNET_ATS_shutdown (ats);
110 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
115 suggest_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
116 const char *plugin_name, const void *plugin_addr,
117 size_t plugin_addr_len, struct Session *session,
118 struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
119 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
121 struct ExpectedValues *ex = cls;
123 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
124 "ATS suggested address for peer `%s': `%s' `%s'\n",
125 GNUNET_i2s (peer), plugin_name, plugin_addr);
126 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS count %u\n", ats_count);
130 while (c < ats_count)
132 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ats[%u]: type %u value %u\n", c,
133 ntohl (ats[c].type), ntohl (ats[c].value));
138 if (ex->expected_ats_count != GNUNET_SYSERR)
139 GNUNET_assert (ex->expected_ats_count == ats_count);
141 if ((ex->expected_ats_value != GNUNET_SYSERR) &&
142 (ex->expected_in_index != GNUNET_SYSERR))
143 GNUNET_assert (ex->expected_ats_value ==
144 ntohl (ats[ex->expected_in_index].value));
146 if ((ex->expected_ats_type != GNUNET_SYSERR) &&
147 (ex->expected_in_index != GNUNET_SYSERR))
148 GNUNET_assert (ex->expected_ats_type ==
149 ntohl (ats[ex->expected_in_index].type));
155 check (void *cls, char *const *args, const char *cfgfile,
156 const struct GNUNET_CONFIGURATION_Handle *cfg)
158 struct ExpectedValues ex;
160 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
162 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Created peer identity `%s'\n",
165 ats = GNUNET_ATS_init (cfg, NULL, NULL);
166 GNUNET_assert (ats != NULL);
168 end_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end, NULL);
170 ar = GNUNET_malloc (sizeof (struct AllocationRecord));
172 ar->plugin_name = "test";
174 ar->plugin_addr = "address1";
175 ar->plugin_addr_len = strlen (ar->plugin_addr) + 1;
176 ar->ats = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information));
178 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initial ATS information\n");
180 ar->ats[0].type = htonl (0);
181 ar->ats[0].value = htonl (0);
183 ex.expected_ats_count = 1;
184 ex.expected_ats_type = 0;
185 ex.expected_ats_value = 0;
186 ex.expected_in_index = 0;
188 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
189 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
191 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
193 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending empty ATS information\n");
195 GNUNET_array_grow (ar->ats, ar->ats_count, ar->ats_count + 1);
196 ar->ats[0].type = htonl (1);
197 ar->ats[0].value = htonl (1);
198 ar->ats[1].type = htonl (0);
199 ar->ats[1].value = htonl (0);
201 ex.expected_ats_count = 2;
202 ex.expected_ats_type = 1;
203 ex.expected_ats_value = 1;
204 ex.expected_in_index = 0;
206 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
207 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
209 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
211 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
213 ar->ats[0].type = htonl (1);
214 ar->ats[0].value = htonl (2);
215 ar->ats[1].type = htonl (0);
216 ar->ats[1].value = htonl (0);
218 ex.expected_ats_count = 2;
219 ex.expected_ats_type = 1;
220 ex.expected_ats_value = 2;
221 ex.expected_in_index = 0;
223 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
224 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
226 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
228 /* Extending existing ATS information */
229 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending existing ATS information\n");
232 ar->ats[0].type = htonl (2);
233 ar->ats[0].value = htonl (2);
234 ar->ats[1].type = htonl (0);
235 ar->ats[1].value = htonl (0);
237 ex.expected_ats_count = 3;
238 ex.expected_ats_type = 2;
239 ex.expected_ats_value = 2;
240 ex.expected_in_index = 1;
242 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
243 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
245 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
247 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
249 ar->ats[0].type = htonl (2);
250 ar->ats[0].value = htonl (3);
251 ar->ats[1].type = htonl (0);
252 ar->ats[1].value = htonl (0);
254 ex.expected_ats_count = 3;
255 ex.expected_ats_type = 2;
256 ex.expected_ats_value = 3;
257 ex.expected_in_index = 1;
259 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
260 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
262 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
264 if (end_task != GNUNET_SCHEDULER_NO_TASK)
265 GNUNET_SCHEDULER_cancel (end_task);
266 end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
270 main (int argc, char *argv1[])
272 static char *const argv[] = { "test_ats_api_update_address",
279 static struct GNUNET_GETOPT_CommandLineOption options[] = {
280 GNUNET_GETOPT_OPTION_END
283 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
284 "test_ats_api_update_address", "nohelp", options, &check,