indent
[oweals/gnunet.git] / src / ats / test_ats_api_scheduling_min_bw.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_min_bw.c
22  * @brief add in address for a network where quota is below min bw: no suggest expected
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
29 #include "ats.h"
30 #include "test_ats_api_common.h"
31
32 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
33
34 static GNUNET_SCHEDULER_TaskIdentifier die_task;
35 static GNUNET_SCHEDULER_TaskIdentifier wait_task;
36
37 /**
38  * Scheduling handle
39  */
40 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
41
42 /**
43  * Return value
44  */
45 static int ret;
46
47 /**
48  * Test address
49  */
50 static struct Test_Address test_addr;
51
52 /**
53  * Test peer
54  */
55 static struct PeerContext p;
56
57 /**
58  * HELLO address
59  */
60 struct GNUNET_HELLO_Address test_hello_address;
61
62 /**
63  * Session
64  */
65 static void *test_session;
66
67 /**
68  * Test ats info
69  */
70 struct GNUNET_ATS_Information test_ats_info[2];
71
72 /**
73  * Test ats count
74  */
75 uint32_t test_ats_count;
76
77
78 static void
79 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   die_task = GNUNET_SCHEDULER_NO_TASK;
82
83   if (sched_ats != NULL)
84     GNUNET_ATS_scheduling_done (sched_ats);
85   free_test_address (&test_addr);
86   ret = GNUNET_SYSERR;
87 }
88
89
90 static void
91 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 {
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
94   if (die_task != GNUNET_SCHEDULER_NO_TASK)
95   {
96     GNUNET_SCHEDULER_cancel (die_task);
97     die_task = GNUNET_SCHEDULER_NO_TASK;
98   }
99   if (NULL != sched_ats)
100     GNUNET_ATS_scheduling_done (sched_ats);
101   sched_ats = NULL;
102   free_test_address (&test_addr);
103 }
104
105 static void
106 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
107                     struct Session *session,
108                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
109                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
110                     const struct GNUNET_ATS_Information *atsi,
111                     uint32_t ats_count)
112 {
113     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not expect suggestion!\n");
114         GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
115         GNUNET_SCHEDULER_add_now (&end, NULL);
116         ret = 1;
117 }
118
119 static void
120 run (void *cls, 
121      const struct GNUNET_CONFIGURATION_Handle *cfg,
122      struct GNUNET_TESTING_Peer *peer)
123 {
124
125   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
126
127   /* Connect to ATS scheduling */
128   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
129   if (sched_ats == NULL)
130   {
131     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
132     ret = 1;
133     GNUNET_SCHEDULER_add_now (&end , NULL);
134     return;
135   }
136
137   /* Set up peer */
138   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
139   {
140       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
141       ret = GNUNET_SYSERR;
142       GNUNET_SCHEDULER_add_now (&end , NULL);
143       return;
144   }
145
146   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p.id)));
147
148   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
149               GNUNET_i2s_full(&p.id));
150
151   /* Prepare ATS Information */
152   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
153   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WLAN); /* WLAN quota is below min bw */
154   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
155   test_ats_info[1].value = htonl(1);
156   test_ats_count = 2;
157
158   /* Adding address without session */
159   test_session = NULL;
160   create_test_address (&test_addr, "test", test_session, "test", strlen ("test") + 1);
161   test_hello_address.peer = p.id;
162   test_hello_address.transport_name = test_addr.plugin;
163   test_hello_address.address = test_addr.addr;
164   test_hello_address.address_length = test_addr.addr_len;
165   GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
166
167   GNUNET_ATS_suggest_address (sched_ats, &p.id);
168
169   ret = 0;
170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Waiting for wait period for no suggest...\n");
171   wait_task = GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
172 }
173
174
175 int
176 main (int argc, char *argv[])
177 {
178   if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_add_address",
179                                     "test_ats_api.conf",
180                                     &run, NULL))
181     return 1;
182   return ret;
183 }
184
185 /* end of file test_ats_api_scheduling_min_bw.c */