(no commit message)
[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 #if HAVE_LIBGLPK
100   ats_calculate_bandwidth_distribution (ats);
101 #endif
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 #if HAVE_LIBGLPK
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 #endif
115   //GNUNET_assert (ats != NULL);
116
117   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118               "Initializing ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
119   return ret;
120 }
121
122
123 int shutdown_ats (void)
124 {
125   int ret = 0;
126 #if HAVE_LIBGLPK
127   ats_delete_problem (ats);
128   ats_shutdown (ats);
129 #endif
130   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
131               "Shutdown ATS: %s \n", (ret==0)? "SUCCESSFUL": "FAILED");
132   return ret;
133 }
134
135 /* To make compiler happy */
136 void dummy(void)
137 {
138   struct ATS_quality_metric * q = qm;
139   q = NULL;
140   struct ATS_ressource * r = ressources;
141   r = NULL;
142 }
143
144 void iterate_peer_values (void *cls,
145                       const char *section,
146                       const char *option,
147                       const char *value)
148 {
149   if (strcmp (option, "f") == 0)
150     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
151                 "\t %s %s\n", option, value);
152 }
153
154 void iterate_mech_values (void *cls,
155                       const char *section,
156                       const char *option,
157                       const char *value)
158 {
159   if (strcmp (option, "f") == 0)
160     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
161                 "\t %s %s\n", option, value);
162 }
163
164 void iterate_sections (void *cls,
165                         const char *section)
166 {
167   struct TransportConfiguration * tc = cls;
168   /* Peer definition */
169   if (99 == strlen(section))
170     {
171       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer '%s`\n", section);
172       GNUNET_HashCode h;
173       int res =GNUNET_CRYPTO_hash_from_string(section, &h);
174       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "HASH '%s` %i\n", GNUNET_h2s (&h), res);
175       GNUNET_CONFIGURATION_iterate_section_values(cfg, section, iterate_peer_values, NULL);
176       tc->peers++;
177     }
178   if (10 == strlen(section))
179     {
180       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Mechanism '%s`\n",section);
181       GNUNET_CONFIGURATION_iterate_section_values(cfg, section, iterate_mech_values, NULL);
182       tc->peers++;
183     }
184 }
185
186 void destroy_transport_configuration (char * filename)
187 {
188   GNUNET_CONFIGURATION_destroy (cfg);
189
190 }
191
192 struct TransportConfiguration * load_transport_configuration (char * filename)
193 {
194   struct TransportConfiguration * ret = GNUNET_malloc(sizeof (struct TransportConfiguration));
195   cfg = GNUNET_CONFIGURATION_create();
196   GNUNET_CONFIGURATION_load(cfg, filename);
197   GNUNET_CONFIGURATION_iterate_sections(cfg, iterate_sections, ret);
198
199   return ret;
200 }
201
202 int
203 main (int argc, char *argv[])
204 {
205   int ret = 0;
206
207   GNUNET_log_setup ("test-transport-ats",
208 #if VERBOSE
209                     "DEBUG",
210 #else
211                     "INFO",
212 #endif
213                     NULL);
214 #if !HAVE_LIBGLPK
215   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
216               "HAVE_LIBGLPK not set, exiting testcase\n");
217 #endif
218
219 #if !HAVE_LIBGLPK
220   return ret;
221 #endif
222
223   return 0;
224
225   tc = load_transport_configuration ("test.ats");
226
227   return ret;
228
229   /* Testing */
230   ats = NULL;
231
232   ret += init_ats ();
233   ret += run_ats ();
234   ret += shutdown_ats ();
235
236   /* Shutdown */
237   return ret;
238
239 }
240
241 /* end of test_transport_ats.c*/