- adding GNUNET_ATS_address_add functionality .. no changes to scheduling API yet
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_add_address.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 TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
38
39 static GNUNET_SCHEDULER_TaskIdentifier die_task;
40
41 static struct GNUNET_ATS_SchedulingHandle *ats;
42
43 struct GNUNET_OS_Process *arm_proc;
44
45
46
47 static int ret;
48
49 struct Address
50 {
51   char *plugin;
52   size_t plugin_len;
53
54   void *addr;
55   size_t addr_len;
56
57   struct GNUNET_ATS_Information *ats;
58   int ats_count;
59
60   void *session;
61 };
62
63 struct PeerContext
64 {
65   struct GNUNET_PeerIdentity id;
66
67   struct Address *addr;
68 };
69
70 struct Address addr;
71 struct PeerContext p;
72 struct GNUNET_ATS_Information atsi;
73
74 static void
75 stop_arm ()
76 {
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);
81   arm_proc = NULL;
82 }
83
84
85 static void
86 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 {
88   die_task = GNUNET_SCHEDULER_NO_TASK;
89   if (ats != NULL)
90     GNUNET_ATS_scheduling_done (ats);
91
92   ret = GNUNET_SYSERR;
93
94   stop_arm ();
95 }
96
97
98 static void
99 end ()
100 {
101   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
102   if (die_task != GNUNET_SCHEDULER_NO_TASK)
103   {
104     GNUNET_SCHEDULER_cancel (die_task);
105     die_task = GNUNET_SCHEDULER_NO_TASK;
106   }
107
108   GNUNET_ATS_scheduling_done (ats);
109
110   ret = 0;
111
112   stop_arm ();
113 }
114
115
116 static void
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,
122                     uint32_t ats_count)
123 {
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggests address `%s'\n",
125               GNUNET_i2s (&address->peer));
126
127   GNUNET_assert (0 ==
128                  memcmp (&address->peer, &p.id,
129                          sizeof (struct GNUNET_PeerIdentity)));
130   GNUNET_assert (0 == strcmp (address->transport_name, addr.plugin));
131   GNUNET_assert (address->address_length == addr.addr_len);
132   GNUNET_assert (0 ==
133                  memcmp (address->address, addr.plugin,
134                          address->address_length));
135   GNUNET_assert (addr.session == session);
136
137   ret = 0;
138
139   GNUNET_SCHEDULER_add_now (&end, NULL);
140 }
141
142 void
143 start_arm (const char *cfgname)
144 {
145   arm_proc =
146     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
147                                "gnunet-service-arm",
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   struct GNUNET_HELLO_Address address0;
156
157   ret = GNUNET_SYSERR;
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,
173                                     &p.id.hashPubKey);
174   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
175               GNUNET_i2s (&p.id));
176
177   addr.plugin = "test";
178   addr.session = NULL;
179   addr.addr = GNUNET_strdup ("test");
180   addr.addr_len = 4;
181
182   /* Adding address without session */
183   address0.peer = p.id;
184   address0.transport_name = addr.plugin;
185   address0.address = addr.addr;
186   address0.address_length = addr.addr_len;
187   GNUNET_ATS_address_add (ats, &address0, addr.session, NULL, 0);
188
189   addr.session = &addr;
190   /* Update address with session */
191   GNUNET_ATS_address_add (ats, &address0, addr.session, NULL, 0);
192
193   /* Update address with session */
194   addr.session = &address0;
195   GNUNET_assert (GNUNET_OK == GNUNET_ATS_address_add (ats, &address0, addr.session, NULL, 0));
196   GNUNET_log_skip (2, GNUNET_NO);
197   GNUNET_assert (GNUNET_SYSERR == GNUNET_ATS_address_add (ats, &address0, addr.session, NULL, 0));
198
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting peer `%s'\n",
200               GNUNET_i2s (&p.id));
201   GNUNET_ATS_suggest_address (ats, &p.id);
202 }
203
204 int
205 main (int argc, char *argv[])
206 {
207   static char *const argv2[] = { "test_ats_api_scheduling_add_address",
208     "-c",
209     "test_ats_api.conf",
210     "-L", "WARNING",
211     NULL
212   };
213
214   static struct GNUNET_GETOPT_CommandLineOption options[] = {
215     GNUNET_GETOPT_OPTION_END
216   };
217
218   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
219                       "test_ats_api_scheduling_add_address", "nohelp", options, &check,
220                       NULL);
221
222
223   return ret;
224 }
225
226 /* end of file test_ats_api_scheduling.c */