renaming ats files
[oweals/gnunet.git] / src / transport / test_transport_ats.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 testing/test_transport_ats.c
22  * @brief testcase for ats functionality without starting peers
23  */
24 #include "platform.h"
25 #include "gnunet-service-transport_ats.h"
26 #include "gnunet_configuration_lib.h"
27 #include "gnunet_crypto_lib.h"
28
29 #define VERBOSE GNUNET_YES
30
31 static struct ATS_Handle * ats;
32 static struct GNUNET_CONFIGURATION_Handle * cfg;
33
34 void ats_result_cb ()
35 {
36   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
37       "ATS Result callback\n");
38 }
39
40 struct TransportConfiguration
41 {
42   int peers;
43   int mechanisms;
44
45   struct ATS_peer * p_head;
46   struct ATS_peer * p_tail;
47
48   struct ATS_mechanism * m_head;
49   struct ATS_mechanism * m_tail;
50 };
51
52 struct TransportConfiguration *tc;
53
54 /*
55 void create_topology (int c_peers, int c_mechanisms)
56 {
57   int c;
58   peers = GNUNET_malloc ( c_peers * sizeof (struct ATS_peer));
59   for (c=0 ; c<c_peers; c++)
60     {
61       peers[c].f = 1.0 / c_peers;
62       GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &peers[c].peer.hashPubKey);
63       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer %s \n", GNUNET_i2s (&peers[c].peer));
64       peers[c].m_head = NULL;
65       peers[c].m_tail = NULL;
66     }
67   mechanisms = GNUNET_malloc ( c_mechanisms * sizeof (struct ATS_mechanism));
68   for (c=0 ; c<c_mechanisms; c++)
69     {
70        mechanisms[c].peer = &peers[c];
71     }
72 }
73
74
75 void delete_topology (void)
76 {
77   GNUNET_free (peers);
78   GNUNET_free (mechanisms);
79 }*/
80
81
82 void create_ats_information (struct ATS_peer **p, int * c_p,
83                              struct ATS_mechanism ** m, int * c_m)
84 {
85
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87       "ATS needs addresses\n");
88
89  (*p) = tc->p_head;
90  (*c_p) = tc->mechanisms;
91  (*m) = tc->m_head;
92  (*c_m) = tc->mechanisms;
93
94 }
95
96 int run_ats (void)
97 {
98   int ret = 0;
99
100   ats_calculate_bandwidth_distribution(ats, NULL);
101
102   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103               "Running ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
104   return ret;
105 }
106
107 int init_ats (void)
108 {
109   int ret = 0;
110
111   ats = ats_init(1.0, 1.0, 1.0, 50000, 5, 10, ATS_MAX_EXEC_DURATION,
112                 create_ats_information,
113                 ats_result_cb);
114   //GNUNET_assert (ats != NULL);
115
116   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117               "Initializing ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
118   return ret;
119 }
120
121
122 int shutdown_ats (void)
123 {
124   int ret = 0;
125
126   ats_delete_problem (ats);
127   ats_shutdown (ats);
128   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129               "Shutdown ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
130   return ret;
131 }
132
133 /* To make compiler happy */
134 void dummy(void)
135 {
136   struct ATS_quality_metric * q = qm;
137   q = NULL;
138   struct ATS_ressource * r = ressources;
139   r = NULL;
140 }
141
142 void iterate_peer_values (void *cls,
143                       const char *section,
144                       const char *option,
145                       const char *value)
146 {
147   if (strcmp (option, "f") == 0)
148     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
149                 "\t %s %s\n", option, value);
150 }
151
152 void iterate_mech_values (void *cls,
153                       const char *section,
154                       const char *option,
155                       const char *value)
156 {
157   if (strcmp (option, "f") == 0)
158     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
159                 "\t %s %s\n", option, value);
160 }
161
162 void iterate_sections (void *cls,
163                         const char *section)
164 {
165   struct TransportConfiguration * tc = cls;
166   /* Peer definition */
167   if (99 == strlen(section))
168     {
169       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer '%s`\n", section);
170       GNUNET_HashCode h;
171       int res =GNUNET_CRYPTO_hash_from_string(section, &h);
172       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "HASH '%s` %i\n", GNUNET_h2s (&h), res);
173       GNUNET_CONFIGURATION_iterate_section_values(cfg, section, iterate_peer_values, NULL);
174       tc->peers++;
175     }
176   if (10 == strlen(section))
177     {
178       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Mechanism '%s`\n",section);
179       GNUNET_CONFIGURATION_iterate_section_values(cfg, section, iterate_mech_values, NULL);
180       tc->peers++;
181     }
182 }
183
184 void destroy_transport_configuration (char * filename)
185 {
186   GNUNET_CONFIGURATION_destroy (cfg);
187
188 }
189
190 struct TransportConfiguration * load_transport_configuration (char * filename)
191 {
192   struct TransportConfiguration * ret = GNUNET_malloc(sizeof (struct TransportConfiguration));
193   cfg = GNUNET_CONFIGURATION_create();
194   GNUNET_CONFIGURATION_load(cfg, filename);
195   GNUNET_CONFIGURATION_iterate_sections(cfg, iterate_sections, ret);
196
197   return ret;
198 }
199
200 int
201 main (int argc, char *argv[])
202 {
203   int ret = 0;
204
205   GNUNET_log_setup ("test-transport-ats",
206 #if VERBOSE
207                     "DEBUG",
208 #else
209                     "INFO",
210 #endif
211                     NULL);
212 #if !HAVE_LIBGLPK
213   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
214               "HAVE_LIBGLPK not set, exiting testcase\n");
215 #endif
216
217 #if !HAVE_LIBGLPK
218   return ret;
219 #endif
220
221   return 0;
222
223   tc = load_transport_configuration ("test.ats");
224
225   return ret;
226
227   /* Testing */
228   ats = NULL;
229
230   ret += init_ats ();
231   ret += run_ats ();
232   ret += shutdown_ats ();
233
234   /* Shutdown */
235   return ret;
236
237 }
238
239 /* end of test_transport_ats.c*/