fix warning
[oweals/gnunet.git] / src / ats / test_ats_solver_request_and_delete_address.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  (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, delete address and wait for disconnect
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 GNUNET_SCHEDULER_TaskIdentifier die_task;
42
43 /**
44  * Statistics handle
45  */
46 struct GNUNET_STATISTICS_Handle *stats;
47
48 /**
49  * Scheduling handle
50  */
51 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
52
53 /**
54  * Return value
55  */
56 static int ret;
57
58 /**
59  * Test address
60  */
61 static struct Test_Address test_addr;
62
63 /**
64  * Test peer
65  */
66 static struct PeerContext p;
67
68 /**
69  * HELLO address
70  */
71 struct GNUNET_HELLO_Address test_hello_address;
72
73 /**
74  * Session
75  */
76 static void *test_session;
77
78 /**
79  * Test ats info
80  */
81 struct GNUNET_ATS_Information test_ats_info[2];
82
83 /**
84  * Test ats count
85  */
86 static uint32_t test_ats_count;
87
88
89 static int address_deleted = GNUNET_NO;
90
91 static int
92 stat_cb(void *cls, const char *subsystem, const char *name, uint64_t value,
93         int is_persistent);
94
95 static void
96 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 {
98   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
99   if (die_task != GNUNET_SCHEDULER_NO_TASK)
100   {
101     GNUNET_SCHEDULER_cancel (die_task);
102     die_task = GNUNET_SCHEDULER_NO_TASK;
103   }
104
105   if (NULL != sched_ats)
106   {
107     GNUNET_ATS_scheduling_done (sched_ats);
108     sched_ats = NULL;
109   }
110
111   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
112   if (NULL != stats)
113   {
114     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
115     stats = NULL;
116   }
117
118   free_test_address (&test_addr);
119
120   ret = 0;
121 }
122
123
124 static void
125 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
126 {
127   die_task = GNUNET_SCHEDULER_NO_TASK;
128
129   end ( NULL, NULL);
130   ret = GNUNET_SYSERR;
131 }
132
133 static void
134 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
135                     struct Session *session,
136                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
137                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
138                     const struct GNUNET_ATS_Information *atsi,
139                     uint32_t ats_count)
140 {
141
142   if (GNUNET_NO == address_deleted)
143   {
144     /* Expected address suggestion */
145     GNUNET_assert (NULL != address);
146     GNUNET_assert (NULL == session);
147     GNUNET_assert (ntohl(bandwidth_in.value__) > 0);
148     GNUNET_assert (ntohl(bandwidth_out.value__) > 0);
149
150     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
151         "Received sugggestion for peer `%s', deleting address\n",
152         GNUNET_i2s (&address->peer));
153     address_deleted = GNUNET_YES;
154     GNUNET_ATS_address_destroyed (sched_ats, &test_hello_address, NULL);
155
156   }
157   else
158   {
159     /* Expecting disconnect */
160
161     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
162         "Received sugggestion for peer `%s': %llu %llu\n",
163         GNUNET_i2s (&address->peer),
164         ntohl(bandwidth_in.value__),
165         ntohl(bandwidth_out.value__));
166
167     if ((ntohl(bandwidth_in.value__) == 0) &&
168         (ntohl(bandwidth_out.value__) == 0))
169     {
170       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
171       GNUNET_SCHEDULER_add_now (&end, NULL);
172     }
173   }
174   return;
175 }
176
177
178 static int
179 stat_cb(void *cls, const char *subsystem,
180         const char *name, uint64_t value,
181         int is_persistent)
182 {
183
184   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
185       subsystem,name, value);
186   if (GNUNET_NO == address_deleted)
187     GNUNET_ATS_suggest_address (sched_ats, &p.id);
188   return GNUNET_OK;
189 }
190
191 static void
192 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
193     struct GNUNET_TESTING_Peer *peer)
194 {
195   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
196   stats = GNUNET_STATISTICS_create ("ats", mycfg);
197   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
198
199
200   /* Connect to ATS scheduling */
201   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
202   if (sched_ats == NULL)
203   {
204     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
205     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
206     return;
207   }
208
209   /* Set up peer */
210   memset (&p.id, '1', sizeof (p.id));
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
212               GNUNET_i2s_full(&p.id));
213
214   /* Prepare ATS Information */
215   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
216   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
217   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
218   test_ats_info[1].value = htonl(1);
219   test_ats_count = 2;
220
221   /* Adding address without session */
222   test_session = NULL;
223   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
224   test_hello_address.peer = p.id;
225   test_hello_address.transport_name = test_addr.plugin;
226   test_hello_address.address = test_addr.addr;
227   test_hello_address.address_length = test_addr.addr_len;
228
229   /* Adding address */
230   GNUNET_ATS_address_add (sched_ats, &test_hello_address,
231       NULL, test_ats_info, test_ats_count);
232
233 }
234
235
236 int
237 main (int argc, char *argv[])
238 {
239   char *sep;
240   char *src_filename = GNUNET_strdup (__FILE__);
241   char *test_filename = GNUNET_strdup (argv[0]);
242   char *config_file;
243   char *solver;
244
245   ret = 0;
246
247   if (NULL == (sep  = (strstr (src_filename,".c"))))
248   {
249     GNUNET_break (0);
250     return -1;
251   }
252   sep[0] = '\0';
253
254   if (NULL != (sep = strstr (test_filename, ".exe")))
255     sep[0] = '\0';
256
257   if (NULL == (solver = strstr (test_filename, src_filename)))
258   {
259     GNUNET_break (0);
260     return -1;
261   }
262   solver += strlen (src_filename) +1;
263
264   if (0 == strcmp(solver, "proportional"))
265   {
266     config_file = "test_ats_solver_proportional.conf";
267   }
268   else if (0 == strcmp(solver, "mlp"))
269   {
270     config_file = "test_ats_solver_mlp.conf";
271   }
272   else if ((0 == strcmp(solver, "ril")))
273   {
274     config_file = "test_ats_solver_ril.conf";
275   }
276   else
277   {
278     GNUNET_break (0);
279     GNUNET_free (src_filename);
280     GNUNET_free (test_filename);
281     return 1;
282   }
283
284   GNUNET_free (src_filename);
285   GNUNET_free (test_filename);
286
287   if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
288       config_file, &run, NULL ))
289     return GNUNET_SYSERR;
290
291   return ret;
292 }
293
294 /* end of file test_ats_solver_add_address.c */