aboutsummaryrefslogtreecommitdiff
path: root/src/mesh/gnunet-service-mesh.c
diff options
context:
space:
mode:
authorBart Polot <bart@net.in.tum.de>2011-06-22 07:01:29 +0000
committerBart Polot <bart@net.in.tum.de>2011-06-22 07:01:29 +0000
commit0b81657b642457b7e15414ded2f66b33ce6835c2 (patch)
treef0687dbeb3c79d4f1ae012c65a581d726fa3d2ef /src/mesh/gnunet-service-mesh.c
parent422b457fbe23cb25e998a021994d8367c58a60b7 (diff)
downloadgnunet-0b81657b642457b7e15414ded2f66b33ce6835c2.tar.gz
gnunet-0b81657b642457b7e15414ded2f66b33ce6835c2.zip
WiP (add path to peer algorithm added)
Diffstat (limited to 'src/mesh/gnunet-service-mesh.c')
-rw-r--r--src/mesh/gnunet-service-mesh.c60
1 files changed, 58 insertions, 2 deletions
diff --git a/src/mesh/gnunet-service-mesh.c b/src/mesh/gnunet-service-mesh.c
index 9cefef7a5..df4f39eed 100644
--- a/src/mesh/gnunet-service-mesh.c
+++ b/src/mesh/gnunet-service-mesh.c
@@ -371,7 +371,7 @@ get_peer_info (const struct GNUNET_PeerIdentity *peer)
371} 371}
372 372
373/** 373/**
374 * find the first peer whom to send a packet to go down this path 374 * Find the first peer whom to send a packet to go down this path
375 * @param path The path to use 375 * @param path The path to use
376 * @return short id of the next peer, myid in case of local delivery, 376 * @return short id of the next peer, myid in case of local delivery,
377 * or 0 in case of error 377 * or 0 in case of error
@@ -396,10 +396,66 @@ get_first_hop (struct MeshPath *path)
396 return 0; 396 return 0;
397} 397}
398 398
399
400/**
401 * Get the cost of the path.
402 * @param path The path to analyze
403 * @return Number of hops to reach destination, UINT_MAX in case the peer is not
404 * in the path
405 */
406static unsigned int
407get_path_cost(struct MeshPath *path)
408{
409 unsigned int i;
410
411 if (NULL == path) return UINT_MAX;
412 for (i = 0; i < path->length; i++) {
413 if (path->peers[i] == myid) {
414 return path->length - i;
415 }
416 }
417 return UINT_MAX;
418}
419
420
421/**
422 * Add the path to the peer and update the path used to reach it in case this
423 * is the shortest.
424 * @param peer_info Destination peer to add the path to.
425 * @param path New path to add. Last peer must be the peer in arg 1.
426 */
399static void 427static void
400add_path_to_peer(struct MeshPeerInfo *peer_info, struct MeshPath *path) 428add_path_to_peer(struct MeshPeerInfo *peer_info, struct MeshPath *path)
401{ 429{
402 430 unsigned int i;
431 unsigned int new_cost;
432 unsigned int best_cost;
433 struct MeshPath *aux;
434 struct MeshPath *best;
435
436 if (NULL == peer_info || NULL == path) return;
437
438 new_cost = get_path_cost(path);
439 best_cost = UINT_MAX;
440 best = NULL;
441 for (aux = peer_info->path; aux != NULL; aux = aux->next) {
442 if ((i = get_path_cost(aux)) < best_cost) {
443 best = aux;
444 best_cost = i;
445 }
446 }
447 if (best_cost < new_cost) {
448 path->in_use = 0;
449 GNUNET_CONTAINER_DLL_insert_tail(peer_info->path,
450 peer_info->path_tail,
451 path);
452 } else {
453 if (NULL != best) best->in_use = 0;
454 path->in_use = 1;
455 GNUNET_CONTAINER_DLL_insert(peer_info->path,
456 peer_info->path_tail,
457 path);
458 }
403 return; 459 return;
404} 460}
405 461