added simulation
[oweals/gnunet.git] / src / ats / test_ats_mlp_averaging.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_mlp.c
22  * @brief test for the MLP solver
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_ats_service.h"
31 #include "gnunet-service-ats_addresses_mlp.h"
32
33 #define MLP_MAX_EXEC_DURATION   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
34 #define MLP_MAX_ITERATIONS      INT_MAX
35
36
37 static int ret;
38
39 struct GNUNET_STATISTICS_Handle * stats;
40
41 struct GNUNET_CONTAINER_MultiHashMap * addresses;
42
43 struct GAS_MLP_Handle *mlp;
44
45
46 static void
47 create_address (struct ATS_Address *addr, char * plugin, int ats_count, struct GNUNET_ATS_Information *ats)
48 {
49   addr->solver_information = NULL;
50   addr->next = NULL;
51   addr->prev = NULL;
52   addr->plugin = GNUNET_strdup (plugin);
53   addr->ats_count = ats_count;
54   addr->ats = ats;
55 }
56
57 static void
58 set_ats (struct GNUNET_ATS_Information *ats, uint32_t type, uint32_t value)
59 {
60   ats->type = type;
61   ats->value = value;
62 }
63
64 static unsigned int
65 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
66 {
67   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
68   char * entry_in = NULL;
69   char * entry_out = NULL;
70   char * quota_out_str;
71   char * quota_in_str;
72   int c;
73
74   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
75   {
76     in_dest[c] = 0;
77     out_dest[c] = 0;
78     switch (quotas[c]) {
79       case GNUNET_ATS_NET_UNSPECIFIED:
80         entry_out = "UNSPECIFIED_QUOTA_OUT";
81         entry_in = "UNSPECIFIED_QUOTA_IN";
82         break;
83       case GNUNET_ATS_NET_LOOPBACK:
84         entry_out = "LOOPBACK_QUOTA_OUT";
85         entry_in = "LOOPBACK_QUOTA_IN";
86         break;
87       case GNUNET_ATS_NET_LAN:
88         entry_out = "LAN_QUOTA_OUT";
89         entry_in = "LAN_QUOTA_IN";
90         break;
91       case GNUNET_ATS_NET_WAN:
92         entry_out = "WAN_QUOTA_OUT";
93         entry_in = "WAN_QUOTA_IN";
94         break;
95       case GNUNET_ATS_NET_WLAN:
96         entry_out = "WLAN_QUOTA_OUT";
97         entry_in = "WLAN_QUOTA_IN";
98         break;
99       default:
100         break;
101     }
102
103     if ((entry_in == NULL) || (entry_out == NULL))
104       continue;
105
106     /* quota out */
107     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
108     {
109       if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
110           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
111         out_dest[c] = UINT32_MAX;
112
113       GNUNET_free (quota_out_str);
114       quota_out_str = NULL;
115     }
116     else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
117       out_dest[c] = UINT32_MAX;
118     else
119       out_dest[c] = UINT32_MAX;
120
121     /* quota in */
122     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
123     {
124       if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
125           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
126         in_dest[c] = UINT32_MAX;
127
128       GNUNET_free (quota_in_str);
129       quota_in_str = NULL;
130     }
131     else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
132     {
133       in_dest[c] = UINT32_MAX;
134     }
135     else
136     {
137         in_dest[c] = UINT32_MAX;
138     }
139     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
140
141   }
142   return GNUNET_ATS_NetworkTypeCount;
143 }
144
145 static void
146 check (void *cls, char *const *args, const char *cfgfile,
147        const struct GNUNET_CONFIGURATION_Handle *cfg)
148 {
149 #if !HAVE_LIBGLPK
150   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
151   ret = 1;
152   return;
153 #endif
154   struct ATS_Address addr[10];
155   struct ATS_Address *res[10];
156   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
157   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
158   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
159   int quota_count;
160   // struct MLP_information *mlpi;
161   struct GAS_MLP_SolutionContext ctx;
162
163   stats = GNUNET_STATISTICS_create("ats", cfg);
164
165   addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
166
167   quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
168   mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
169   mlp->auto_solve = GNUNET_NO;
170
171   struct GNUNET_PeerIdentity p[10];
172
173   /* Creating peer 1 */
174   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
175
176   /* Creating peer 1 address 1 */
177   addr[0].peer.hashPubKey = p[0].hashPubKey;
178   struct GNUNET_ATS_Information a1_ats[3];
179   set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
180   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 0);
181   set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
182   create_address (&addr[0], "dummy", 3, &a1_ats[0]);
183   addr[0].atsp_network_type = GNUNET_ATS_NET_LAN;
184
185   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
186
187   /* Add peer 1 address 1 */
188   GAS_mlp_address_update (mlp, addresses, &addr[0]);
189   // mlpi = addr[0].mlp_information;
190
191   GNUNET_assert (mlp != NULL);
192   GNUNET_assert (mlp->addresses_in_problem == 1);
193
194   /* Update an peer 1 address 1  */
195   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 20);
196   GAS_mlp_address_update (mlp, addresses, &addr[0]);
197   GNUNET_assert (mlp->addresses_in_problem == 1);
198
199
200   /* Update an peer 1 address 1  */
201   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 10);
202   GAS_mlp_address_update (mlp, addresses, &addr[0]);
203   GNUNET_assert (mlp->addresses_in_problem == 1);
204
205   /* Update an peer 1 address 1  */
206   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 10);
207   GAS_mlp_address_update (mlp, addresses, &addr[0]);
208   GNUNET_assert (mlp->addresses_in_problem == 1);
209
210   /* Update an peer 1 address 1  */
211   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 30);
212   GAS_mlp_address_update (mlp, addresses, &addr[0]);
213   GNUNET_assert (mlp->addresses_in_problem == 1);
214
215
216   GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
217   GNUNET_assert (GNUNET_OK == ctx.lp_result);
218   GNUNET_assert (GNUNET_OK == ctx.mlp_result);
219
220   res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
221   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[0]->plugin, ntohl(res[0]->assigned_bw_out.value__));
222
223   /* Delete an address */
224   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
225   GAS_mlp_address_delete (mlp, addresses, &addr[0]);
226
227   GNUNET_assert (mlp->addresses_in_problem == 0);
228
229   GAS_mlp_done (mlp);
230
231   GNUNET_free (addr[0].plugin);
232   GNUNET_CONTAINER_multihashmap_destroy (addresses);
233   GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
234
235   ret = 0;
236   return;
237 }
238
239
240 int
241 main (int argc, char *argv[])
242 {
243
244   static char *const argv2[] = { "test_ats_mlp",
245     "-c",
246     "test_ats_api.conf",
247     "-L", "WARNING",
248     NULL
249   };
250
251   static struct GNUNET_GETOPT_CommandLineOption options[] = {
252     GNUNET_GETOPT_OPTION_END
253   };
254
255   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
256                       "test_ats_mlp", "nohelp", options,
257                       &check, NULL);
258
259
260   return ret;
261 }
262
263 /* end of file test_ats_api_bandwidth_consumption.c */