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_bandwidth_consumption.c
22 * @brief test automatic transport selection scheduling API
23 * @author Christian Grothoff
24 * @author Matthias Wachs
28 #include "gnunet_ats_service.h"
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33 static GNUNET_SCHEDULER_TaskIdentifier die_task;
35 static GNUNET_SCHEDULER_TaskIdentifier consume_task;
37 static struct GNUNET_ATS_SchedulingHandle *ats;
39 static struct GNUNET_ATS_PerformanceHandle *atp;
41 struct GNUNET_ATS_ReservationContext *sh;
43 static struct GNUNET_OS_Process *arm_proc;
45 static struct PeerContext *p;
47 static uint32_t bw_in;
49 static uint32_t bw_out;
61 struct GNUNET_ATS_Information *ats;
69 struct GNUNET_PeerIdentity id;
78 if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
79 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
80 GNUNET_OS_process_wait (arm_proc);
81 GNUNET_OS_process_close (arm_proc);
87 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89 die_task = GNUNET_SCHEDULER_NO_TASK;
91 if (consume_task != GNUNET_SCHEDULER_NO_TASK)
93 GNUNET_SCHEDULER_cancel (consume_task);
94 consume_task = GNUNET_SCHEDULER_NO_TASK;
98 GNUNET_ATS_reserve_bandwidth_cancel (sh);
101 GNUNET_ATS_scheduling_done (ats);
103 GNUNET_ATS_performance_done (atp);
105 GNUNET_free (p->addr);
117 if (die_task != GNUNET_SCHEDULER_NO_TASK)
119 GNUNET_SCHEDULER_cancel (die_task);
120 die_task = GNUNET_SCHEDULER_NO_TASK;
123 if (consume_task != GNUNET_SCHEDULER_NO_TASK)
125 GNUNET_SCHEDULER_cancel (consume_task);
126 consume_task = GNUNET_SCHEDULER_NO_TASK;
129 GNUNET_ATS_scheduling_done (ats);
131 GNUNET_ATS_performance_done (atp);
133 GNUNET_free (p->addr);
142 performance_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
143 const char *plugin_name, const void *plugin_addr,
144 size_t plugin_addr_len,
145 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
146 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
147 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
153 reservation_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
154 int32_t amount, struct GNUNET_TIME_Relative res_delay)
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "ATS reserved bandwidth of %i to peer `%s' in %llu ms\n", amount,
159 GNUNET_i2s (peer), res_delay.rel_value);
163 consume_bandwidth (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 consume_task = GNUNET_SCHEDULER_NO_TASK;
166 int32_t to_reserve = 500;
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Trying to reserver bandwidth of %i to peer `%s' in %llu ms\n",
170 to_reserve, GNUNET_i2s (&p->id));
172 sh = GNUNET_ATS_reserve_bandwidth (atp, &p->id, to_reserve, &reservation_cb,
177 address_suggest_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
178 const char *plugin_name, const void *plugin_addr,
179 size_t plugin_addr_len, struct Session *session,
180 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
181 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
182 const struct GNUNET_ATS_Information *ats,
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggested address for peer `%s'\n",
188 bw_in = ntohl (bandwidth_in.value__);
189 bw_out = ntohl (bandwidth_out.value__);
191 consume_task = GNUNET_SCHEDULER_add_now (&consume_bandwidth, NULL);
195 start_arm (const char *cfgname)
198 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
199 "gnunet-service-arm",
200 "-c", cfgname, NULL);
204 check (void *cls, char *const *args, const char *cfgfile,
205 const struct GNUNET_CONFIGURATION_Handle *cfg)
208 struct Address *addr;
210 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
213 ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
221 p = GNUNET_malloc (sizeof (struct PeerContext));
222 addr = GNUNET_malloc (sizeof (struct Address));
224 atp = GNUNET_ATS_performance_init (cfg, &performance_cb, NULL);
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init ATS performance\n");
229 end_badly (NULL, NULL);
236 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
240 GNUNET_i2s (&p->id));
242 addr->plugin = "test";
243 addr->session = NULL;
247 GNUNET_ATS_address_update (ats, &p->id, addr->plugin, addr->addr,
248 addr->addr_len, addr->session, NULL, 0);
250 GNUNET_ATS_suggest_address (ats, &p->id);
254 main (int argc, char *argv[])
256 static char *const argv2[] = { "test_ats_api_bandwidth_consumption",
263 static struct GNUNET_GETOPT_CommandLineOption options[] = {
264 GNUNET_GETOPT_OPTION_END
267 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
268 "test_ats_api_bandwidth_consumption", "nohelp", options,
275 /* end of file test_ats_api_bandwidth_consumption.c */