-fix config, shutdown issue
[oweals/gnunet.git] / src / ats / test_ats_api_bandwidth_consumption.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
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
25
26  */
27 #include "platform.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_testing_lib.h"
30 #include "ats.h"
31
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33
34 static struct GNUNET_SCHEDULER_Task * die_task;
35
36 static struct GNUNET_SCHEDULER_Task * consume_task;
37
38 static struct GNUNET_ATS_SchedulingHandle *ats;
39
40 static struct GNUNET_ATS_PerformanceHandle *atp;
41
42 /**
43  * Connectivity handle
44  */
45 static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
46
47 static struct GNUNET_ATS_ReservationContext *sh;
48
49 static struct PeerContext *p;
50
51 static uint32_t bw_in;
52
53 static uint32_t bw_out;
54
55 static int ret;
56
57 struct Address
58 {
59   char *plugin;
60
61   size_t plugin_len;
62
63   void *addr;
64
65   size_t addr_len;
66
67   struct GNUNET_ATS_Information *ats;
68
69   int ats_count;
70
71   void *session;
72 };
73
74 struct PeerContext
75 {
76   struct GNUNET_PeerIdentity id;
77
78   struct Address *addr;
79 };
80
81
82 static void
83 end ()
84 {
85   if (NULL != connect_ats)
86   {
87     GNUNET_ATS_connectivity_done (connect_ats);
88     connect_ats = NULL;
89   }
90   if (die_task != NULL)
91   {
92     GNUNET_SCHEDULER_cancel (die_task);
93     die_task = NULL;
94   }
95   if (consume_task != NULL)
96   {
97     GNUNET_SCHEDULER_cancel (consume_task);
98     consume_task = NULL;
99   }
100   if (sh != NULL)
101   {
102     GNUNET_ATS_reserve_bandwidth_cancel (sh);
103     sh = NULL;
104   }
105   if (ats != NULL)
106   {
107     GNUNET_ATS_scheduling_done (ats);
108     ats = NULL;
109   }
110   if (atp != NULL)
111   {
112     GNUNET_ATS_performance_done (atp);
113     atp = NULL;
114   }
115   GNUNET_free (p->addr);
116   GNUNET_free (p);
117   p = NULL;
118   ret = 0;
119 }
120
121
122 static void
123 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
124 {
125   die_task = NULL;
126   end ();
127   ret = GNUNET_SYSERR;
128 }
129
130
131 static void
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)
138 {
139
140 }
141
142
143 static void
144 reservation_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
145                 int32_t amount, struct GNUNET_TIME_Relative res_delay)
146 {
147   sh = NULL;
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);
151 }
152
153
154 static void
155 consume_bandwidth (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
156 {
157   consume_task = NULL;
158   int32_t to_reserve = 500;
159
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));
163
164   sh = GNUNET_ATS_reserve_bandwidth (atp, &p->id, to_reserve, &reservation_cb,
165                                      NULL);
166 }
167
168
169 static void
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,
176                     uint32_t ats_count)
177 {
178   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggested address for peer `%s'\n",
179               GNUNET_i2s (peer));
180
181   bw_in = ntohl (bandwidth_in.value__);
182   bw_out = ntohl (bandwidth_out.value__);
183
184   consume_task = GNUNET_SCHEDULER_add_now (&consume_bandwidth, NULL);
185 }
186
187
188 static void
189 run (void *cls,
190      const struct GNUNET_CONFIGURATION_Handle *cfg,
191      struct GNUNET_TESTING_Peer *peer)
192 {
193   struct Address *addr;
194
195   ret = GNUNET_SYSERR;
196   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
197   connect_ats = GNUNET_ATS_connectivity_init (mycfg);
198
199   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
200   if (ats == NULL)
201   {
202     ret = GNUNET_SYSERR;
203     end ();
204     return;
205   }
206   p = GNUNET_new (struct PeerContext);
207   addr = GNUNET_new (struct Address);
208
209   atp = GNUNET_ATS_performance_init (cfg, NULL, NULL);
210   if (atp == NULL)
211   {
212     ret = GNUNET_SYSERR;
213     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init ATS performance\n");
214     end_badly (NULL, NULL);
215     GNUNET_free (p);
216     GNUNET_free (addr);
217     return;
218   }
219
220   /* set up peer */
221   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
222                                     &p->id.hashPubKey);
223
224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
225               GNUNET_i2s (&p->id));
226   p->addr = addr;
227   addr->plugin = "test";
228   addr->session = NULL;
229   addr->addr = NULL;
230   addr->addr_len = 0;
231
232   GNUNET_ATS_address_update (ats, &p->id, addr->plugin, addr->addr,
233                              addr->addr_len, addr->session, NULL, 0);
234
235   GNUNET_ATS_connectivity_suggest (connect_ats, &p->id);
236 }
237
238
239 int
240 main (int argc, char *argv[])
241 {
242   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_bandwidth_consumption",
243                                     "test_ats_api.conf",
244                                     &run, NULL))
245     return 1;
246   return ret;
247 }
248
249 /* end of file test_ats_api_bandwidth_consumption.c */