(no commit message)
[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_YES
38
39 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
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 static int ret;
50
51 struct Address
52 {
53   char * plugin;
54   size_t plugin_len;
55
56   void * addr;
57   size_t addr_len;
58
59   struct GNUNET_TRANSPORT_ATS_Information * ats;
60   int ats_count;
61
62   void  *session;
63 };
64
65 struct PeerContext
66 {
67   struct GNUNET_PeerIdentity id;
68
69   struct Address * addr;
70 };
71
72
73 static void
74 stop_arm ()
75 {
76   if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
77     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
78   GNUNET_OS_process_wait (arm_proc);
79   GNUNET_OS_process_close (arm_proc);
80   arm_proc = NULL;
81 }
82
83
84 static void
85 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 {
87   die_task = GNUNET_SCHEDULER_NO_TASK;
88   if (ats != NULL)
89     GNUNET_ATS_scheduling_done (ats);
90
91   ret = GNUNET_SYSERR;
92
93   stop_arm ();
94 }
95
96
97 static void
98 end ()
99 {
100   if (die_task != GNUNET_SCHEDULER_NO_TASK)
101   {
102     GNUNET_SCHEDULER_cancel(die_task);
103     die_task = GNUNET_SCHEDULER_NO_TASK;
104   }
105
106   GNUNET_ATS_scheduling_done (ats);
107
108   ret = 0;
109
110   stop_arm ();
111 }
112
113
114 static void
115 address_suggest_cb (void *cls,
116                     const struct
117                     GNUNET_PeerIdentity *
118                     peer,
119                     const char *plugin_name,
120                     const void *plugin_addr,
121                     size_t plugin_addr_len,
122                     struct Session * session,
123                     struct
124                     GNUNET_BANDWIDTH_Value32NBO
125                     bandwidth_out,
126                     struct
127                     GNUNET_BANDWIDTH_Value32NBO
128                     bandwidth_in,
129                     const struct
130                     GNUNET_TRANSPORT_ATS_Information
131                     * ats,
132                     uint32_t ats_count)
133
134 {
135   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address_suggest_cb `%s'\n", GNUNET_i2s (peer));
136
137   end ();
138 }
139
140 void
141 start_arm (const char *cfgname)
142 {
143   arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
144                            "gnunet-service-arm",
145 #if VERBOSE_ARM
146                            "-L", "DEBUG",
147 #endif
148                            "-c", cfgname, NULL);
149 }
150
151 static void
152 check (void *cls, char *const *args, const char *cfgfile,
153        const struct GNUNET_CONFIGURATION_Handle *cfg)
154 {
155   ret = GNUNET_SYSERR;
156   struct Address addr;
157   struct PeerContext p;
158
159   die_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &end_badly, NULL);
160   start_arm (cfgfile);
161
162   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
163
164   if (ats == NULL)
165   {
166     ret = GNUNET_SYSERR;
167     end ();
168     return;
169   }
170
171   /* set up peer */
172   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p.id.hashPubKey);
173
174
175   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n", GNUNET_i2s (&p.id));
176   p.addr = &addr;
177   addr.plugin = "test";
178   addr.session = NULL;
179   addr.addr = NULL;
180   addr.addr_len = 0;
181
182   GNUNET_ATS_address_update(ats, &p.id, addr.plugin, addr.addr, addr.addr_len, addr.session, NULL, 0);
183
184   GNUNET_ATS_suggest_address(ats, &p.id);
185 }
186
187 int
188 main (int argc, char *argv[])
189 {
190   static char *const argv2[] = { "test_ats_api_scheduling",
191     "-c",
192     "test_ats_api.conf",
193 #if VERBOSE
194     "-L", "DEBUG",
195 #else
196     "-L", "WARNING",
197 #endif
198     NULL
199   };
200
201   static struct GNUNET_GETOPT_CommandLineOption options[] = {
202     GNUNET_GETOPT_OPTION_END
203   };
204
205   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
206                       "test_ats_api_scheduling", "nohelp", options, &check,
207                       NULL);
208
209
210   return ret;
211 }
212
213 /* end of file test_ats_api_scheduling.c */