-patch from #1972 to display disconnects instead of exiting
[oweals/gnunet.git] / src / ats / test_ats_api_update_address.c
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_EXTRA_LOGGING
38
39 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
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
47 static struct GNUNET_ATS_SchedulingHandle *ats;
48
49 static struct GNUNET_ATS_SuggestionContext *asc;
50
51 static struct GNUNET_PeerIdentity peer;
52
53 static GNUNET_SCHEDULER_TaskIdentifier end_task;
54
55 static struct AllocationRecord *ar;
56
57 static int result;
58
59 struct 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
70 struct AllocationRecord
71 {
72
73   /**
74    * Performance information associated with this address (array).
75    */
76   struct GNUNET_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
104 static void
105 end (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
119 void
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)
125 {
126   struct ExpectedValues *ex = cls;
127
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);
132
133   int c = 0;
134
135   while (c < ats_count)
136   {
137     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ats[%u]: type %u value %u\n", c,
138                 ntohl (ats[c].type), ntohl (ats[c].value));
139
140     c++;
141   }
142
143   if (ex->expected_ats_count != GNUNET_SYSERR)
144     GNUNET_assert (ex->expected_ats_count == ats_count);
145
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));
150
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));
155
156
157 }
158
159 static void
160 check (void *cls, char *const *args, const char *cfgfile,
161        const struct GNUNET_CONFIGURATION_Handle *cfg)
162 {
163   struct ExpectedValues ex;
164
165   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
166                                     &peer.hashPubKey);
167   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Created peer identity `%s'\n",
168               GNUNET_i2s (&peer));
169
170   ats = GNUNET_ATS_init (cfg, NULL, NULL);
171   GNUNET_assert (ats != NULL);
172
173   end_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end, NULL);
174
175   ar = GNUNET_malloc (sizeof (struct AllocationRecord));
176
177   ar->plugin_name = "test";
178   ar->session = NULL;
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));
182
183   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Initial ATS information\n");
184   ar->ats_count = 1;
185   ar->ats[0].type = htonl (0);
186   ar->ats[0].value = htonl (0);
187
188   ex.expected_ats_count = 1;
189   ex.expected_ats_type = 0;
190   ex.expected_ats_value = 0;
191   ex.expected_in_index = 0;
192
193   GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
194                              ar->plugin_addr, ar->plugin_addr_len, ar->ats,
195                              ar->ats_count);
196   asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
197
198   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending empty ATS information\n");
199
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);
205
206   ex.expected_ats_count = 2;
207   ex.expected_ats_type = 1;
208   ex.expected_ats_value = 1;
209   ex.expected_in_index = 0;
210
211   GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
212                              ar->plugin_addr, ar->plugin_addr_len, ar->ats,
213                              ar->ats_count);
214   asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
215
216   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
217
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);
222
223   ex.expected_ats_count = 2;
224   ex.expected_ats_type = 1;
225   ex.expected_ats_value = 2;
226   ex.expected_in_index = 0;
227
228   GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
229                              ar->plugin_addr, ar->plugin_addr_len, ar->ats,
230                              ar->ats_count);
231   asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
232
233   /* Extending existing ATS information */
234   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Extending existing ATS information\n");
235
236
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);
241
242   ex.expected_ats_count = 3;
243   ex.expected_ats_type = 2;
244   ex.expected_ats_value = 2;
245   ex.expected_in_index = 1;
246
247   GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
248                              ar->plugin_addr, ar->plugin_addr_len, ar->ats,
249                              ar->ats_count);
250   asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
251
252   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Updating existing ATS information\n");
253
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);
258
259   ex.expected_ats_count = 3;
260   ex.expected_ats_type = 2;
261   ex.expected_ats_value = 3;
262   ex.expected_in_index = 1;
263
264   GNUNET_ATS_address_update (ats, &peer, VALID, ar->plugin_name, ar->session,
265                              ar->plugin_addr, ar->plugin_addr_len, ar->ats,
266                              ar->ats_count);
267   asc = GNUNET_ATS_suggest_address (ats, &peer, &suggest_cb, &ex);
268
269   if (end_task != GNUNET_SCHEDULER_NO_TASK)
270     GNUNET_SCHEDULER_cancel (end_task);
271   end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
272 }
273
274 int
275 main (int argc, char *argv1[])
276 {
277   static char *const argv[] = { "test_ats_api_update_address",
278     "-c",
279     "test_ats_api.conf",
280 #if VERBOSE
281     "-L", "DEBUG",
282 #else
283     "-L", "WARNING",
284 #endif
285     NULL
286   };
287
288   static struct GNUNET_GETOPT_CommandLineOption options[] = {
289     GNUNET_GETOPT_OPTION_END
290   };
291
292   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
293                       "test_ats_api_update_address", "nohelp", options, &check,
294                       NULL);
295
296   return result;
297 }