aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/testbed_api_topology.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-11-16 17:32:37 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-11-16 17:32:37 +0000
commit89b4941e400fce0e4abaf7b456caf11d236b2587 (patch)
tree0f689fe8004c8db37f0261c28466389ebdb578f7 /src/testbed/testbed_api_topology.c
parent2fe4c0eb178885680f687ff915eba8ba3fb76435 (diff)
downloadgnunet-89b4941e400fce0e4abaf7b456caf11d236b2587.tar.gz
gnunet-89b4941e400fce0e4abaf7b456caf11d236b2587.zip
- de-duplication
Diffstat (limited to 'src/testbed/testbed_api_topology.c')
-rw-r--r--src/testbed/testbed_api_topology.c83
1 files changed, 58 insertions, 25 deletions
diff --git a/src/testbed/testbed_api_topology.c b/src/testbed/testbed_api_topology.c
index effc428e8..9c62502c4 100644
--- a/src/testbed/testbed_api_topology.c
+++ b/src/testbed/testbed_api_topology.c
@@ -28,6 +28,7 @@
28#include "testbed_api.h" 28#include "testbed_api.h"
29#include "testbed_api_peers.h" 29#include "testbed_api_peers.h"
30#include "testbed_api_operations.h" 30#include "testbed_api_operations.h"
31#include "testbed_api_topology.h"
31 32
32/** 33/**
33 * Generic loggins shorthand 34 * Generic loggins shorthand
@@ -232,49 +233,81 @@ gen_topo_ring (struct TopologyContext *tc)
232 233
233 234
234/** 235/**
235 * Generates ring topology 236 * Returns the number of links that are required to generate a 2d torus for the
237 * given number of peers. Also returns the arrangment (number of rows and the
238 * length of each row)
236 * 239 *
237 * @param tc the topology context 240 * @param num_peers number of peers
241 * @param rows number of rows in the 2d torus. Can be NULL
242 * @param rows_len the length of each row. This array will be allocated
243 * fresh. The caller should free it. Can be NULL
238 */ 244 */
239static void 245unsigned int
240gen_topo_2dtorus (struct TopologyContext *tc) 246GNUNET_TESTBED_2dtorus_calc_links (unsigned int num_peers,
247 unsigned int *rows,
248 unsigned int **rows_len)
241{ 249{
242 double sq; 250 double sq;
243 unsigned int sq_floor; 251 unsigned int sq_floor;
244 unsigned int rows; 252 unsigned int _rows;
245 unsigned int *rows_len; 253 unsigned int *_rows_len;
246 unsigned int x; 254 unsigned int x;
247 unsigned int y; 255 unsigned int y;
248 unsigned int num_peers; 256 unsigned int _num_peers;
249 unsigned int cnt; 257 unsigned int cnt;
250 unsigned int offset; 258
251 259 sq = sqrt (num_peers);
252 sq = sqrt (tc->num_peers);
253 sq = floor (sq); 260 sq = floor (sq);
254 sq_floor = (unsigned int) sq; 261 sq_floor = (unsigned int) sq;
255 rows = (sq_floor + 1); 262 _rows = (sq_floor + 1);
256 rows_len = GNUNET_malloc (sizeof (unsigned int) * rows); 263 _rows_len = GNUNET_malloc (sizeof (unsigned int) * _rows);
257 for (y = 0; y < rows - 1; y++) 264 for (y = 0; y < _rows - 1; y++)
258 rows_len[y] = sq_floor; 265 _rows_len[y] = sq_floor;
259 num_peers = sq_floor * sq_floor; 266 _num_peers = sq_floor * sq_floor;
260 GNUNET_assert (num_peers <= tc->num_peers); 267 cnt = 2 * _num_peers;
261 tc->link_array_size = 2 * num_peers;
262 x = 0; 268 x = 0;
263 y = 0; 269 y = 0;
264 while (num_peers < tc->num_peers) 270 while (_num_peers < num_peers)
265 { 271 {
266 if (x < y) 272 if (x < y)
267 rows_len[rows - 1] = ++x; 273 _rows_len[_rows - 1] = ++x;
268 else 274 else
269 rows_len[y++]++; 275 _rows_len[y++]++;
270 num_peers++; 276 _num_peers++;
271 } 277 }
272 tc->link_array_size += (x < 2) ? x : 2 * x; 278 cnt += (x < 2) ? x : 2 * x;
273 tc->link_array_size += (y < 2) ? y : 2 * y; 279 cnt += (y < 2) ? y : 2 * y;
280 if (0 == _rows_len[_rows - 1])
281 _rows--;
282 if (NULL != rows)
283 *rows = _rows;
284 if (NULL != rows_len)
285 *rows_len = _rows_len;
286 else
287 GNUNET_free (_rows_len);
288 return cnt;
289}
290
291
292/**
293 * Generates ring topology
294 *
295 * @param tc the topology context
296 */
297static void
298gen_topo_2dtorus (struct TopologyContext *tc)
299{
300 unsigned int rows;
301 unsigned int *rows_len;
302 unsigned int x;
303 unsigned int y;
304 unsigned int cnt;
305 unsigned int offset;
306
307 tc->link_array_size = GNUNET_TESTBED_2dtorus_calc_links (tc->num_peers, &rows,
308 &rows_len);
274 tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink) * 309 tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink) *
275 tc->link_array_size); 310 tc->link_array_size);
276 if (0 == rows_len[rows - 1])
277 rows--;
278 cnt = 0; 311 cnt = 0;
279 offset = 0; 312 offset = 0;
280 for (y = 0; y < rows; y++) 313 for (y = 0; y < rows; y++)