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 TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
39 static GNUNET_SCHEDULER_TaskIdentifier die_task;
41 static struct GNUNET_ATS_SchedulingHandle *ats;
43 struct GNUNET_OS_Process *arm_proc;
57 struct GNUNET_ATS_Information *ats;
65 struct GNUNET_PeerIdentity id;
70 struct Address addr[2];
71 struct PeerContext p[2];
72 struct GNUNET_ATS_Information atsi[2];
77 if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
78 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
79 GNUNET_OS_process_wait (arm_proc);
80 GNUNET_OS_process_destroy (arm_proc);
86 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 die_task = GNUNET_SCHEDULER_NO_TASK;
90 GNUNET_ATS_scheduling_done (ats);
101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
102 if (die_task != GNUNET_SCHEDULER_NO_TASK)
104 GNUNET_SCHEDULER_cancel (die_task);
105 die_task = GNUNET_SCHEDULER_NO_TASK;
108 GNUNET_ATS_scheduling_done (ats);
117 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
118 struct Session *session,
119 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
120 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
121 const struct GNUNET_ATS_Information *ats,
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggests address `%s'\n",
125 GNUNET_i2s (&address->peer));
128 memcmp (&address->peer, &p[0].id,
129 sizeof (struct GNUNET_PeerIdentity)));
130 GNUNET_assert (0 == strcmp (address->transport_name, addr[0].plugin));
131 GNUNET_assert (address->address_length == addr[0].addr_len);
133 memcmp (address->address, addr[0].plugin,
134 address->address_length));
135 GNUNET_assert (addr[0].session == session);
139 GNUNET_SCHEDULER_add_now (&end, NULL);
143 start_arm (const char *cfgname)
146 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
147 "gnunet-service-arm",
148 "-c", cfgname, NULL);
152 check (void *cls, char *const *args, const char *cfgfile,
153 const struct GNUNET_CONFIGURATION_Handle *cfg)
155 struct GNUNET_HELLO_Address address0;
159 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
162 ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
172 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
173 &p[0].id.hashPubKey);
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
175 GNUNET_i2s (&p[0].id));
177 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
178 &p[1].id.hashPubKey);
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
180 GNUNET_i2s (&p[1].id));
182 addr[0].plugin = "test";
183 addr[0].session = NULL;
184 addr[0].addr = GNUNET_strdup ("test");
185 addr[0].addr_len = 4;
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing address creation\n");
189 address0.peer = p[0].id;
190 address0.transport_name = addr[0].plugin;
191 address0.address = addr[0].addr;
192 address0.address_length = addr[0].addr_len;
193 GNUNET_ATS_address_add (ats, &address0, addr[0].session, NULL, 0);
194 GNUNET_ATS_address_update (ats, &address0, addr[0].session, NULL, 0);
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing ATS info creation\n");
198 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP);
199 atsi[0].value = htonl (1024);
201 GNUNET_ATS_address_update (ats, &address0, addr[0].session, atsi, 1);
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing ATS info update\n");
205 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP);
206 atsi[0].value = htonl (2048);
208 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_DOWN);
209 atsi[1].value = htonl (1024);
211 GNUNET_ATS_address_update (ats, &address0, addr[0].session, atsi, 2);
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting peer `%s'\n",
214 GNUNET_i2s (&p[0].id));
215 GNUNET_ATS_suggest_address (ats, &p[0].id);
219 main (int argc, char *argv[])
221 static char *const argv2[] = { "test_ats_api_scheduling",
228 static struct GNUNET_GETOPT_CommandLineOption options[] = {
229 GNUNET_GETOPT_OPTION_END
232 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
233 "test_ats_api_scheduling", "nohelp", options, &check,
240 /* end of file test_ats_api_scheduling.c */