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