aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/testbed_api_topology.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-11-20 11:24:05 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-11-20 11:24:05 +0000
commit17111fe7d78609ef33fe99232f9e80c0b4741470 (patch)
treef2d192f51cbe409a580978d898192db5776635e7 /src/testbed/testbed_api_topology.c
parent41716910083763fdfc1bf796c96ba4cc76dd499d (diff)
downloadgnunet-17111fe7d78609ef33fe99232f9e80c0b4741470.tar.gz
gnunet-17111fe7d78609ef33fe99232f9e80c0b4741470.zip
scale free topology
Diffstat (limited to 'src/testbed/testbed_api_topology.c')
-rw-r--r--src/testbed/testbed_api_topology.c122
1 files changed, 97 insertions, 25 deletions
diff --git a/src/testbed/testbed_api_topology.c b/src/testbed/testbed_api_topology.c
index b42934f3b..17fb7070a 100644
--- a/src/testbed/testbed_api_topology.c
+++ b/src/testbed/testbed_api_topology.c
@@ -191,6 +191,28 @@ oprelease_overlay_configure_topology (void *cls)
191 191
192 192
193/** 193/**
194 * Populates the OverlayLink structure
195 *
196 * @param link the OverlayLink
197 * @param A the peer A
198 * @param B the peer B
199 * @param tc the TopologyContext
200 * @return
201 */
202static void
203make_link (struct OverlayLink *link,
204 uint32_t A,
205 uint32_t B,
206 struct TopologyContext *tc)
207{
208 link->A = A;
209 link->B = B;
210 link->op = NULL;
211 link->tc = tc;
212}
213
214
215/**
194 * Generates line topology 216 * Generates line topology
195 * 217 *
196 * @param tc the topology context 218 * @param tc the topology context
@@ -204,11 +226,7 @@ gen_topo_line (struct TopologyContext *tc)
204 tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink) * 226 tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink) *
205 tc->link_array_size); 227 tc->link_array_size);
206 for (cnt=0; cnt < (tc->num_peers - 1); cnt++) 228 for (cnt=0; cnt < (tc->num_peers - 1); cnt++)
207 { 229 make_link (&tc->link_array[cnt], cnt, cnt + 1, tc);
208 tc->link_array[cnt].A = cnt;
209 tc->link_array[cnt].B = cnt + 1;
210 tc->link_array[cnt].tc = tc;
211 }
212} 230}
213 231
214 232
@@ -225,10 +243,10 @@ gen_topo_ring (struct TopologyContext *tc)
225 tc->link_array = GNUNET_realloc (tc->link_array, 243 tc->link_array = GNUNET_realloc (tc->link_array,
226 sizeof (struct OverlayLink) * 244 sizeof (struct OverlayLink) *
227 tc->link_array_size); 245 tc->link_array_size);
228 tc->link_array[tc->link_array_size - 1].op = NULL; 246 make_link (&tc->link_array[tc->link_array_size - 1],
229 tc->link_array[tc->link_array_size - 1].tc = tc; 247 tc->num_peers - 1,
230 tc->link_array[tc->link_array_size - 1].A = tc->num_peers - 1; 248 0,
231 tc->link_array[tc->link_array_size - 1].B = 0; 249 tc);
232} 250}
233 251
234 252
@@ -314,16 +332,18 @@ gen_topo_2dtorus (struct TopologyContext *tc)
314 { 332 {
315 for (x = 0; x < rows_len[y] - 1; x++) 333 for (x = 0; x < rows_len[y] - 1; x++)
316 { 334 {
317 tc->link_array[cnt].tc = tc; 335 make_link (&tc->link_array[cnt],
318 tc->link_array[cnt].A = offset + x; 336 offset + x,
319 tc->link_array[cnt].B = offset + x + 1; 337 offset + x + 1,
338 tc);
320 cnt++; 339 cnt++;
321 } 340 }
322 if (0 == x) 341 if (0 == x)
323 break; 342 break;
324 tc->link_array[cnt].tc = tc; 343 make_link (&tc->link_array[cnt],
325 tc->link_array[cnt].A = offset + x; 344 offset + x,
326 tc->link_array[cnt].B = offset; 345 offset,
346 tc);
327 cnt++; 347 cnt++;
328 offset += rows_len[y]; 348 offset += rows_len[y];
329 } 349 }
@@ -335,17 +355,19 @@ gen_topo_2dtorus (struct TopologyContext *tc)
335 if (x == rows_len[y+1]) 355 if (x == rows_len[y+1])
336 break; 356 break;
337 GNUNET_assert (x < rows_len[y+1]); 357 GNUNET_assert (x < rows_len[y+1]);
338 tc->link_array[cnt].tc= tc; 358 make_link (&tc->link_array[cnt],
339 tc->link_array[cnt].A = offset + x; 359 offset + x,
360 offset + rows_len[y] + x,
361 tc);
340 offset += rows_len[y]; 362 offset += rows_len[y];
341 tc->link_array[cnt].B = offset + x;
342 cnt++; 363 cnt++;
343 } 364 }
344 if (0 == offset) 365 if (0 == offset)
345 break; 366 break;
346 tc->link_array[cnt].tc = tc; 367 make_link (&tc->link_array[cnt],
347 tc->link_array[cnt].A = offset + x; 368 offset + x,
348 tc->link_array[cnt].B = x; 369 x,
370 tc);
349 cnt++; 371 cnt++;
350 } 372 }
351 GNUNET_assert (cnt == tc->link_array_size); 373 GNUNET_assert (cnt == tc->link_array_size);
@@ -394,11 +416,58 @@ gen_topo_random (struct TopologyContext *tc, unsigned int links, int append)
394 B_rand = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 416 B_rand = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
395 tc->num_peers); 417 tc->num_peers);
396 } while (A_rand == B_rand); 418 } while (A_rand == B_rand);
397 tc->link_array[index + cnt].op = NULL; 419 make_link (&tc->link_array[index + cnt], A_rand, B_rand, tc);
398 tc->link_array[index + cnt].A = A_rand; 420 }
399 tc->link_array[index + cnt].B = B_rand; 421}
400 tc->link_array[index + cnt].tc = tc; 422
423
424/**
425 * Generates scale free network. Its construction is described in:
426 *
427 * "Emergence of Scaling in Random Networks." Science 286, 509-512, 1999.
428 *
429 * @param tc the topology context
430 * @param links the number of random links to establish
431 * @param append GNUNET_YES to add links to existing link array; GNUNET_NO to
432 * create a new link array
433 */
434static void
435gen_scale_free (struct TopologyContext *tc)
436{
437 double random;
438 double probability;
439 unsigned int cnt;
440 unsigned int previous_connections;
441 unsigned int i;
442 uint16_t *popularity;
443
444 popularity = GNUNET_malloc (sizeof (uint16_t) * tc->num_peers);
445 /* Initially connect peer 1 to peer 0 */
446 tc->link_array_size = 1;
447 tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink));
448 make_link (&tc->link_array[0], 0, 1, tc);
449 popularity[0]++; /* increase popularity of 0 as 1 connected to it*/
450 for (cnt = 1; cnt < tc->num_peers; cnt++)
451 {
452 previous_connections = tc->link_array_size;
453 for (i = 0; i < cnt; i++)
454 {
455 probability = ((double) popularity[i]) / ((double) previous_connections);
456 random = ((double)
457 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
458 UINT64_MAX)) / ((double) UINT64_MAX);
459 if (random < probability)
460 {
461 tc->link_array_size++;
462 tc->link_array = GNUNET_realloc (tc->link_array,
463 (sizeof (struct OverlayLink) *
464 tc->link_array_size));
465 make_link (&tc->link_array[tc->link_array_size - 1], cnt, i, tc);
466 popularity[cnt]++;
467 }
468 }
401 } 469 }
470 GNUNET_free (popularity);
402} 471}
403 472
404 473
@@ -536,6 +605,9 @@ GNUNET_TESTBED_overlay_configure_topology_va (void *op_cls,
536 va_arg (va, unsigned int), 605 va_arg (va, unsigned int),
537 GNUNET_YES); 606 GNUNET_YES);
538 break; 607 break;
608 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
609 gen_scale_free (tc);
610 break;
539 default: 611 default:
540 GNUNET_break (0); 612 GNUNET_break (0);
541 GNUNET_free (tc); 613 GNUNET_free (tc);