LRN: Use GNUNET_EXTRA_LOGGING to manage compile-time logging calls
[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_Handle *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_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
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_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
160 static void
161 check (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
275 int
276 main (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 }