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_EXTRA_LOGGING
39 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
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_SchedulingHandle *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_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_ATS_Information *ats, uint32_t ats_count)
126 struct ExpectedValues *ex = cls;
128 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
129 "ATS suggested address for peer `%s': `%s' `%s'\n",
130 GNUNET_i2s (peer), plugin_name, plugin_addr);
131 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS count %u\n", ats_count);
135 while (c < ats_count)
137 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ats[%u]: type %u value %u\n", c,
138 ntohl (ats[c].type), ntohl (ats[c].value));
143 if (ex->expected_ats_count != GNUNET_SYSERR)
144 GNUNET_assert (ex->expected_ats_count == ats_count);
146 if ((ex->expected_ats_value != GNUNET_SYSERR) &&
147 (ex->expected_in_index != GNUNET_SYSERR))
148 GNUNET_assert (ex->expected_ats_value ==
149 ntohl (ats[ex->expected_in_index].value));
151 if ((ex->expected_ats_type != GNUNET_SYSERR) &&
152 (ex->expected_in_index != GNUNET_SYSERR))
153 GNUNET_assert (ex->expected_ats_type ==
154 ntohl (ats[ex->expected_in_index].type));
160 check (void *cls, char *const *args, const char *cfgfile,
161 const struct GNUNET_CONFIGURATION_Handle *cfg)
163 struct ExpectedValues ex;
165 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
167 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Created peer identity `%s'\n",
170 ats = GNUNET_ATS_init (cfg, NULL, NULL);
171 GNUNET_assert (ats != NULL);
173 end_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end, NULL);
175 ar = GNUNET_malloc (sizeof (struct AllocationRecord));
177 ar->plugin_name = "test";
179 ar->plugin_addr = "address1";
180 ar->plugin_addr_len = strlen (ar->plugin_addr) + 1;
181 ar->ats = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information));
183 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initial ATS information\n");
185 ar->ats[0].type = htonl (0);
186 ar->ats[0].value = htonl (0);
188 ex.expected_ats_count = 1;
189 ex.expected_ats_type = 0;
190 ex.expected_ats_value = 0;
191 ex.expected_in_index = 0;
193 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
194 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
196 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
198 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending empty ATS information\n");
200 GNUNET_array_grow (ar->ats, ar->ats_count, ar->ats_count + 1);
201 ar->ats[0].type = htonl (1);
202 ar->ats[0].value = htonl (1);
203 ar->ats[1].type = htonl (0);
204 ar->ats[1].value = htonl (0);
206 ex.expected_ats_count = 2;
207 ex.expected_ats_type = 1;
208 ex.expected_ats_value = 1;
209 ex.expected_in_index = 0;
211 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
212 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
214 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
216 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
218 ar->ats[0].type = htonl (1);
219 ar->ats[0].value = htonl (2);
220 ar->ats[1].type = htonl (0);
221 ar->ats[1].value = htonl (0);
223 ex.expected_ats_count = 2;
224 ex.expected_ats_type = 1;
225 ex.expected_ats_value = 2;
226 ex.expected_in_index = 0;
228 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
229 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
231 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
233 /* Extending existing ATS information */
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending existing ATS information\n");
237 ar->ats[0].type = htonl (2);
238 ar->ats[0].value = htonl (2);
239 ar->ats[1].type = htonl (0);
240 ar->ats[1].value = htonl (0);
242 ex.expected_ats_count = 3;
243 ex.expected_ats_type = 2;
244 ex.expected_ats_value = 2;
245 ex.expected_in_index = 1;
247 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
248 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
250 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
252 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
254 ar->ats[0].type = htonl (2);
255 ar->ats[0].value = htonl (3);
256 ar->ats[1].type = htonl (0);
257 ar->ats[1].value = htonl (0);
259 ex.expected_ats_count = 3;
260 ex.expected_ats_type = 2;
261 ex.expected_ats_value = 3;
262 ex.expected_in_index = 1;
264 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
265 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
267 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
269 if (end_task != GNUNET_SCHEDULER_NO_TASK)
270 GNUNET_SCHEDULER_cancel (end_task);
271 end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
275 main (int argc, char *argv1[])
277 static char *const argv[] = { "test_ats_api_update_address",
288 static struct GNUNET_GETOPT_CommandLineOption options[] = {
289 GNUNET_GETOPT_OPTION_END
292 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
293 "test_ats_api_update_address", "nohelp", options, &check,