- fixes
[oweals/gnunet.git] / src / ats / test_ats_mlp.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 VERBOSE GNUNET_YES
34 #define VERBOSE_ARM GNUNET_NO
35
36 #define MLP_MAX_EXEC_DURATION   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
37 #define MLP_MAX_ITERATIONS      INT_MAX
38
39
40 static int ret;
41
42 struct GNUNET_STATISTICS_Handle * stats;
43
44 struct GNUNET_CONTAINER_MultiHashMap * addresses;
45
46 struct GAS_MLP_Handle *mlp;
47
48
49 static void
50 create_address (struct ATS_Address *addr, char * plugin, int ats_count, struct GNUNET_ATS_Information *ats)
51 {
52   addr->mlp_information = NULL;
53   addr->next = NULL;
54   addr->prev = NULL;
55   addr->plugin = strdup (plugin);
56   addr->ats_count = ats_count;
57   addr->ats = ats;
58 }
59
60 static void
61 set_ats (struct GNUNET_ATS_Information *ats, uint32_t type, uint32_t value)
62 {
63   ats->type = type;
64   ats->value = value;
65 }
66
67 static void
68 check (void *cls, char *const *args, const char *cfgfile,
69        const struct GNUNET_CONFIGURATION_Handle *cfg)
70 {
71 #if !HAVE_LIBGLPK
72   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
73   ret = 1;
74   return;
75 #endif
76   struct ATS_Address addr[10];
77   struct ATS_PreferedAddress *res[10];
78   struct GAS_MLP_SolutionContext ctx;
79
80   stats = GNUNET_STATISTICS_create("ats", cfg);
81
82   addresses = GNUNET_CONTAINER_multihashmap_create (10);
83
84   mlp = GAS_mlp_init (cfg, NULL, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
85   mlp->auto_solve = GNUNET_NO;
86
87   struct GNUNET_PeerIdentity p[10];
88
89   /* Creating peer 1 */
90   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
91   /* Creating peer 2 */
92   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[1].hashPubKey);
93
94   /* Creating peer 1 address 1 */
95   addr[0].peer.hashPubKey = p[0].hashPubKey;
96   struct GNUNET_ATS_Information a1_ats[3];
97   set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
98   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
99   set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
100   create_address (&addr[0], "dummy", 3, &a1_ats[0]);
101   addr[0].atsp_network_type = GNUNET_ATS_NET_WAN;
102
103   /* Creating peer 1  address 2 */
104   addr[1].peer.hashPubKey = p[0].hashPubKey;
105   struct GNUNET_ATS_Information a2_ats[3];
106   set_ats (&a2_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
107   set_ats (&a2_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
108   set_ats (&a2_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
109   create_address (&addr[1], "dummy2", 3, &a2_ats[0]);
110   addr[1].atsp_network_type = GNUNET_ATS_NET_LAN;
111
112   /* Creating peer 2  address 1 */
113   addr[2].peer.hashPubKey = p[1].hashPubKey;
114   struct GNUNET_ATS_Information a3_ats[3];
115   set_ats (&a3_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
116   set_ats (&a3_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
117   set_ats (&a3_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
118   create_address (&addr[2], "dummy3", 3, &a3_ats[0]);
119   addr[2].atsp_network_type = GNUNET_ATS_NET_LAN;
120
121   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
122
123   /* Add peer 1 address 1 */
124   GAS_mlp_address_update (mlp, addresses, &addr[0]);
125
126   GNUNET_assert (mlp != NULL);
127   GNUNET_assert (mlp->addr_in_problem == 1);
128
129   /* Update an peer 1 address 1  */
130   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
131   GAS_mlp_address_update (mlp, addresses, &addr[0]);
132   GNUNET_assert (mlp->addr_in_problem == 1);
133
134   /* Add peer 1 address 2 */
135   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[1], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
136   GAS_mlp_address_update (mlp, addresses, &addr[1]);
137   GNUNET_assert (mlp->addr_in_problem == 2);
138
139   /* Add peer 2 address 1 */
140   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[2].peer.hashPubKey, &addr[2], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
141   GAS_mlp_address_update (mlp, addresses, &addr[2]);
142   GNUNET_assert (mlp->addr_in_problem == 3);
143
144   GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
145   GNUNET_assert (GNUNET_OK == ctx.lp_result);
146   GNUNET_assert (GNUNET_OK == ctx.mlp_result);
147
148   res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
149   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[0]->address->plugin, res[0]->bandwidth_out);
150   res[1] = GAS_mlp_get_preferred_address(mlp, addresses, &p[1]);
151   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[1]->address->plugin, res[1]->bandwidth_out);
152
153   /* Delete an address */
154   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
155   GAS_mlp_address_delete (mlp, addresses, &addr[0]);
156   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[1].peer.hashPubKey, &addr[1]);
157   GAS_mlp_address_delete (mlp, addresses, &addr[1]);
158   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[2].peer.hashPubKey, &addr[2]);
159   GAS_mlp_address_delete (mlp, addresses, &addr[2]);
160
161   GNUNET_assert (mlp->addr_in_problem == 0);
162
163   GAS_mlp_done (mlp);
164
165   GNUNET_free (addr[0].plugin);
166   GNUNET_free (addr[1].plugin);
167   GNUNET_CONTAINER_multihashmap_destroy (addresses);
168   GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
169
170   ret = 0;
171   return;
172 }
173
174
175 int
176 main (int argc, char *argv[])
177 {
178
179   static char *const argv2[] = { "test_ats_mlp",
180     "-c",
181     "test_ats_api.conf",
182 #if VERBOSE
183     "-L", "DEBUG",
184 #else
185     "-L", "WARNING",
186 #endif
187     NULL
188   };
189
190   static struct GNUNET_GETOPT_CommandLineOption options[] = {
191     GNUNET_GETOPT_OPTION_END
192   };
193
194   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
195                       "test_ats_mlp", "nohelp", options,
196                       &check, NULL);
197
198
199   return ret;
200 }
201
202 /* end of file test_ats_api_bandwidth_consumption.c */