2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file ats/test_ats_api_reset_backoff.c
22 * @brief test case for block reset api
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
31 #define ATS_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
33 static GNUNET_SCHEDULER_TaskIdentifier die_task;
35 static GNUNET_SCHEDULER_TaskIdentifier suggest_timeout_task;
37 static struct GNUNET_ATS_SchedulingHandle *ats;
39 struct GNUNET_OS_Process *arm_proc;
51 struct GNUNET_ATS_Information *ats;
59 struct GNUNET_PeerIdentity id;
64 struct GNUNET_HELLO_Address hello_addr;
65 struct Address address;
66 struct PeerContext peer;
67 struct GNUNET_ATS_Information atsi[2];
72 if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
73 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
74 GNUNET_OS_process_wait (arm_proc);
75 GNUNET_OS_process_destroy (arm_proc);
81 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 die_task = GNUNET_SCHEDULER_NO_TASK;
85 if (suggest_timeout_task != GNUNET_SCHEDULER_NO_TASK)
87 GNUNET_SCHEDULER_cancel (suggest_timeout_task);
88 suggest_timeout_task = GNUNET_SCHEDULER_NO_TASK;
93 GNUNET_ATS_scheduling_done (ats);
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
107 if (die_task != GNUNET_SCHEDULER_NO_TASK)
109 GNUNET_SCHEDULER_cancel (die_task);
110 die_task = GNUNET_SCHEDULER_NO_TASK;
113 if (suggest_timeout_task != GNUNET_SCHEDULER_NO_TASK)
115 GNUNET_SCHEDULER_cancel (suggest_timeout_task);
116 suggest_timeout_task = GNUNET_SCHEDULER_NO_TASK;
119 GNUNET_ATS_scheduling_done (ats);
128 suggest_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
130 suggest_timeout_task = GNUNET_SCHEDULER_NO_TASK;
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting address for peer timed out\n");
135 static void request_address ()
138 if (suggest_timeout_task != GNUNET_SCHEDULER_NO_TASK)
140 GNUNET_SCHEDULER_cancel (suggest_timeout_task);
141 suggest_timeout_task = GNUNET_SCHEDULER_NO_TASK;
144 suggest_timeout_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MINUTES, &suggest_timeout, NULL);
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting address for peer `%s'\n",
147 GNUNET_i2s (&peer.id));
148 GNUNET_ATS_suggest_address (ats, &peer.id);
153 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *a,
154 struct Session *session,
155 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
156 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
157 const struct GNUNET_ATS_Information *atsi,
160 static int suggestions;
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggests address `%s'\n",
162 GNUNET_i2s (&a->peer));
164 if (0 != memcmp (&a->peer, &peer.id,
165 sizeof (struct GNUNET_PeerIdentity)))
168 if (die_task != GNUNET_SCHEDULER_NO_TASK)
170 GNUNET_SCHEDULER_cancel (die_task);
171 die_task = GNUNET_SCHEDULER_NO_TASK;
173 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
177 if (0 != strcmp (a->transport_name, address.plugin))
180 if (die_task != GNUNET_SCHEDULER_NO_TASK)
182 GNUNET_SCHEDULER_cancel (die_task);
183 die_task = GNUNET_SCHEDULER_NO_TASK;
185 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
189 if (a->address_length != address.addr_len)
192 if (die_task != GNUNET_SCHEDULER_NO_TASK)
194 GNUNET_SCHEDULER_cancel (die_task);
195 die_task = GNUNET_SCHEDULER_NO_TASK;
197 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
201 if (0 != memcmp (a->address, address.addr,
205 if (die_task != GNUNET_SCHEDULER_NO_TASK)
207 GNUNET_SCHEDULER_cancel (die_task);
208 die_task = GNUNET_SCHEDULER_NO_TASK;
210 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
214 if (session != address.session)
217 if (die_task != GNUNET_SCHEDULER_NO_TASK)
219 GNUNET_SCHEDULER_cancel (die_task);
220 die_task = GNUNET_SCHEDULER_NO_TASK;
222 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
228 if (2 == suggestions)
230 GNUNET_SCHEDULER_add_now(&end, NULL);
234 if (suggest_timeout_task != GNUNET_SCHEDULER_NO_TASK)
236 GNUNET_SCHEDULER_cancel (suggest_timeout_task);
237 suggest_timeout_task = GNUNET_SCHEDULER_NO_TASK;
240 GNUNET_ATS_reset_backoff(ats, &a->peer);
248 start_arm (const char *cfgname)
251 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
252 "gnunet-service-arm",
253 "-c", cfgname, NULL);
257 check (void *cls, char *const *args, const char *cfgfile,
258 const struct GNUNET_CONFIGURATION_Handle *cfg)
262 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
265 ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
275 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
276 &peer.id.hashPubKey);
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n", GNUNET_i2s (&peer.id));
279 address.plugin = "test";
280 address.session = NULL;
281 address.addr = GNUNET_strdup ("test");
282 address.addr_len = 4;
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding address\n");
286 hello_addr.peer = peer.id;
287 hello_addr.transport_name = address.plugin;
288 hello_addr.address = address.addr;
289 hello_addr.address_length = address.addr_len;
290 GNUNET_ATS_address_update (ats, &hello_addr, address.session, NULL, 0);
296 main (int argc, char *argv[])
298 static char *const argv2[] = { "test_ats_api_scheduling",
305 static struct GNUNET_GETOPT_CommandLineOption options[] = {
306 GNUNET_GETOPT_OPTION_END
309 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
310 "test_ats_api_scheduling", "nohelp", options, &check,
316 /* end of file test_ats_api_reset_backoff.c */