document options provided
[oweals/gnunet.git] / src / ats / test_ats_api_bandwidth_consumption.c
1 /*
2      This file is part of GNUnet.
3      (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 GNUNET_SCHEDULER_TaskIdentifier die_task;
35
36 static GNUNET_SCHEDULER_TaskIdentifier consume_task;
37
38 static struct GNUNET_ATS_SchedulingHandle *ats;
39
40 static struct GNUNET_ATS_PerformanceHandle *atp;
41
42 struct GNUNET_ATS_ReservationContext *sh;
43
44 static struct PeerContext *p;
45
46 static uint32_t bw_in;
47
48 static uint32_t bw_out;
49
50 static int ret;
51
52 struct Address
53 {
54   char *plugin;
55
56   size_t plugin_len;
57
58   void *addr;
59
60   size_t addr_len;
61
62   struct GNUNET_ATS_Information *ats;
63
64   int ats_count;
65
66   void *session;
67 };
68
69 struct PeerContext
70 {
71   struct GNUNET_PeerIdentity id;
72
73   struct Address *addr;
74 };
75
76
77 static void
78 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 {
80   die_task = GNUNET_SCHEDULER_NO_TASK;
81   if (consume_task != GNUNET_SCHEDULER_NO_TASK)
82   {
83     GNUNET_SCHEDULER_cancel (consume_task);
84     consume_task = GNUNET_SCHEDULER_NO_TASK;
85   }
86   if (sh != NULL)
87     GNUNET_ATS_reserve_bandwidth_cancel (sh);
88   if (ats != NULL)
89     GNUNET_ATS_scheduling_done (ats);
90   if (atp != NULL)
91     GNUNET_ATS_performance_done (atp);
92   GNUNET_free (p->addr);
93   GNUNET_free (p);
94   ret = GNUNET_SYSERR;
95 }
96
97
98 static void
99 end ()
100 {
101   if (die_task != GNUNET_SCHEDULER_NO_TASK)
102   {
103     GNUNET_SCHEDULER_cancel (die_task);
104     die_task = GNUNET_SCHEDULER_NO_TASK;
105   }
106   if (consume_task != GNUNET_SCHEDULER_NO_TASK)
107   {
108     GNUNET_SCHEDULER_cancel (consume_task);
109     consume_task = GNUNET_SCHEDULER_NO_TASK;
110   }
111   GNUNET_ATS_scheduling_done (ats);
112   GNUNET_ATS_performance_done (atp);
113   GNUNET_free (p->addr);
114   GNUNET_free (p);
115   ret = 0;
116 }
117
118
119 static void
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)
126 {
127
128 }
129
130
131 static void
132 reservation_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
133                 int32_t amount, struct GNUNET_TIME_Relative res_delay)
134 {
135   sh = NULL;
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);
139 }
140
141
142 static void
143 consume_bandwidth (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 {
145   consume_task = GNUNET_SCHEDULER_NO_TASK;
146   int32_t to_reserve = 500;
147
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));
151
152   sh = GNUNET_ATS_reserve_bandwidth (atp, &p->id, to_reserve, &reservation_cb,
153                                      NULL);
154 }
155
156
157 static void
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,
164                     uint32_t ats_count)
165 {
166   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggested address for peer `%s'\n",
167               GNUNET_i2s (peer));
168
169   bw_in = ntohl (bandwidth_in.value__);
170   bw_out = ntohl (bandwidth_out.value__);
171
172   consume_task = GNUNET_SCHEDULER_add_now (&consume_bandwidth, NULL);
173 }
174
175
176 static void
177 run (void *cls,
178      const struct GNUNET_CONFIGURATION_Handle *cfg,
179      struct GNUNET_TESTING_Peer *peer)
180 {
181   struct Address *addr;
182
183   ret = GNUNET_SYSERR;
184   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
185   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
186   if (ats == NULL)
187   {
188     ret = GNUNET_SYSERR;
189     end ();
190     return;
191   }
192   p = GNUNET_malloc (sizeof (struct PeerContext));
193   addr = GNUNET_malloc (sizeof (struct Address));
194
195   atp = GNUNET_ATS_performance_init (cfg, &performance_cb, NULL);
196   if (atp == NULL)
197   {
198     ret = GNUNET_SYSERR;
199     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init ATS performance\n");
200     end_badly (NULL, NULL);
201     GNUNET_free (p);
202     GNUNET_free (addr);
203     return;
204   }
205
206   /* set up peer */
207   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
208                                     &p->id.hashPubKey);
209
210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
211               GNUNET_i2s (&p->id));
212   p->addr = addr;
213   addr->plugin = "test";
214   addr->session = NULL;
215   addr->addr = NULL;
216   addr->addr_len = 0;
217
218   GNUNET_ATS_address_update (ats, &p->id, addr->plugin, addr->addr,
219                              addr->addr_len, addr->session, NULL, 0);
220
221   GNUNET_ATS_suggest_address (ats, &p->id);
222 }
223
224
225 int
226 main (int argc, char *argv[])
227 {
228   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_bandwidth_consumption",
229                                     "test_ats_api.conf",
230                                     &run, NULL))
231     return 1;
232   return ret;
233 }
234
235 /* end of file test_ats_api_bandwidth_consumption.c */