extending ATS api suggest callback with peer id
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_destroy_session.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_destroy_session.c
22  * @brief test destroying sessions: first add an address with a session,
23  *        request the address and compare, delete the session, request and
24  *        compare again, delete whole address, request and wait for timeout,
25  *        shutdown
26  * @author Christian Grothoff
27  * @author Matthias Wachs
28  *
29  */
30 #include "platform.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet_testing_lib.h"
33 #include "ats.h"
34 #include "test_ats_api_common.h"
35
36 /**
37  * Timeout task
38  */
39 static GNUNET_SCHEDULER_TaskIdentifier die_task;
40
41 /**
42  * Statistics handle
43  */
44 struct GNUNET_STATISTICS_Handle *stats;
45
46 /**
47  * Scheduling handle
48  */
49 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
50
51 /**
52  * Return value
53  */
54 static int ret;
55
56 /**
57  * Test address
58  */
59 static struct Test_Address test_addr;
60
61 /**
62  * Test peer
63  */
64 static struct PeerContext p;
65
66 /**
67  * HELLO address
68  */
69 struct GNUNET_HELLO_Address test_hello_address;
70
71 /**
72  * Session
73  */
74 static void *test_session;
75
76 /**
77  * Test ats info
78  */
79 struct GNUNET_ATS_Information test_ats_info[2];
80
81 /**
82  * Test ats count
83  */
84 uint32_t test_ats_count;
85
86
87 static void end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
88 static void end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
89
90 static int
91 stat_cb(void *cls, const char *subsystem,
92         const char *name, uint64_t value,
93         int is_persistent)
94 {
95   static int first_stat_cb = GNUNET_YES;
96
97   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS statistics: `%s' `%s' %llu\n",
98       subsystem,name, value);
99   if ((GNUNET_YES == first_stat_cb) && (1 == value))
100   {
101     GNUNET_ATS_address_add (sched_ats, &test_hello_address, (struct Session *) &test_session, test_ats_info, test_ats_count);
102     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
103   }
104   if ((GNUNET_NO == first_stat_cb) && (1 == value))
105   {
106     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ATS updated existing address\n");
107   }
108   if (value > 1)
109   {
110     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS did not update existing address, but added 2nd address!\n");
111     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
112   }
113
114   return GNUNET_OK;
115 }
116
117
118 static void
119 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 {
121   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
122
123   if (die_task != GNUNET_SCHEDULER_NO_TASK)
124   {
125     GNUNET_SCHEDULER_cancel (die_task);
126     die_task = GNUNET_SCHEDULER_NO_TASK;
127   }
128
129   if (NULL != sched_ats)
130   {
131     GNUNET_ATS_scheduling_done (sched_ats);
132     sched_ats = NULL;
133   }
134
135   GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
136   if (NULL != stats)
137   {
138     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
139     stats = NULL;
140   }
141
142   free_test_address (&test_addr);
143
144   ret = 0;
145 }
146
147 static void
148 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 {
150   die_task = GNUNET_SCHEDULER_NO_TASK;
151   end ( NULL, NULL);
152   ret = GNUNET_SYSERR;
153 }
154
155 static void
156 address_suggest_cb (void *cls,
157                     const struct GNUNET_PeerIdentity *peer,
158                     const struct GNUNET_HELLO_Address *address,
159                     struct Session *session,
160                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
161                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
162                     const struct GNUNET_ATS_Information *atsi,
163                     uint32_t ats_count)
164 {
165   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
166   GNUNET_SCHEDULER_add_now (&end_badly, NULL);
167   return;
168 }
169
170 static void
171 got_initial_value (void *cls, int success)
172 {
173   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
174
175   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got initial value\n");
176
177   /* Connect to ATS scheduling */
178   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
179   if (sched_ats == NULL)
180   {
181     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
182     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
183     return;
184   }
185
186   /* Set up peer */
187   memset (&p.id, '1', sizeof (p.id));
188   /* Prepare ATS Information */
189   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
190   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
191   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
192   test_ats_info[1].value = htonl(1);
193   test_ats_count = 2;
194
195   /* Adding address without session */
196   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
197   test_hello_address.peer = p.id;
198   test_hello_address.transport_name = test_addr.plugin;
199   test_hello_address.address = test_addr.addr;
200   test_hello_address.address_length = test_addr.addr_len;
201
202   /* Adding address */
203   GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
204
205 }
206
207 static int
208 dummy_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
209             int is_persistent)
210 {
211   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got dummy stat %s%s:%s = %llu\n",
212               is_persistent ? "!" : " ", subsystem, name, value);
213   return GNUNET_OK;
214 }
215
216
217
218 static void
219 run (void *cls,
220      const struct GNUNET_CONFIGURATION_Handle *cfg,
221      struct GNUNET_TESTING_Peer *peer)
222 {
223   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
224   stats = GNUNET_STATISTICS_create ("ats", cfg);
225   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
226
227
228   GNUNET_STATISTICS_get (stats, "ats", "# addresses", TIMEOUT,
229                                        &got_initial_value, &dummy_stat,
230                                        GNUNET_CONFIGURATION_dup (cfg));
231 }
232
233
234 int
235 main (int argc, char *argv[])
236 {
237   ret = 0;
238   if (0 != GNUNET_TESTING_peer_run ("test-ats-api",
239                                     "test_ats_api.conf",
240                                     &run, NULL))
241     return 1;
242   return ret;
243 }
244 /* end of file test_ats_api_scheduling_destroy_session.c */