-fix config, shutdown issue
[oweals/gnunet.git] / src / ats / test_ats_solver_request_and_add_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  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: request address, add 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 static void
99 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
100 {
101   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
102   if (die_task != NULL)
103   {
104     GNUNET_SCHEDULER_cancel (die_task);
105     die_task = NULL;
106   }
107
108   if (NULL != sched_ats)
109   {
110     GNUNET_ATS_scheduling_done (sched_ats);
111     sched_ats = NULL;
112   }
113   if (NULL != connect_ats)
114   {
115     GNUNET_ATS_connectivity_done (connect_ats);
116     connect_ats = NULL;
117   }
118   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
119   if (NULL != stats)
120   {
121     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
122     stats = NULL;
123   }
124
125   free_test_address (&test_addr);
126
127   ret = 0;
128 }
129
130
131 static void
132 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {
134   die_task = NULL;
135   end ( NULL, NULL);
136   ret = GNUNET_SYSERR;
137 }
138
139 static void
140 address_suggest_cb (void *cls,
141                     const struct GNUNET_PeerIdentity *peer,
142                     const struct GNUNET_HELLO_Address *address,
143                     struct Session *session,
144                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
145                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
146 {
147
148   GNUNET_assert (NULL != address);
149   GNUNET_assert (NULL == session);
150
151   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received sugggestion for peer `%s'\n",
152       GNUNET_i2s (&address->peer));
153
154   GNUNET_SCHEDULER_add_now (&end, NULL);
155 }
156
157
158 static int
159 stat_cb(void *cls, const char *subsystem,
160         const char *name, uint64_t value,
161         int is_persistent)
162 {
163
164   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
165       subsystem,name, value);
166   return GNUNET_OK;
167 }
168
169 static void
170 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
171     struct GNUNET_TESTING_Peer *peer)
172 {
173   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
174   stats = GNUNET_STATISTICS_create ("ats", mycfg);
175   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
176
177   connect_ats = GNUNET_ATS_connectivity_init (mycfg);
178   /* Connect to ATS scheduling */
179   sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
180   if (sched_ats == NULL)
181   {
182     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
183     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
184     return;
185   }
186
187   /* Set up peer */
188   memset (&p.id, '1', sizeof (p.id));
189   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
190               GNUNET_i2s_full(&p.id));
191
192   /* Prepare ATS Information */
193   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
194   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
195   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
196   test_ats_info[1].value = htonl(1);
197   test_ats_count = 2;
198
199   /* Adding address without session */
200   test_session = NULL;
201   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
202   test_hello_address.peer = p.id;
203   test_hello_address.transport_name = test_addr.plugin;
204   test_hello_address.address = test_addr.addr;
205   test_hello_address.address_length = test_addr.addr_len;
206
207   /* Request */
208   GNUNET_ATS_connectivity_suggest (connect_ats, &p.id);
209
210
211   /* Adding address */
212   GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
213 }
214
215
216 int
217 main (int argc, char *argv[])
218 {
219   char *sep;
220   char *src_filename = GNUNET_strdup (__FILE__);
221   char *test_filename = GNUNET_strdup (argv[0]);
222   char *config_file;
223   char *solver;
224
225   ret = 0;
226
227   if (NULL == (sep  = (strstr (src_filename,".c"))))
228   {
229     GNUNET_break (0);
230     return -1;
231   }
232   sep[0] = '\0';
233
234   if (NULL != (sep = strstr (test_filename, ".exe")))
235     sep[0] = '\0';
236
237   if (NULL == (solver = strstr (test_filename, src_filename)))
238   {
239     GNUNET_break (0);
240     return -1;
241   }
242   solver += strlen (src_filename) +1;
243
244   if (0 == strcmp(solver, "proportional"))
245   {
246     config_file = "test_ats_solver_proportional.conf";
247   }
248   else if (0 == strcmp(solver, "mlp"))
249   {
250     config_file = "test_ats_solver_mlp.conf";
251   }
252   else if ((0 == strcmp(solver, "ril")))
253   {
254     config_file = "test_ats_solver_ril.conf";
255   }
256   else
257   {
258     GNUNET_break (0);
259     GNUNET_free (src_filename);
260     GNUNET_free (test_filename);
261     return 1;
262   }
263
264   GNUNET_free (src_filename);
265   GNUNET_free (test_filename);
266
267   if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
268       config_file, &run, NULL ))
269     return GNUNET_SYSERR;
270
271   return ret;
272 }
273
274 /* end of file test_ats_solver_add_address.c */