2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file ats/test_ats_api_scheduling.c
22 * @brief test automatic transport selection scheduling API
23 * @author Christian Grothoff
24 * @author Matthias Wachs
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 (!)
34 #include "gnunet_ats_service.h"
37 #define VERBOSE GNUNET_EXTRA_LOGGING
39 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
43 static GNUNET_SCHEDULER_TaskIdentifier die_task;
45 static struct GNUNET_ATS_SchedulingHandle *ats;
47 struct GNUNET_OS_Process *arm_proc;
61 struct GNUNET_ATS_Information *ats;
69 struct GNUNET_PeerIdentity id;
74 struct Address addr[2];
75 struct PeerContext p[2];
76 struct GNUNET_ATS_Information atsi[2];
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);
90 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 die_task = GNUNET_SCHEDULER_NO_TASK;
94 GNUNET_ATS_scheduling_done (ats);
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
106 if (die_task != GNUNET_SCHEDULER_NO_TASK)
108 GNUNET_SCHEDULER_cancel (die_task);
109 die_task = GNUNET_SCHEDULER_NO_TASK;
112 GNUNET_ATS_scheduling_done (ats);
121 address_suggest_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
122 const char *plugin_name, const void *plugin_addr,
123 size_t plugin_addr_len, struct Session *session,
124 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
125 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
126 const struct GNUNET_ATS_Information *ats,
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggests address `%s'\n",
133 memcmp (peer, &p[0].id, sizeof (struct GNUNET_PeerIdentity)));
134 GNUNET_assert (0 == strcmp (plugin_name, addr[0].plugin));
135 GNUNET_assert (plugin_addr_len == addr[0].addr_len);
136 GNUNET_assert (0 == memcmp (plugin_addr, addr[0].plugin, plugin_addr_len));
137 GNUNET_assert (addr[0].session == session);
141 * GNUNET_assert (ats_count == 2);
142 * GNUNET_assert (atsi[0].type == htons (1));
143 * GNUNET_assert (atsi[0].type == htons (2));
144 * GNUNET_assert (atsi[1].type == htons (2));
145 * GNUNET_assert (atsi[1].type == htons (2));
150 GNUNET_SCHEDULER_add_now (&end, NULL);
154 start_arm (const char *cfgname)
157 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
158 "gnunet-service-arm",
162 "-c", cfgname, NULL);
166 check (void *cls, char *const *args, const char *cfgfile,
167 const struct GNUNET_CONFIGURATION_Handle *cfg)
171 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
174 ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
184 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
185 &p[0].id.hashPubKey);
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
187 GNUNET_i2s (&p[0].id));
189 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
190 &p[1].id.hashPubKey);
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
192 GNUNET_i2s (&p[1].id));
194 addr[0].plugin = "test";
195 addr[0].session = NULL;
196 addr[0].addr = strdup ("test");
197 addr[0].addr_len = 4;
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing address creation\n");
201 GNUNET_ATS_address_update (ats, &p[0].id, addr[0].plugin, addr[0].addr,
202 addr[0].addr_len, addr[0].session, NULL, 0);
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing ATS info creation\n");
206 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP);
207 atsi[0].value = htonl (1024);
209 GNUNET_ATS_address_update (ats, &p[0].id, addr[0].plugin, addr[0].addr,
210 addr[0].addr_len, addr[0].session, atsi, 1);
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing ATS info update\n");
214 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP);
215 atsi[0].value = htonl (2048);
217 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_DOWN);
218 atsi[1].value = htonl (1024);
220 GNUNET_ATS_address_update (ats, &p[0].id, addr[0].plugin, addr[0].addr,
221 addr[0].addr_len, addr[0].session, atsi, 2);
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing manual address deletion \n");
224 GNUNET_ATS_address_update (ats, &p[1].id, addr[0].plugin, addr[0].addr,
225 addr[0].addr_len, addr[0].session, NULL, 0);
226 GNUNET_ATS_address_destroyed (ats, &p[1].id, addr[0].plugin, addr[0].addr,
227 addr[0].addr_len, addr[0].session);
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting peer `%s'\n",
230 GNUNET_i2s (&p[0].id));
231 GNUNET_ATS_suggest_address (ats, &p[0].id);
235 main (int argc, char *argv[])
237 static char *const argv2[] = { "test_ats_api_scheduling",
248 static struct GNUNET_GETOPT_CommandLineOption options[] = {
249 GNUNET_GETOPT_OPTION_END
252 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
253 "test_ats_api_scheduling", "nohelp", options, &check,
260 /* end of file test_ats_api_scheduling.c */