(no commit message)
[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 "ats.h"
30
31 #define VERBOSE GNUNET_EXTRA_LOGGING
32
33 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
34
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
36
37 static GNUNET_SCHEDULER_TaskIdentifier die_task;
38
39 static GNUNET_SCHEDULER_TaskIdentifier consume_task;
40
41 static struct GNUNET_ATS_SchedulingHandle *ats;
42
43 static struct GNUNET_ATS_PerformanceHandle *atp;
44
45 struct GNUNET_ATS_ReservationContext *sh;
46
47 static struct GNUNET_OS_Process * arm_proc;
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   size_t plugin_len;
61
62   void * addr;
63   size_t addr_len;
64
65   struct GNUNET_TRANSPORT_ATS_Information * ats;
66   int ats_count;
67
68   void  *session;
69 };
70
71 struct PeerContext
72 {
73   struct GNUNET_PeerIdentity id;
74
75   struct Address * addr;
76 };
77
78
79 static void
80 stop_arm ()
81 {
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);
86   arm_proc = NULL;
87 }
88
89
90 static void
91 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 {
93   die_task = GNUNET_SCHEDULER_NO_TASK;
94
95   if (consume_task != GNUNET_SCHEDULER_NO_TASK)
96   {
97     GNUNET_SCHEDULER_cancel(consume_task);
98     consume_task = GNUNET_SCHEDULER_NO_TASK;
99   }
100
101   if (sh != NULL)
102     GNUNET_ATS_reserve_bandwidth_cancel(sh);
103
104   if (ats != NULL)
105     GNUNET_ATS_scheduling_done (ats);
106   if (atp != NULL)
107     GNUNET_ATS_performance_done (atp);
108
109   GNUNET_free (p->addr);
110   GNUNET_free (p);
111
112   ret = GNUNET_SYSERR;
113
114   stop_arm ();
115 }
116
117
118 static void
119 end ()
120 {
121   if (die_task != GNUNET_SCHEDULER_NO_TASK)
122   {
123     GNUNET_SCHEDULER_cancel(die_task);
124     die_task = GNUNET_SCHEDULER_NO_TASK;
125   }
126
127   if (consume_task != GNUNET_SCHEDULER_NO_TASK)
128   {
129     GNUNET_SCHEDULER_cancel(consume_task);
130     consume_task = GNUNET_SCHEDULER_NO_TASK;
131   }
132
133   GNUNET_ATS_scheduling_done (ats);
134
135   GNUNET_ATS_performance_done (atp);
136
137   GNUNET_free (p->addr);
138   GNUNET_free (p);
139
140   ret = 0;
141
142   stop_arm ();
143 }
144
145 void performance_cb (void *cls,
146                     const struct
147                     GNUNET_PeerIdentity *
148                     peer,
149                     const char *plugin_name,
150                     const void *plugin_addr,
151                     size_t plugin_addr_len,
152                     struct
153                     GNUNET_BANDWIDTH_Value32NBO
154                     bandwidth_out,
155                     struct
156                     GNUNET_BANDWIDTH_Value32NBO
157                     bandwidth_in,
158                     const struct
159                     GNUNET_TRANSPORT_ATS_Information
160                     * ats,
161                     uint32_t ats_count)
162 {
163
164 }
165
166 void reservation_cb (void *cls,
167                     const struct
168                     GNUNET_PeerIdentity *
169                     peer,
170                     int32_t amount,
171                     struct
172                     GNUNET_TIME_Relative
173                     res_delay)
174 {
175   sh = NULL;
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS reserved bandwidth of %i to peer `%s' in %llu ms\n",
177       amount,
178       GNUNET_i2s (peer),
179       res_delay.rel_value);
180 }
181
182 static void
183 consume_bandwidth (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
184 {
185   consume_task = GNUNET_SCHEDULER_NO_TASK;
186   int32_t to_reserve = 500;
187   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to reserver bandwidth of %i to peer `%s' in %llu ms\n",
188       to_reserve,
189       GNUNET_i2s (&p->id));
190
191   sh = GNUNET_ATS_reserve_bandwidth (atp, &p->id, to_reserve, &reservation_cb, NULL);
192 }
193
194 static void
195 address_suggest_cb (void *cls,
196                     const struct
197                     GNUNET_PeerIdentity *
198                     peer,
199                     const char *plugin_name,
200                     const void *plugin_addr,
201                     size_t plugin_addr_len,
202                     struct Session * session,
203                     struct
204                     GNUNET_BANDWIDTH_Value32NBO
205                     bandwidth_out,
206                     struct
207                     GNUNET_BANDWIDTH_Value32NBO
208                     bandwidth_in,
209                     const struct
210                     GNUNET_TRANSPORT_ATS_Information
211                     * ats,
212                     uint32_t ats_count)
213
214 {
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggested address for peer `%s'\n", GNUNET_i2s (peer));
216
217   bw_in = ntohl (bandwidth_in.value__);
218   bw_out = ntohl (bandwidth_out.value__);
219
220   consume_task = GNUNET_SCHEDULER_add_now(&consume_bandwidth, NULL);
221 }
222
223 void
224 start_arm (const char *cfgname)
225 {
226   arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
227                            "gnunet-service-arm",
228 #if VERBOSE_ARM
229                            "-L", "DEBUG",
230 #endif
231                            "-c", cfgname, NULL);
232 }
233
234 static void
235 check (void *cls, char *const *args, const char *cfgfile,
236        const struct GNUNET_CONFIGURATION_Handle *cfg)
237 {
238   ret = GNUNET_SYSERR;
239   struct Address *addr;
240
241   die_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &end_badly, NULL);
242   start_arm (cfgfile);
243
244   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
245   if (ats == NULL)
246   {
247     ret = GNUNET_SYSERR;
248     end ();
249     return;
250   }
251
252   p = GNUNET_malloc (sizeof (struct PeerContext));
253   addr = GNUNET_malloc (sizeof (struct Address));
254
255   atp = GNUNET_ATS_performance_init (cfg, &performance_cb, NULL);
256   if (atp == NULL)
257   {
258     ret = GNUNET_SYSERR;
259     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to init ATS performance\n");
260     end_badly(NULL, NULL);
261     return;
262   }
263
264   /* set up peer */
265   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p->id.hashPubKey);
266
267   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n", GNUNET_i2s (&p->id));
268   p->addr = addr;
269   addr->plugin = "test";
270   addr->session = NULL;
271   addr->addr = NULL;
272   addr->addr_len = 0;
273
274   GNUNET_ATS_address_update(ats, &p->id, addr->plugin, addr->addr, addr->addr_len, addr->session, NULL, 0);
275
276   GNUNET_ATS_suggest_address(ats, &p->id);
277 }
278
279 int
280 main (int argc, char *argv[])
281 {
282   static char *const argv2[] = { "test_ats_api_bandwidth_consumption",
283     "-c",
284     "test_ats_api.conf",
285 #if VERBOSE
286     "-L", "DEBUG",
287 #else
288     "-L", "WARNING",
289 #endif
290     NULL
291   };
292
293   static struct GNUNET_GETOPT_CommandLineOption options[] = {
294     GNUNET_GETOPT_OPTION_END
295   };
296
297   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
298                       "test_ats_api_bandwidth_consumption", "nohelp", options, &check,
299                       NULL);
300
301
302   return ret;
303 }
304
305 /* end of file test_ats_api_bandwidth_consumption.c */