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"
29 #include "gnunet_testing_lib.h"
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
34 static GNUNET_SCHEDULER_TaskIdentifier die_task;
36 static GNUNET_SCHEDULER_TaskIdentifier consume_task;
38 static struct GNUNET_ATS_SchedulingHandle *ats;
40 static struct GNUNET_ATS_PerformanceHandle *atp;
42 struct GNUNET_ATS_ReservationContext *sh;
44 static struct PeerContext *p;
46 static uint32_t bw_in;
48 static uint32_t bw_out;
62 struct GNUNET_ATS_Information *ats;
71 struct GNUNET_PeerIdentity id;
78 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 die_task = GNUNET_SCHEDULER_NO_TASK;
81 if (consume_task != GNUNET_SCHEDULER_NO_TASK)
83 GNUNET_SCHEDULER_cancel (consume_task);
84 consume_task = GNUNET_SCHEDULER_NO_TASK;
87 GNUNET_ATS_reserve_bandwidth_cancel (sh);
89 GNUNET_ATS_scheduling_done (ats);
91 GNUNET_ATS_performance_done (atp);
92 GNUNET_free (p->addr);
101 if (die_task != GNUNET_SCHEDULER_NO_TASK)
103 GNUNET_SCHEDULER_cancel (die_task);
104 die_task = GNUNET_SCHEDULER_NO_TASK;
106 if (consume_task != GNUNET_SCHEDULER_NO_TASK)
108 GNUNET_SCHEDULER_cancel (consume_task);
109 consume_task = GNUNET_SCHEDULER_NO_TASK;
111 GNUNET_ATS_scheduling_done (ats);
112 GNUNET_ATS_performance_done (atp);
113 GNUNET_free (p->addr);
120 performance_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
121 const char *plugin_name, const void *plugin_addr,
122 size_t plugin_addr_len,
123 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
124 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
125 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
132 reservation_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
133 int32_t amount, struct GNUNET_TIME_Relative res_delay)
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "ATS reserved bandwidth of %i to peer `%s' in %llu ms\n", amount,
138 GNUNET_i2s (peer), res_delay.rel_value);
143 consume_bandwidth (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
145 consume_task = GNUNET_SCHEDULER_NO_TASK;
146 int32_t to_reserve = 500;
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Trying to reserver bandwidth of %i to peer `%s' in %llu ms\n",
150 to_reserve, GNUNET_i2s (&p->id));
152 sh = GNUNET_ATS_reserve_bandwidth (atp, &p->id, to_reserve, &reservation_cb,
158 address_suggest_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
159 const char *plugin_name, const void *plugin_addr,
160 size_t plugin_addr_len, struct Session *session,
161 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
162 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
163 const struct GNUNET_ATS_Information *ats,
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggested address for peer `%s'\n",
169 bw_in = ntohl (bandwidth_in.value__);
170 bw_out = ntohl (bandwidth_out.value__);
172 consume_task = GNUNET_SCHEDULER_add_now (&consume_bandwidth, NULL);
178 const struct GNUNET_CONFIGURATION_Handle *cfg,
179 struct GNUNET_TESTING_Peer *peer)
181 struct Address *addr;
184 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
185 ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
192 p = GNUNET_malloc (sizeof (struct PeerContext));
193 addr = GNUNET_malloc (sizeof (struct Address));
195 atp = GNUNET_ATS_performance_init (cfg, &performance_cb, NULL);
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init ATS performance\n");
200 end_badly (NULL, NULL);
207 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
211 GNUNET_i2s (&p->id));
213 addr->plugin = "test";
214 addr->session = NULL;
218 GNUNET_ATS_address_update (ats, &p->id, addr->plugin, addr->addr,
219 addr->addr_len, addr->session, NULL, 0);
221 GNUNET_ATS_suggest_address (ats, &p->id);
226 main (int argc, char *argv[])
228 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_bandwidth_consumption",
235 /* end of file test_ats_api_bandwidth_consumption.c */