-fixing
[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 "gnunet_testing_lib-new.h"
36 #include "ats.h"
37
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
39
40 static GNUNET_SCHEDULER_TaskIdentifier die_task;
41
42 static struct GNUNET_ATS_SchedulingHandle *ats;
43
44 static int ret;
45
46 struct Address
47 {
48   char *plugin;
49   size_t plugin_len;
50
51   void *addr;
52   size_t addr_len;
53
54   struct GNUNET_ATS_Information *ats;
55   int ats_count;
56
57   void *session;
58 };
59
60 struct PeerContext
61 {
62   struct GNUNET_PeerIdentity id;
63
64   struct Address *addr;
65 };
66
67 static struct Address test_addr[2];
68
69 static struct PeerContext p[2];
70
71 static struct GNUNET_ATS_Information atsi[2];
72
73
74 static void
75 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
76 {
77   die_task = GNUNET_SCHEDULER_NO_TASK;
78   if (ats != NULL)
79     GNUNET_ATS_scheduling_done (ats);
80   ret = GNUNET_SYSERR;
81 }
82
83
84 static void
85 end ()
86 {
87   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
88   if (die_task != GNUNET_SCHEDULER_NO_TASK)
89   {
90     GNUNET_SCHEDULER_cancel (die_task);
91     die_task = GNUNET_SCHEDULER_NO_TASK;
92   }
93
94   GNUNET_ATS_scheduling_done (ats);
95   ret = 0;
96 }
97
98
99 static void
100 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
101                     struct Session *session,
102                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
103                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
104                     const struct GNUNET_ATS_Information *ats,
105                     uint32_t ats_count)
106 {
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS suggests address `%s'\n",
108               GNUNET_i2s (&address->peer));
109
110   GNUNET_assert (0 ==
111                  memcmp (&address->peer, &p[0].id,
112                          sizeof (struct GNUNET_PeerIdentity)));
113   GNUNET_assert (0 == strcmp (address->transport_name, test_addr[0].plugin));
114   GNUNET_assert (address->address_length == test_addr[0].addr_len);
115   GNUNET_assert (0 ==
116                  memcmp (address->address, test_addr[0].plugin,
117                          address->address_length));
118   GNUNET_assert (test_addr[0].session == session);
119
120   ret = 0;
121
122   GNUNET_SCHEDULER_add_now (&end, NULL);
123 }
124
125
126 static void
127 run (void *cls, 
128      const struct GNUNET_CONFIGURATION_Handle *cfg,
129      struct GNUNET_TESTING_Peer *peer)
130 {
131   struct GNUNET_HELLO_Address address0;
132
133   ret = GNUNET_SYSERR;
134
135   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
136   ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
137   if (ats == NULL)
138   {
139     ret = GNUNET_SYSERR;
140     end ();
141     return;
142   }
143   /* set up peer */
144   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
145                                     &p[0].id.hashPubKey);
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
147               GNUNET_i2s (&p[0].id));
148
149   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
150                                     &p[1].id.hashPubKey);
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
152               GNUNET_i2s (&p[1].id));
153
154   test_addr[0].plugin = "test";
155   test_addr[0].session = NULL;
156   test_addr[0].addr = GNUNET_strdup ("test");
157   test_addr[0].addr_len = 4;
158
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing address creation\n");
160
161   address0.peer = p[0].id;
162   address0.transport_name = test_addr[0].plugin;
163   address0.address = test_addr[0].addr;
164   address0.address_length = test_addr[0].addr_len;
165   GNUNET_ATS_address_add (ats, &address0, test_addr[0].session, NULL, 0);
166   GNUNET_ATS_address_update (ats, &address0, test_addr[0].session, NULL, 0);
167
168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing ATS info creation\n");
169
170   atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP);
171   atsi[0].value = htonl (1024);
172
173   GNUNET_ATS_address_update (ats, &address0, test_addr[0].session, atsi, 1);
174
175   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing ATS info update\n");
176
177   atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP);
178   atsi[0].value = htonl (2048);
179
180   atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_DOWN);
181   atsi[1].value = htonl (1024);
182
183   GNUNET_ATS_address_update (ats, &address0, test_addr[0].session, atsi, 2);
184
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting peer `%s'\n",
186               GNUNET_i2s (&p[0].id));
187   GNUNET_ATS_suggest_address (ats, &p[0].id);
188 }
189
190
191 int
192 main (int argc, char *argv[])
193 {
194   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling",
195                                     "test_ats_api.conf",
196                                     &run, NULL))
197     return 1;
198   return ret;
199 }
200
201 /* end of file test_ats_api_scheduling.c */