2 This file is part of GNUnet.
3 Copyright (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"
29 #include "gnunet_testing_lib.h"
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
34 static struct GNUNET_SCHEDULER_Task * die_task;
36 static struct GNUNET_SCHEDULER_Task * consume_task;
38 static struct GNUNET_ATS_SchedulingHandle *ats;
40 static struct GNUNET_ATS_PerformanceHandle *atp;
45 static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
47 static struct GNUNET_ATS_ReservationContext *sh;
49 static struct PeerContext *p;
51 static uint32_t bw_in;
53 static uint32_t bw_out;
67 struct GNUNET_ATS_Information *ats;
76 struct GNUNET_PeerIdentity id;
85 if (NULL != connect_ats)
87 GNUNET_ATS_connectivity_done (connect_ats);
92 GNUNET_SCHEDULER_cancel (die_task);
95 if (consume_task != NULL)
97 GNUNET_SCHEDULER_cancel (consume_task);
102 GNUNET_ATS_reserve_bandwidth_cancel (sh);
107 GNUNET_ATS_scheduling_done (ats);
112 GNUNET_ATS_performance_done (atp);
115 GNUNET_free (p->addr);
123 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
132 performance_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
133 const char *plugin_name, const void *plugin_addr,
134 size_t plugin_addr_len,
135 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
136 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
137 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
144 reservation_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
145 int32_t amount, struct GNUNET_TIME_Relative res_delay)
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "ATS reserved bandwidth of %i to peer `%s' in %llu ms\n", amount,
150 GNUNET_i2s (peer), res_delay.rel_value);
155 consume_bandwidth (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 int32_t to_reserve = 500;
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "Trying to reserver bandwidth of %i to peer `%s' in %llu ms\n",
162 to_reserve, GNUNET_i2s (&p->id));
164 sh = GNUNET_ATS_reserve_bandwidth (atp, &p->id, to_reserve, &reservation_cb,
170 address_suggest_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
171 const char *plugin_name, const void *plugin_addr,
172 size_t plugin_addr_len, struct Session *session,
173 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
174 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
175 const struct GNUNET_ATS_Information *ats,
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggested address for peer `%s'\n",
181 bw_in = ntohl (bandwidth_in.value__);
182 bw_out = ntohl (bandwidth_out.value__);
184 consume_task = GNUNET_SCHEDULER_add_now (&consume_bandwidth, NULL);
190 const struct GNUNET_CONFIGURATION_Handle *cfg,
191 struct GNUNET_TESTING_Peer *peer)
193 struct Address *addr;
196 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
197 connect_ats = GNUNET_ATS_connectivity_init (mycfg);
199 ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
206 p = GNUNET_new (struct PeerContext);
207 addr = GNUNET_new (struct Address);
209 atp = GNUNET_ATS_performance_init (cfg, NULL, NULL);
213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init ATS performance\n");
214 end_badly (NULL, NULL);
221 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
225 GNUNET_i2s (&p->id));
227 addr->plugin = "test";
228 addr->session = NULL;
232 GNUNET_ATS_address_update (ats, &p->id, addr->plugin, addr->addr,
233 addr->addr_len, addr->session, NULL, 0);
235 GNUNET_ATS_connectivity_suggest (connect_ats, &p->id);
240 main (int argc, char *argv[])
242 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_bandwidth_consumption",
249 /* end of file test_ats_api_bandwidth_consumption.c */