- added benchmarking for updates
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling.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_scheduling.c
22  * @brief test automatic transport selection scheduling API
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  *
26  * TODO:
27  * - write test case
28  * - extend API to get performance data
29  * - implement simplistic strategy based on say 'lowest latency' or strict ordering
30  * - extend API to get peer preferences, implement proportional bandwidth assignment
31  * - re-implement API against a real ATS service (!)
32  */
33 #include "platform.h"
34 #include "gnunet_ats_service.h"
35 #include "ats.h"
36
37 #define VERBOSE GNUNET_NO
38
39 #define VERBOSE_ARM GNUNET_NO
40
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
42
43 static GNUNET_SCHEDULER_TaskIdentifier die_task;
44
45 static struct GNUNET_ATS_SchedulingHandle *ats;
46
47 struct GNUNET_OS_Process *arm_proc;
48
49
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 struct Address addr[2];
75 struct PeerContext p[2];
76 struct GNUNET_ATS_Information atsi[2];
77
78 static void
79 stop_arm ()
80 {
81   if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
82     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
83   GNUNET_OS_process_wait (arm_proc);
84   GNUNET_OS_process_close (arm_proc);
85   arm_proc = NULL;
86 }
87
88
89 static void
90 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92   die_task = GNUNET_SCHEDULER_NO_TASK;
93   if (ats != NULL)
94     GNUNET_ATS_scheduling_done (ats);
95
96   ret = GNUNET_SYSERR;
97
98   stop_arm ();
99 }
100
101
102 static void
103 end ()
104 {
105   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
106   if (die_task != GNUNET_SCHEDULER_NO_TASK)
107   {
108     GNUNET_SCHEDULER_cancel (die_task);
109     die_task = GNUNET_SCHEDULER_NO_TASK;
110   }
111
112   GNUNET_ATS_scheduling_done (ats);
113
114   ret = 0;
115
116   stop_arm ();
117 }
118
119
120 static void
121 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
122                     struct Session *session,
123                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
124                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
125                     const struct GNUNET_ATS_Information *ats,
126                     uint32_t ats_count)
127 {
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggests address `%s'\n",
129               GNUNET_i2s (&address->peer));
130
131   GNUNET_assert (0 ==
132                  memcmp (&address->peer, &p[0].id,
133                          sizeof (struct GNUNET_PeerIdentity)));
134   GNUNET_assert (0 == strcmp (address->transport_name, addr[0].plugin));
135   GNUNET_assert (address->address_length == addr[0].addr_len);
136   GNUNET_assert (0 ==
137                  memcmp (address->address, addr[0].plugin,
138                          address->address_length));
139   GNUNET_assert (addr[0].session == session);
140
141
142   /* TODO ats merge
143    * GNUNET_assert (ats_count == 2);
144    * GNUNET_assert (atsi[0].type == htons (1));
145    * GNUNET_assert (atsi[0].type == htons (2));
146    * GNUNET_assert (atsi[1].type == htons (2));
147    * GNUNET_assert (atsi[1].type == htons (2));
148    */
149
150   ret = 0;
151
152   GNUNET_SCHEDULER_add_now (&end, NULL);
153 }
154
155 void
156 start_arm (const char *cfgname)
157 {
158   arm_proc =
159     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
160                                "gnunet-service-arm",
161 #if VERBOSE_ARM
162                                "-L", "DEBUG",
163 #endif
164                                "-c", cfgname, NULL);
165 }
166
167 static void
168 check (void *cls, char *const *args, const char *cfgfile,
169        const struct GNUNET_CONFIGURATION_Handle *cfg)
170 {
171   struct GNUNET_HELLO_Address address0;
172
173   ret = GNUNET_SYSERR;
174
175   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
176   start_arm (cfgfile);
177
178   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
179
180   if (ats == NULL)
181   {
182     ret = GNUNET_SYSERR;
183     end ();
184     return;
185   }
186
187   /* set up peer */
188   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
189                                     &p[0].id.hashPubKey);
190   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
191               GNUNET_i2s (&p[0].id));
192
193   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
194                                     &p[1].id.hashPubKey);
195   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
196               GNUNET_i2s (&p[1].id));
197
198   addr[0].plugin = "test";
199   addr[0].session = NULL;
200   addr[0].addr = GNUNET_strdup ("test");
201   addr[0].addr_len = 4;
202
203   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing address creation\n");
204
205   address0.peer = p[0].id;
206   address0.transport_name = addr[0].plugin;
207   address0.address = addr[0].addr;
208   address0.address_length = addr[0].addr_len;
209   GNUNET_ATS_address_update (ats, &address0, addr[0].session, NULL, 0);
210
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing ATS info creation\n");
212
213   atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP);
214   atsi[0].value = htonl (1024);
215
216   GNUNET_ATS_address_update (ats, &address0, addr[0].session, atsi, 1);
217
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing ATS info update\n");
219
220   atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP);
221   atsi[0].value = htonl (2048);
222
223   atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_DOWN);
224   atsi[1].value = htonl (1024);
225
226   GNUNET_ATS_address_update (ats, &address0, addr[0].session, atsi, 2);
227
228   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing manual address deletion \n");
229   address0.peer = p[1].id;      // FIXME: why? typo in old code?
230   GNUNET_ATS_address_update (ats, &address0, addr[0].session, NULL, 0);
231   GNUNET_ATS_address_destroyed (ats, &address0, addr[0].session);
232
233   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting peer `%s'\n",
234               GNUNET_i2s (&p[0].id));
235   GNUNET_ATS_suggest_address (ats, &p[0].id);
236 }
237
238 int
239 main (int argc, char *argv[])
240 {
241   static char *const argv2[] = { "test_ats_api_scheduling",
242     "-c",
243     "test_ats_api.conf",
244 #if VERBOSE
245     "-L", "DEBUG",
246 #else
247     "-L", "WARNING",
248 #endif
249     NULL
250   };
251
252   static struct GNUNET_GETOPT_CommandLineOption options[] = {
253     GNUNET_GETOPT_OPTION_END
254   };
255
256   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
257                       "test_ats_api_scheduling", "nohelp", options, &check,
258                       NULL);
259
260
261   return ret;
262 }
263
264 /* end of file test_ats_api_scheduling.c */