-improve UDP logging
[oweals/gnunet.git] / src / ats / test_ats_solver_add_address_and_request.c
1 /*
2   if (NULL == (perf_ats = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL)))
3   {
4     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5         "Failed to connect to performance API\n");
6     GNUNET_SCHEDULER_add_now (end_badly, NULL);
7   }
8  This file is part of GNUnet.
9  Copyright (C) 2010-2013 Christian Grothoff (and other contributing authors)
10
11  GNUnet is free software; you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published
13  by the Free Software Foundation; either version 3, or (at your
14  option) any later version.
15
16  GNUnet is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with GNUnet; see the file COPYING.  If not, write to the
23  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  Boston, MA 02111-1307, USA.
25  */
26 /**
27  * @file ats/test_ats_solver_add_address.c
28  * @brief solver test:  add address, request address and wait for suggest
29  * @author Christian Grothoff
30  * @author Matthias Wachs
31  */
32 #include "platform.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_testbed_service.h"
35 #include "gnunet_ats_service.h"
36 #include "test_ats_api_common.h"
37
38 /**
39  * Timeout task
40  */
41 static struct GNUNET_SCHEDULER_Task *die_task;
42
43 /**
44  * Statistics handle
45  */
46 static struct GNUNET_STATISTICS_Handle *stats;
47
48 /**
49  * Scheduling handle
50  */
51 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
52
53 /**
54  * Connectivity handle
55  */
56 static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
57
58 /**
59  * Return value
60  */
61 static int ret;
62
63 /**
64  * Test address
65  */
66 static struct Test_Address test_addr;
67
68 /**
69  * Test peer
70  */
71 static struct PeerContext p;
72
73 /**
74  * HELLO address
75  */
76 static struct GNUNET_HELLO_Address test_hello_address;
77
78 /**
79  * Session
80  */
81 static void *test_session;
82
83 /**
84  * Test ats info
85  */
86 static struct GNUNET_ATS_Information test_ats_info[2];
87
88 /**
89  * Test ats count
90  */
91 static uint32_t test_ats_count;
92
93
94 static int
95 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
96         int is_persistent);
97
98
99 static void
100 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 {
102   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
103   if (die_task != NULL)
104   {
105     GNUNET_SCHEDULER_cancel (die_task);
106     die_task = NULL;
107   }
108
109   if (NULL != sched_ats)
110   {
111     GNUNET_ATS_scheduling_done (sched_ats);
112     sched_ats = NULL;
113   }
114   if (NULL != connect_ats)
115   {
116     GNUNET_ATS_connectivity_done (connect_ats);
117     connect_ats = NULL;
118   }
119
120   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
121   if (NULL != stats)
122   {
123     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
124     stats = NULL;
125   }
126
127   free_test_address (&test_addr);
128
129   ret = 0;
130 }
131
132
133 static void
134 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136   die_task = NULL;
137   end ( NULL, NULL);
138   ret = GNUNET_SYSERR;
139 }
140
141
142 static void
143 address_suggest_cb (void *cls,
144                     const struct GNUNET_PeerIdentity *peer,
145                     const struct GNUNET_HELLO_Address *address,
146                     struct Session *session,
147                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
148                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
149 {
150   GNUNET_assert (NULL != address);
151   GNUNET_assert (NULL == session);
152   GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
153   GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
154
155   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
156               "Received sugggestion for peer `%s'\n",
157               GNUNET_i2s (peer));
158   GNUNET_SCHEDULER_add_now (&end, NULL);
159 }
160
161
162 static int
163 stat_cb (void *cls, const char *subsystem,
164          const char *name, uint64_t value,
165          int is_persistent)
166 {
167   static struct GNUNET_ATS_ConnectivitySuggestHandle *sh;
168
169   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
170               "ATS statistics: `%s' `%s' %llu\n",
171               subsystem,
172               name,
173               value);
174   if (NULL == sh)
175     sh = GNUNET_ATS_connectivity_suggest (connect_ats, &p.id);
176   return GNUNET_OK;
177 }
178
179
180 static void
181 run (void *cls,
182      const struct GNUNET_CONFIGURATION_Handle *mycfg,
183      struct GNUNET_TESTING_Peer *peer)
184 {
185   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
186   stats = GNUNET_STATISTICS_create ("ats", mycfg);
187   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
188
189
190   connect_ats = GNUNET_ATS_connectivity_init (mycfg);
191   /* Connect to ATS scheduling */
192   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
193   if (sched_ats == NULL)
194   {
195     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196                 "Could not connect to ATS scheduling!\n");
197     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
198     return;
199   }
200
201   /* Set up peer */
202   memset (&p.id, '1', sizeof (p.id));
203   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204               "Created peer `%s'\n",
205               GNUNET_i2s_full(&p.id));
206
207   /* Prepare ATS Information */
208   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
209   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
210   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
211   test_ats_info[1].value = htonl(1);
212   test_ats_count = 2;
213
214   /* Adding address without session */
215   test_session = NULL;
216   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
217   test_hello_address.peer = p.id;
218   test_hello_address.transport_name = test_addr.plugin;
219   test_hello_address.address = test_addr.addr;
220   test_hello_address.address_length = test_addr.addr_len;
221
222   /* Adding address */
223   GNUNET_ATS_address_add (sched_ats,
224                           &test_hello_address, NULL,
225                           test_ats_info, test_ats_count);
226 }
227
228
229 int
230 main (int argc, char *argv[])
231 {
232   char *sep;
233   char *src_filename = GNUNET_strdup (__FILE__);
234   char *test_filename = GNUNET_strdup (argv[0]);
235   char *config_file;
236   char *solver;
237   int delayed = GNUNET_NO;
238
239   ret = 0;
240
241   if (NULL == (sep  = (strstr (src_filename,".c"))))
242   {
243     GNUNET_break (0);
244     return -1;
245   }
246   sep[0] = '\0';
247
248   if (NULL != (sep = strstr (test_filename, ".exe")))
249     sep[0] = '\0';
250
251   if (NULL == (solver = strstr (test_filename, src_filename)))
252   {
253     GNUNET_break (0);
254     return -1;
255   }
256
257   solver += strlen (src_filename) +1;
258
259   if (NULL != strstr (solver, "delayed_"))
260   {
261     delayed = GNUNET_YES;
262     solver += strlen  ("delayed_");
263   }
264
265   if (0 == strcmp(solver, "proportional"))
266   {
267     if (delayed)
268       config_file = "test_ats_solver_delayed_proportional.conf";
269     else
270       config_file = "test_ats_solver_proportional.conf";
271   }
272   else if (0 == strcmp(solver, "mlp"))
273   {
274     if (delayed)
275       config_file = "test_ats_solver_delayed_mlp.conf";
276     else
277       config_file = "test_ats_solver_mlp.conf";
278   }
279   else if ((0 == strcmp(solver, "ril")))
280   {
281     if (delayed)
282       config_file = "test_ats_solver_delayed_ril.conf";
283     else
284       config_file = "test_ats_solver_ril.conf";
285   }
286   else
287   {
288     GNUNET_break (0);
289     GNUNET_free (src_filename);
290     GNUNET_free (test_filename);
291     FPRINTF (stderr, "Invalid test name or configuration not found `%s'\n",src_filename);
292     return 1;
293   }
294
295   GNUNET_free (src_filename);
296   GNUNET_free (test_filename);
297
298   if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
299       config_file, &run, NULL ))
300     return GNUNET_SYSERR;
301
302   return ret;
303 }
304
305 /* end of file test_ats_solver_add_address.c */