Fix testcase
[oweals/gnunet.git] / src / mesh / mesh_tunnel_tree.c
index 83ce893dc891df1a4a028b4b332044a53e0b72e9..c58260601caf4e66bd373bb29ae4e0fcf37f4f7b 100644 (file)
@@ -27,6 +27,8 @@
 #include "mesh.h"
 #include "mesh_tunnel_tree.h"
 
+#define MESH_TREE_DEBUG GNUNET_YES
+
 
 /**
  * Create a new path
@@ -157,6 +159,8 @@ path_get_cost (struct MeshTunnelTree *t, struct MeshPeerPath *path)
 int
 path_destroy (struct MeshPeerPath *p)
 {
+  if (NULL == p)
+    return GNUNET_OK;
   GNUNET_PEER_decrement_rcs (p->peers, p->length);
   GNUNET_free (p->peers);
   GNUNET_free (p);
@@ -196,6 +200,7 @@ static void
 tree_node_debug(struct MeshTunnelTreeNode *n, uint16_t level)
 {
   struct MeshTunnelTreeNode *c;
+  struct GNUNET_PeerIdentity id;;
   uint16_t i;
 
   for (i = 0; i < level; i++)
@@ -209,9 +214,13 @@ tree_node_debug(struct MeshTunnelTreeNode *n, uint16_t level)
   if (n->status == MESH_PEER_RECONNECTING)
     fprintf(stderr, "*");
 
-  fprintf(stderr, "%u [%p] ", n->peer, n);
+  GNUNET_PEER_resolve(n->peer, &id);
+  fprintf(stderr, "%s, [%u, %p] ", GNUNET_i2s (&id), n->peer, n);
   if (NULL != n->parent)
-    fprintf(stderr, "(-> %u)\n", n->parent->peer);
+  {
+    GNUNET_PEER_resolve(n->parent->peer, &id);
+    fprintf(stderr, "(-> %s [%u])\n", GNUNET_i2s(&id), n->parent->peer);
+  }
   else
     fprintf(stderr, "(root)\n");
   for (c = n->children_head; NULL != c; c = c->next)
@@ -229,7 +238,17 @@ tree_node_destroy (struct MeshTunnelTreeNode *parent)
 {
   struct MeshTunnelTreeNode *n;
   struct MeshTunnelTreeNode *next;
+#if MESH_TREE_DEBUG
+  struct GNUNET_PeerIdentity id;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "tree: Destroying node %u\n",
+              parent->peer);
+  GNUNET_PEER_resolve (parent->peer, &id);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "tree:   (%s)\n",
+              GNUNET_i2s (&id));
+#endif
   n = parent->children_head;
   while (NULL != n)
   {
@@ -317,11 +336,12 @@ tree_mark_peers_disconnected (struct MeshTunnelTree *tree,
   {
     tree_mark_peers_disconnected (tree, n, cb);
   }
-  if (MESH_PEER_READY == parent->status && NULL != cb)
+  if (MESH_PEER_READY == parent->status)
   {
-    cb (parent);
+    if (NULL != cb)
+      cb (parent);
+    parent->status = MESH_PEER_RECONNECTING;
   }
-  parent->status = MESH_PEER_RECONNECTING;
 
   /* Remove and free info about first hop */
   GNUNET_PEER_resolve(parent->peer, &id);
@@ -350,36 +370,47 @@ tree_update_first_hops (struct MeshTunnelTree *tree,
   struct GNUNET_PeerIdentity id;
   struct MeshTunnelTreeNode *n;
 
+#if MESH_TREE_DEBUG
+  GNUNET_PEER_resolve(parent->peer, &id);
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Finding first hop for %u.\n",
-             parent->peer);
+          "tree:   Finding first hop for %s.\n",
+          GNUNET_i2s (&id));
+#endif
   if (NULL == hop)
   {
     struct MeshTunnelTreeNode *aux;
     struct MeshTunnelTreeNode *old;
 
-    old = parent;
-    aux = old->parent;
+    aux = old = parent;
     while (aux != tree->me)
     {
+#if MESH_TREE_DEBUG
+      GNUNET_PEER_resolve(old->peer, &id);
       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   ... its not %u.\n",
-             old->peer);
+             "tree:   ... its not %s.\n",
+             GNUNET_i2s (&id));
+#endif
       old = aux;
       aux = aux->parent;
       GNUNET_assert(NULL != aux);
     }
+#if MESH_TREE_DEBUG
+    GNUNET_PEER_resolve(old->peer, &id);
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   It's %u!!\n",
-             old->peer);
+               "tree:   It's %s!\n",
+               GNUNET_i2s (&id));
+#endif
     hop = &pi;
     GNUNET_PEER_resolve(old->peer, hop);
   }
   copy = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
   *copy = *hop;
   GNUNET_PEER_resolve(parent->peer, &id);
