2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file testbed/testbed_api_topology.c
23 * @brief topology-generation functions
24 * @author Christian Grothoff
27 #include "gnunet_testbed_service.h"
28 #include "testbed_api.h"
29 #include "testbed_api_peers.h"
30 #include "testbed_api_operations.h"
33 * Generic loggins shorthand
35 #define LOG(kind,...) \
36 GNUNET_log_from (kind, "testbed-api-topology", __VA_ARGS__)
39 * Representation of an overlay link
44 * position of peer A's handle in peers array
49 * position of peer B's handle in peers array
57 * Context information for topology operations
59 struct TopologyContext
64 struct GNUNET_TESTBED_Peer **peers;
67 * An array of links; this array is of size link_array_size
69 struct OverlayLink *link_array;
72 * An array of operations resulting from the links we try to establish; the
73 * number of operations in this array is equal to link_array_size (1 link = 1
76 struct GNUNET_TESTBED_Operation **link_ops;
79 * The operation closure
84 * The size of the link array
86 unsigned int link_array_size;
92 * Callback to be called when an overlay_link operation complete
94 * @param cls element of the link_op array which points to the corresponding operation
95 * @param op the operation that has been finished
96 * @param emsg error message in case the operation has failed; will be NULL if
97 * operation has executed successfully.
100 overlay_link_completed (void *cls,
101 struct GNUNET_TESTBED_Operation *op,
104 struct GNUNET_TESTBED_Operation **link_op = cls;
106 GNUNET_assert (*link_op == op);
107 GNUNET_TESTBED_operation_done (op);
111 LOG (GNUNET_ERROR_TYPE_WARNING,
112 "Error while establishing a link: %s\n", emsg);
120 * Function called when a overlay connect operation is ready
122 * @param cls the Topology context
125 opstart_overlay_configure_topology (void *cls)
127 struct TopologyContext *tc = cls;
130 tc->link_ops = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operations *)
131 * tc->link_array_size);
132 for (p = 0; p < tc->link_array_size; p++)
135 GNUNET_TESTBED_overlay_connect (tc->op_cls, &overlay_link_completed,
137 tc->peers[tc->link_array[p].A],
138 tc->peers[tc->link_array[p].B]);
144 * Callback which will be called when overlay connect operation is released
146 * @param cls the Topology context
149 oprelease_overlay_configure_topology (void *cls)
151 struct TopologyContext *tc = cls;
154 if (NULL != tc->link_ops)
156 for (p = 0; p < tc->link_array_size; p++)
157 if (NULL != tc->link_ops[p])
158 GNUNET_TESTBED_operation_cancel (tc->link_ops[p]);
159 GNUNET_free (tc->link_ops);
161 GNUNET_free_non_null (tc->link_array);
167 * Configure overall network topology to have a particular shape.
169 * @param op_cls closure argument to give with the operation event
170 * @param num_peers number of peers in 'peers'
171 * @param peers array of 'num_peers' with the peers to configure
172 * @param topo desired underlay topology to use
173 * @param ap topology-specific options
174 * @return handle to the operation, NULL if configuring the topology
175 * is not allowed at this time
177 struct GNUNET_TESTBED_Operation *
178 GNUNET_TESTBED_underlay_configure_topology_va (void *op_cls,
179 unsigned int num_peers,
180 struct GNUNET_TESTBED_Peer
183 GNUNET_TESTBED_TopologyOption
192 * Configure overall network topology to have a particular shape.
194 * @param op_cls closure argument to give with the operation event
195 * @param num_peers number of peers in 'peers'
196 * @param peers array of 'num_peers' with the peers to configure
197 * @param topo desired underlay topology to use
198 * @param ... topology-specific options
199 * @return handle to the operation, NULL if configuring the topology
200 * is not allowed at this time
202 struct GNUNET_TESTBED_Operation *
203 GNUNET_TESTBED_underlay_configure_topology (void *op_cls,
204 unsigned int num_peers,
205 struct GNUNET_TESTBED_Peer **peers,
206 enum GNUNET_TESTBED_TopologyOption
215 * All peers must have been started before calling this function.
216 * This function then connects the given peers in the P2P overlay
217 * using the given topology.
219 * @param op_cls closure argument to give with the operation event
220 * @param num_peers number of peers in 'peers'
221 * @param peers array of 'num_peers' with the peers to configure
222 * @param topo desired underlay topology to use
223 * @param va topology-specific options
224 * @return handle to the operation, NULL if connecting these
225 * peers is fundamentally not possible at this time (peers
226 * not running or underlay disallows) or if num_peers is less than 2
228 struct GNUNET_TESTBED_Operation *
229 GNUNET_TESTBED_overlay_configure_topology_va (void *op_cls,
230 unsigned int num_peers,
231 struct GNUNET_TESTBED_Peer **peers,
232 enum GNUNET_TESTBED_TopologyOption
235 struct TopologyContext *tc;
236 struct GNUNET_TESTBED_Operation *op;
237 struct GNUNET_TESTBED_Controller *c;
242 c = peers[0]->controller;
243 tc = GNUNET_malloc (sizeof (struct TopologyContext));
245 tc->op_cls = tc->op_cls;
248 case GNUNET_TESTBED_TOPOLOGY_LINE:
249 tc->link_array_size = num_peers - 1;
250 tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink) *
251 tc->link_array_size);
252 for (cnt=1; cnt < num_peers; cnt++)
254 tc->link_array[cnt-1].A = cnt-1;
255 tc->link_array[cnt-1].B = cnt;
258 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
259 tc->link_array_size = va_arg (va, unsigned int);
260 tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink) *
261 tc->link_array_size);
262 for (cnt = 0; cnt < tc->link_array_size; cnt++)
268 A_rand = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
270 B_rand = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
272 } while (A_rand == B_rand);
273 tc->link_array[cnt].A = A_rand;
274 tc->link_array[cnt].B = B_rand;
281 op = GNUNET_TESTBED_operation_create_ (tc,
282 &opstart_overlay_configure_topology,
283 &oprelease_overlay_configure_topology);
284 GNUNET_TESTBED_operation_queue_insert_
285 (c->opq_parallel_topology_config_operations, op);
291 * All peers must have been started before calling this function.
292 * This function then connects the given peers in the P2P overlay
293 * using the given topology.
295 * @param op_cls closure argument to give with the operation event
296 * @param num_peers number of peers in 'peers'
297 * @param peers array of 'num_peers' with the peers to configure
298 * @param topo desired underlay topology to use
299 * @param ... topology-specific options
300 * @return handle to the operation, NULL if connecting these
301 * peers is fundamentally not possible at this time (peers
302 * not running or underlay disallows) or if num_peers is less than 2
304 struct GNUNET_TESTBED_Operation *
305 GNUNET_TESTBED_overlay_configure_topology (void *op_cls, unsigned int num_peers,
306 struct GNUNET_TESTBED_Peer **peers,
307 enum GNUNET_TESTBED_TopologyOption
310 struct GNUNET_TESTBED_Operation *op;
313 va_start (vargs, topo);
314 op = GNUNET_TESTBED_overlay_configure_topology_va (op_cls, num_peers, peers,
320 /* end of testbed_api_topology.c */