adding initial statistics call
[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, const struct GNUNET_HELLO_Address *address,
157                     struct Session *session,
158                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
159                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
160                     const struct GNUNET_ATS_Information *atsi,
161                     uint32_t ats_count)
162 {
163   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion callback!\n");
164   GNUNET_SCHEDULER_add_now (&end_badly, NULL);
165   return;
166 }
167
168 static void
169 got_initial_value (void *cls, int success)
170 {
171   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
172
173   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got initial value\n");
174
175   /* Connect to ATS scheduling */
176   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
177   if (sched_ats == NULL)
178   {
179     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
180     GNUNET_SCHEDULER_add_now (&end_badly, NULL);
181     return;
182   }
183
184   /* Set up peer */
185   memset (&p.id, '1', sizeof (p.id));
186   /* Prepare ATS Information */
187   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
188   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
189   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
190   test_ats_info[1].value = htonl(1);
191   test_ats_count = 2;
192
193   /* Adding address without session */
194   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
195   test_hello_address.peer = p.id;
196   test_hello_address.transport_name = test_addr.plugin;
197   test_hello_address.address = test_addr.addr;
198   test_hello_address.address_length = test_addr.addr_len;
199
200   /* Adding address */
201   GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, test_ats_info, test_ats_count);
202
203 }
204
205 static int
206 dummy_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
207             int is_persistent)
208 {
209   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got dummy stat %s%s:%s = %llu\n",
210               is_persistent ? "!" : " ", subsystem, name, value);
211   return GNUNET_OK;
212 }
213
214
215
216 static void
217 run (void *cls,
218      const struct GNUNET_CONFIGURATION_Handle *cfg,
219      struct GNUNET_TESTING_Peer *peer)
220 {
221   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
222   stats = GNUNET_STATISTICS_create ("ats", cfg);
223   GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
224
225
226   GNUNET_STATISTICS_get (stats, "ats", "# addresses", TIMEOUT,
227                                        &got_initial_value, &dummy_stat,
228                                        GNUNET_CONFIGURATION_dup (cfg));
229 }
230
231
232 int
233 main (int argc, char *argv[])
234 {
235   ret = 0;
236   if (0 != GNUNET_TESTING_peer_run ("test-ats-api",
237                                     "test_ats_api.conf",
238                                     &run, NULL))
239     return 1;
240   return ret;
241 }
242 /* end of file test_ats_api_scheduling_destroy_session.c */