aboutsummaryrefslogtreecommitdiff
path: root/src/ats
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-09-09 15:06:53 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-09-09 15:06:53 +0000
commit572e6cfded2bece750dc1a0e35432112e89f4467 (patch)
tree6b1ece186d66c4bf321f1a781be2f0f370a9307a /src/ats
parente58f3d1bc8729c6557b7cad0c04746ae21822364 (diff)
downloadgnunet-572e6cfded2bece750dc1a0e35432112e89f4467.tar.gz
gnunet-572e6cfded2bece750dc1a0e35432112e89f4467.zip
fix in address update + testcase
Diffstat (limited to 'src/ats')
-rw-r--r--src/ats/Makefile.am10
-rw-r--r--src/ats/ats_api.c22
-rw-r--r--src/ats/test_ats_api_update_address.c298
3 files changed, 313 insertions, 17 deletions
diff --git a/src/ats/Makefile.am b/src/ats/Makefile.am
index 652cc7462..d44f59e6f 100644
--- a/src/ats/Makefile.am
+++ b/src/ats/Makefile.am
@@ -28,7 +28,8 @@ libgnunetats_la_LIBADD = \
28 28
29 29
30check_PROGRAMS = \ 30check_PROGRAMS = \
31 test_ats_api 31 test_ats_api \
32 test_ats_api_update_address
32 33
33if ENABLE_TEST_RUN 34if ENABLE_TEST_RUN
34TESTS = $(check_PROGRAMS) 35TESTS = $(check_PROGRAMS)
@@ -40,5 +41,8 @@ test_ats_api_LDADD = \
40 $(top_builddir)/src/util/libgnunetutil.la \ 41 $(top_builddir)/src/util/libgnunetutil.la \
41 $(top_builddir)/src/ats/libgnunetats.la 42 $(top_builddir)/src/ats/libgnunetats.la
42 43
43 44test_ats_api_update_address_SOURCES = \
44 45 test_ats_api_update_address.c
46test_ats_api_update_address_LDADD = \
47 $(top_builddir)/src/util/libgnunetutil.la \
48 $(top_builddir)/src/ats/libgnunetats.la
diff --git a/src/ats/ats_api.c b/src/ats/ats_api.c
index e6013a84c..a411d4dcb 100644
--- a/src/ats/ats_api.c
+++ b/src/ats/ats_api.c
@@ -343,13 +343,6 @@ GNUNET_ATS_suggest_address (struct GNUNET_ATS_Handle *atc,
343 GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers, &peer->hashPubKey, 343 GNUNET_CONTAINER_multihashmap_get_multiple (atc->peers, &peer->hashPubKey,
344 &suggest_address, asc); 344 &suggest_address, asc);
345 345
346#if DEBUG_ATS
347 GNUNET_CONTAINER_multihashmap_iterate (atc->peers, &map_it, (void *) peer);
348 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-api",
349 "Addresses %u (of %i) processed, \n", count,
350 GNUNET_CONTAINER_multihashmap_size (atc->peers));
351#endif
352
353 if (NULL == asc->cb) 346 if (NULL == asc->cb)
354 { 347 {
355 GNUNET_free (asc); 348 GNUNET_free (asc);
@@ -531,7 +524,7 @@ update_session (void *cls, const GNUNET_HashCode * key, void *value)
531 { 524 {
532#if DEBUG_ATS 525#if DEBUG_ATS
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
534 "Found type %u, old value=%u new value=%u\n", 527 "Found type %i, old value=%i new value=%i\n",
535 ntohl (arold->ats[c_old].type), 528 ntohl (arold->ats[c_old].type),
536 ntohl (arold->ats[c_old].value), 529 ntohl (arold->ats[c_old].value),
537 ntohl (arnew->ats[c_new].value)); 530 ntohl (arnew->ats[c_new].value));
@@ -545,18 +538,18 @@ update_session (void *cls, const GNUNET_HashCode * key, void *value)
545 if (found == GNUNET_NO) 538 if (found == GNUNET_NO)
546 { 539 {
547#if DEBUG_ATS 540#if DEBUG_ATS
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new type %i new value=%i\n",
549 "Added new value type %u, old value=%u new value=%u\n",
550 ntohl (arnew->ats[c_new].type), 542 ntohl (arnew->ats[c_new].type),
551 ntohl (arnew->ats[c_new].value)); 543 ntohl (arnew->ats[c_new].value));
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Old array size: %u\n", 544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Old array size: %u\n",
553 arold->ats_count); 545 arold->ats_count);
554#endif 546#endif
555 GNUNET_array_grow (arold->ats, arold->ats_count, arold->ats_count + 1); 547 GNUNET_array_grow (arold->ats, arold->ats_count, arold->ats_count + 1);
556 arold->ats[arold->ats_count - 1].type = arnew->ats[c_new].type; 548 GNUNET_assert (arold->ats_count >= 2);
557 arold->ats[arold->ats_count - 1].value = arnew->ats[c_new].value; 549 arold->ats[arold->ats_count - 2].type = arnew->ats[c_new].type;
558 arold->ats[arold->ats_count].type = htonl (0); 550 arold->ats[arold->ats_count - 2].value = arnew->ats[c_new].value;
559 arold->ats[arold->ats_count].value = htonl (0); 551 arold->ats[arold->ats_count - 1].type = htonl (0);
552 arold->ats[arold->ats_count - 1].value = htonl (0);
560#if DEBUG_ATS 553#if DEBUG_ATS
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New array size: %i\n", 554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New array size: %i\n",
562 arold->ats_count); 555 arold->ats_count);
@@ -780,6 +773,7 @@ notify_valid (void *cls, const GNUNET_HashCode * key, void *value)
780 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), ar->ats, 773 GNUNET_BANDWIDTH_value_init (asc->atc->total_bps / 32), ar->ats,
781 ar->ats_count); 774 ar->ats_count);
782 GNUNET_ATS_suggest_address_cancel (asc); 775 GNUNET_ATS_suggest_address_cancel (asc);
776 asc = NULL;
783 return GNUNET_OK; 777 return GNUNET_OK;
784} 778}
785 779
diff --git a/src/ats/test_ats_api_update_address.c b/src/ats/test_ats_api_update_address.c
new file mode 100644
index 000000000..1c6351fcd
--- /dev/null
+++ b/src/ats/test_ats_api_update_address.c
@@ -0,0 +1,298 @@
1/*
2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
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.
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 General Public License for more details.
14
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.
19*/
20/**
21 * @file ats/ats_api.c
22 * @brief automatic transport selection API
23 * @author Christian Grothoff
24 * @author Matthias Wachs
25 *
26 * TODO:
27 * - write test case
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 (!)
32 */
33#include "platform.h"
34#include "gnunet_ats_service.h"
35#include "gnunet_transport_service.h"
36
37#define VERBOSE GNUNET_NO
38
39#define VERBOSE_ARM GNUNET_NO
40
41#define START_ARM GNUNET_YES
42
43#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
44
45#define VALID GNUNET_TIME_absolute_get_forever ()
46
47static struct GNUNET_ATS_Handle *ats;
48
49static struct GNUNET_ATS_SuggestionContext *asc;
50
51static struct GNUNET_PeerIdentity peer;
52
53static GNUNET_SCHEDULER_TaskIdentifier end_task;
54
55static struct AllocationRecord *ar;
56
57static int result;
58
59struct ExpectedValues
60{
61 int expected_ats_count;
62
63 int expected_ats_type;
64
65 int expected_ats_value;
66
67 int expected_in_index;
68};
69
70struct AllocationRecord
71{
72
73 /**
74 * Performance information associated with this address (array).
75 */
76 struct GNUNET_TRANSPORT_ATS_Information *ats;
77
78 /**
79 * Name of the plugin
80 */
81 char *plugin_name;
82
83 /**
84 * Address this record represents, allocated at the end of this struct.
85 */
86 const void *plugin_addr;
87
88 /**
89 * Session associated with this record.
90 */
91 struct Session *session;
92
93 /**
94 * Number of bytes in plugin_addr.
95 */
96 size_t plugin_addr_len;
97
98 /**
99 * Number of entries in 'ats'.
100 */
101 uint32_t ats_count;
102};
103
104static void
105end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
106{
107 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Shutdown\n");
108 if (asc != NULL)
109 {
110 GNUNET_ATS_suggest_address_cancel (asc);
111 asc = NULL;
112 }
113 GNUNET_ATS_shutdown (ats);
114
115 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
116 GNUNET_free (ar);
117}
118
119void
120suggest_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,
125 uint32_t ats_count)
126{
127 struct ExpectedValues *ex = cls;
128
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);
133
134 int c = 0;
135
136 while (c < ats_count)
137 {
138 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ats[%u]: type %u value %u\n", c,
139 ntohl (ats[c].type), ntohl (ats[c].value));
140
141 c++;
142 }
143
144 if (ex->expected_ats_count != GNUNET_SYSERR)
145 GNUNET_assert (ex->expected_ats_count == ats_count);
146
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));
151
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));
156
157
158}
159
160static void
161check (void *cls, char *const *args, const char *cfgfile,
162 const struct GNUNET_CONFIGURATION_Handle *cfg)
163{
164 struct ExpectedValues ex;
165
166 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
167 &peer.hashPubKey);
168 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Created peer identity `%s'\n",
169 GNUNET_i2s (&peer));
170
171 ats = GNUNET_ATS_init (cfg, NULL, NULL);
172 GNUNET_assert (ats != NULL);
173
174 end_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end, NULL);
175
176 ar = GNUNET_malloc (sizeof (struct AllocationRecord));
177
178 ar->plugin_name = "test";
179 ar->session = NULL;
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));
183
184 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initial ATS information\n");
185 ar->ats_count = 1;
186 ar->ats[0].type = htonl (0);
187 ar->ats[0].value = htonl (0);
188
189 ex.expected_ats_count = 1;
190 ex.expected_ats_type = 0;
191 ex.expected_ats_value = 0;
192 ex.expected_in_index = 0;
193
194 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
195 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
196 ar->ats_count);
197 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
198
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending empty ATS information\n");
200
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);
206
207 ex.expected_ats_count = 2;
208 ex.expected_ats_type = 1;
209 ex.expected_ats_value = 1;
210 ex.expected_in_index = 0;
211
212 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
213 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
214 ar->ats_count);
215 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
216
217 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
218
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);
223
224 ex.expected_ats_count = 2;
225 ex.expected_ats_type = 1;
226 ex.expected_ats_value = 2;
227 ex.expected_in_index = 0;
228
229 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
230 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
231 ar->ats_count);
232 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
233
234 /* Extending existing ATS information */
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending existing ATS information\n");
236
237
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);
242
243 ex.expected_ats_count = 3;
244 ex.expected_ats_type = 2;
245 ex.expected_ats_value = 2;
246 ex.expected_in_index = 1;
247
248 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
249 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
250 ar->ats_count);
251 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
252
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
254
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);
259
260 ex.expected_ats_count = 3;
261 ex.expected_ats_type = 2;
262 ex.expected_ats_value = 3;
263 ex.expected_in_index = 1;
264
265 GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
266 ar->plugin_addr, ar->plugin_addr_len, ar->ats,
267 ar->ats_count);
268 asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
269
270 if (end_task != GNUNET_SCHEDULER_NO_TASK)
271 GNUNET_SCHEDULER_cancel (end_task);
272 end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
273}
274
275int
276main (int argc, char *argv1[])
277{
278 static char *const argv[] = { "test_ats_api_update_address",
279 "-c",
280 "test_ats_api.conf",
281#if VERBOSE
282 "-L", "DEBUG",
283#else
284 "-L", "WARNING",
285#endif
286 NULL
287 };
288
289 static struct GNUNET_GETOPT_CommandLineOption options[] = {
290 GNUNET_GETOPT_OPTION_END
291 };
292
293 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
294 "test_ats_api_update_address", "nohelp", options, &check,
295 NULL);
296
297 return result;
298}