aux = NULL;
for (p = peer_d->path_head; NULL != p; p = p->next)
{
- if ((cost = path_get_cost (peer->tunnels[i]->tree, p)) < best)
+ if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
{
best = cost;
aux = p;
if (NULL != (p = peer->path_head))
{
best_p = p;
- best_cost = path_get_cost (t->tree, p);
+ best_cost = tree_get_path_cost (t->tree, p);
while (NULL != p)
{
- if ((cost = path_get_cost (t->tree, p)) < best_cost)
+ if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
{
best_cost = cost;
best_p = p;
}
else
{
- /* Start a DHT get if necessary */
+ /* Start a DHT get */
peer_info_connect (peer, t);
}
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got results from DHT!\n");
GNUNET_PEER_resolve (path_info->peer->id, &pi);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n", GNUNET_i2s (&pi));
-// GNUNET_DHT_get_stop(path_info->peer->dhtget);
-// path_info->peer->dhtget = NULL;
p = path_build_from_dht (get_path, get_path_length, put_path,
put_path_length);
tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
peer_info_connect (path_info->peer, path_info->t);
}
-// GNUNET_free (path_info);
return;
}
}
-/**
- * Get the cost of the path relative to the already built tunnel tree
- *
- * @param t The tunnel tree to which compare
- * @param path The individual path to reach a peer
- *
- * @return Number of hops to reach destination, UINT_MAX in case the peer is not
- * in the path
- *
- * TODO: remove dummy implementation, look into the tunnel tree
- */
-unsigned int
-path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
-{
- return path_get_length (path);
-}
-
-
/**
* Destroy the path and free any allocated resources linked to it
*
}
+
+/**
+ * Get the cost of the path relative to the already built tunnel tree.
+ *
+ * @param t The tunnel tree to which compare.
+ * @param path The individual path to reach a peer. It has to start at the
+ * root of the tree to be comparable.
+ *
+ * @return Number of hops to reach destination, UINT_MAX in case the peer is not
+ * in the path.
+ *
+ * TODO: adapt to allow any start / root combination
+ * TODO: take in account state of the nodes
+ */
+unsigned int
+tree_get_path_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
+{
+ struct MeshTunnelTreeNode *n;
+ struct MeshTunnelTreeNode *p;
+ unsigned int i;
+ unsigned int l;
+
+ l = path_get_length (path);
+ p = t->root;
+ if (t->root->peer != path->peers[0])
+ {
+ GNUNET_break (0);
+ return UINT_MAX;
+ }
+ for (i = 1; i < l; i++)
+ {
+ for (n = p->children_head; NULL != n; n = n->next)
+ {
+ if (path->peers[i] == n->peer)
+ {
+ break;
+ }
+ }
+ if (NULL == n)
+ return l - i;
+ p = n;
+ }
+ return l - i;
+}
+
+
/**
* Print the tree on stderr
*
path_get_length (struct MeshPeerPath *p);
-/**
- * Get the cost of the path relative to the already built tunnel tree
- *
- * @param t The tunnel tree to which compare
- * @param path The individual path to reach a peer
- *
- * @return Number of hops to reach destination, UINT_MAX in case the peer is not
- * in the path
- */
-unsigned int
-path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path);
-
-
/**
* Destroy the path and free any allocated resources linked to it
*
tree_del_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer,
MeshTreeCallback cb, void *cbcls);
+
+/**
+ * Get the cost of the path relative to the already built tunnel tree
+ *
+ * @param t The tunnel tree to which compare
+ * @param path The individual path to reach a peer
+ *
+ * @return Number of hops to reach destination, UINT_MAX in case the peer is not
+ * in the path
+ */
+unsigned int
+tree_get_path_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path);
+
+
/**
* Print the tree on stderr
*
test_assert (2, MESH_PEER_RELAY, 1, 0);
test_assert (1, MESH_PEER_ROOT, 1, 0);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Calculating costs...\n");
+ for (i = 1; i < 5; i++)
+ {
+ path->length = i;
+ if (tree_get_path_cost(tree, path) != 0)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "test: length %u cost failed!\n",
+ i);
+ failed++;
+ }
+ }
+ path->length++;
+ path->peers[4] = 6;
+ if (tree_get_path_cost(tree, path) != 1)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "test: length %u cost failed!\n",
+ i);
+ failed++;
+ }
+ path->peers[3] = 7;
+ if (tree_get_path_cost(tree, path) != 2)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "test: length %u cost failed!\n",
+ i);
+ failed++;
+ }
+ path->length--;
+ if (tree_get_path_cost(tree, path) != 1)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "test: length %u cost failed!\n",
+ i);
+ failed++;
+ }
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path (5)\n");
tree_set_status(tree, 5, MESH_PEER_READY);
cb_call = 1;