-  GNUNET_CONTAINER_multihashmap_put(tree->first_hops, &id.hashPubKey, copy,
-                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+  GNUNET_CONTAINER_multihashmap_put(
+    tree->first_hops,
+    &id.hashPubKey,
+    copy,
+    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
 
   for (n = parent->children_head; NULL != n; n = n->next)
   {
@@ -401,15 +432,21 @@ tree_update_first_hops (struct MeshTunnelTree *tree,
  *         NULL when not found
  */
 struct MeshTunnelTreeNode *
-tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
-                 MeshNodeDisconnectCB cb)
+tree_del_path (struct MeshTunnelTree *t,
+               GNUNET_PEER_Id peer_id,
+               MeshNodeDisconnectCB cb)
 {
   struct MeshTunnelTreeNode *parent;
   struct MeshTunnelTreeNode *node;
   struct MeshTunnelTreeNode *n;
 
+#if MESH_TREE_DEBUG
+  struct GNUNET_PeerIdentity id;
+  GNUNET_PEER_resolve(peer_id, &id);
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Deleting path to %u.\n", peer_id);
+          "tree:   Deleting path to %s.\n",
+          GNUNET_i2s (&id));
+#endif
   if (peer_id == t->root->peer)
     return NULL;
 
@@ -435,16 +472,22 @@ tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id,
 
   while (MESH_PEER_RELAY == parent->status && NULL == parent->children_head)
   {
+#if MESH_TREE_DEBUG
+    GNUNET_PEER_resolve(parent->peer, &id);
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-               "tree:   Deleting node %u.\n",
-               parent->peer);
+              "tree:   Deleting node %s.\n",
+              GNUNET_i2s (&id));
+#endif
     n = parent->parent;
     tree_node_destroy(parent);
     parent = n;
   }
+#if MESH_TREE_DEBUG
+  GNUNET_PEER_resolve(parent->peer, &id);
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Not deleted peer %u.\n",
-             parent->peer);
+             "tree:   Not deleted peer %s.\n",
+             GNUNET_i2s (&id));
+#endif
 
   tree_mark_peers_disconnected (t, node, cb);
 
@@ -522,12 +565,16 @@ tree_add_path (struct MeshTunnelTree *t,
   struct GNUNET_PeerIdentity id;
   GNUNET_PEER_Id myid;
   int me;
+//   int oldnode_is_me;
   unsigned int i;
 
+#if MESH_TREE_DEBUG
+  GNUNET_PEER_resolve(p->peers[p->length - 1], &id);
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Adding path [%u] towards peer %u.\n",
-             p->length,
-             p->peers[p->length - 1]);
+             "tree:   Adding path [%u] towards peer %s.\n",
+            p->length,
+            GNUNET_i2s (&id));
+#endif
 
   if (NULL != t->me)
     myid = t->me->peer;
@@ -552,9 +599,12 @@ tree_add_path (struct MeshTunnelTree *t,
   me = t->root->peer == myid ? 0 : -1;
   for (i = 1; i < p->length; i++)
   {
+#if MESH_TREE_DEBUG
+    GNUNET_PEER_resolve(p->peers[i], &id);
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Looking for peer %u.\n",
-             p->peers[i]);
+               "tree:   Looking for peer %s.\n",
+               GNUNET_i2s (&id));
+#endif
     parent = n;
     if (p->peers[i] == myid)
       me = i;
@@ -562,9 +612,12 @@ tree_add_path (struct MeshTunnelTree *t,
     {
       if (c->peer == p->peers[i])
       {
+#if MESH_TREE_DEBUG
+        GNUNET_PEER_resolve(parent->peer, &id);
         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-             "tree:   Found in children of %u.\n",
-             parent->peer);
+                   "tree:   Found in children of %s.\n",
+                   GNUNET_i2s (&id));
+#endif
         n = c;
         break;
       }
@@ -574,20 +627,30 @@ tree_add_path (struct MeshTunnelTree *t,
     if (parent == n)
       break;
   }
+#if MESH_TREE_DEBUG
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
              "tree:   All childen visited.\n");
+#endif
   /* Add the rest of the path as a branch from parent. */
   while (i < p->length)
   {
+#if MESH_TREE_DEBUG
+    GNUNET_PEER_resolve(p->peers[i], &id);
+    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+               "tree:   Adding  peer %s.\n",
+               GNUNET_i2s (&id));
+    GNUNET_PEER_resolve(parent->peer, &id);
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-               "tree:   Adding  peer %u, to %u.\n",
-               p->peers[i],
-               parent->peer);
+               "tree:     to %s.\n",
+               GNUNET_i2s (&id));
+#endif
 
     if (i == p->length - 1 && NULL != oldnode)
     {
+#if MESH_TREE_DEBUG
       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
                  "tree:   Putting old node into place.\n");
+#endif
       oldnode->parent = parent;
       GNUNET_CONTAINER_DLL_insert(parent->children_head,
                                   parent->children_tail,
@@ -597,7 +660,9 @@ tree_add_path (struct MeshTunnelTree *t,
     }
     else
     {
+#if MESH_TREE_DEBUG
       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   Creating new node.\n");
+#endif
       n = tree_node_new(parent, p->peers[i]);
       n->t = t->t;
       n->status = MESH_PEER_RELAY;
@@ -610,15 +675,19 @@ tree_add_path (struct MeshTunnelTree *t,
   /* Add info about first hop into hashmap. */
   if (-1 != me && me < p->length - 1)
   {
+#if MESH_TREE_DEBUG
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "MESH:   finding first hop (own pos %d/%u)\n",
                 me, p->length - 1);
+#endif
     GNUNET_PEER_resolve (p->peers[me + 1], &id);
     tree_update_first_hops(t,
                            tree_find_peer(t->root, p->peers[p->length - 1]),
                            &id);
   }
+#if MESH_TREE_DEBUG
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "tree:   New node added.\n");
+#endif
   return GNUNET_OK;
 }
 
@@ -745,6 +814,9 @@ iterate_free (void *cls, const GNUNET_HashCode * key, void *value)
 void
 tree_destroy (struct MeshTunnelTree *t)
 {
+#if MESH_TREE_DEBUG
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tree: Destroying tree\n");
+#endif
   tree_node_destroy(t->root);
   GNUNET_CONTAINER_multihashmap_iterate(t->first_hops, &iterate_free, NULL);
   GNUNET_CONTAINER_multihashmap_destroy(t->first_hops);