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