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 VERBOSE GNUNET_EXTRA_LOGGING
33 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
37 static GNUNET_SCHEDULER_TaskIdentifier die_task;
39 static GNUNET_SCHEDULER_TaskIdentifier consume_task;
41 static struct GNUNET_ATS_SchedulingHandle *ats;
43 static struct GNUNET_ATS_PerformanceHandle *atp;
45 struct GNUNET_ATS_ReservationContext *sh;
47 static struct GNUNET_OS_Process *arm_proc;
49 static struct PeerContext *p;
51 static uint32_t bw_in;
53 static uint32_t bw_out;
65 struct GNUNET_ATS_Information *ats;
73 struct GNUNET_PeerIdentity id;
82 if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
83 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
84 GNUNET_OS_process_wait (arm_proc);
85 GNUNET_OS_process_close (arm_proc);
91 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 die_task = GNUNET_SCHEDULER_NO_TASK;
95 if (consume_task != GNUNET_SCHEDULER_NO_TASK)
97 GNUNET_SCHEDULER_cancel (consume_task);
98 consume_task = GNUNET_SCHEDULER_NO_TASK;
102 GNUNET_ATS_reserve_bandwidth_cancel (sh);
105 GNUNET_ATS_scheduling_done (ats);
107 GNUNET_ATS_performance_done (atp);
109 GNUNET_free (p->addr);
121 if (die_task != GNUNET_SCHEDULER_NO_TASK)
123 GNUNET_SCHEDULER_cancel (die_task);
124 die_task = GNUNET_SCHEDULER_NO_TASK;
127 if (consume_task != GNUNET_SCHEDULER_NO_TASK)
129 GNUNET_SCHEDULER_cancel (consume_task);
130 consume_task = GNUNET_SCHEDULER_NO_TASK;
133 GNUNET_ATS_scheduling_done (ats);
135 GNUNET_ATS_performance_done (atp);
137 GNUNET_free (p->addr);
146 performance_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
147 const char *plugin_name, const void *plugin_addr,
148 size_t plugin_addr_len,
149 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
150 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
151 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
157 reservation_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
158 int32_t amount, struct GNUNET_TIME_Relative res_delay)
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162 "ATS reserved bandwidth of %i to peer `%s' in %llu ms\n", amount,
163 GNUNET_i2s (peer), res_delay.rel_value);
167 consume_bandwidth (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 consume_task = GNUNET_SCHEDULER_NO_TASK;
170 int32_t to_reserve = 500;
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Trying to reserver bandwidth of %i to peer `%s' in %llu ms\n",
174 to_reserve, GNUNET_i2s (&p->id));
176 sh = GNUNET_ATS_reserve_bandwidth (atp, &p->id, to_reserve, &reservation_cb,
181 address_suggest_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
182 const char *plugin_name, const void *plugin_addr,
183 size_t plugin_addr_len, struct Session *session,
184 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
185 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
186 const struct GNUNET_ATS_Information *ats,
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggested address for peer `%s'\n",
192 bw_in = ntohl (bandwidth_in.value__);
193 bw_out = ntohl (bandwidth_out.value__);
195 consume_task = GNUNET_SCHEDULER_add_now (&consume_bandwidth, NULL);
199 start_arm (const char *cfgname)
202 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
203 "gnunet-service-arm",
207 "-c", cfgname, NULL);
211 check (void *cls, char *const *args, const char *cfgfile,
212 const struct GNUNET_CONFIGURATION_Handle *cfg)
215 struct Address *addr;
217 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
220 ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
228 p = GNUNET_malloc (sizeof (struct PeerContext));
229 addr = GNUNET_malloc (sizeof (struct Address));
231 atp = GNUNET_ATS_performance_init (cfg, &performance_cb, NULL);
235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init ATS performance\n");
236 end_badly (NULL, NULL);
243 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
247 GNUNET_i2s (&p->id));
249 addr->plugin = "test";
250 addr->session = NULL;
254 GNUNET_ATS_address_update (ats, &p->id, addr->plugin, addr->addr,
255 addr->addr_len, addr->session, NULL, 0);
257 GNUNET_ATS_suggest_address (ats, &p->id);
261 main (int argc, char *argv[])
263 static char *const argv2[] = { "test_ats_api_bandwidth_consumption",
274 static struct GNUNET_GETOPT_CommandLineOption options[] = {
275 GNUNET_GETOPT_OPTION_END
278 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
279 "test_ats_api_bandwidth_consumption", "nohelp", options,
286 /* end of file test_ats_api_bandwidth_consumption.c */