From 5d4dd95a17d90d532b52e1060dd259f80a333931 Mon Sep 17 00:00:00 2001 From: Bart Polot Date: Wed, 2 Nov 2011 14:34:27 +0000 Subject: [PATCH] Make tree library independent from tunnels, add cls to callbacks --- src/mesh/gnunet-service-mesh.c | 35 ++++++++++++++--------------- src/mesh/mesh_tunnel_tree.c | 40 +++++++++++++++++++--------------- src/mesh/mesh_tunnel_tree.h | 36 +++++++++++++++--------------- src/mesh/test_mesh_path_api.c | 18 +++++++-------- 4 files changed, 68 insertions(+), 61 deletions(-) diff --git a/src/mesh/gnunet-service-mesh.c b/src/mesh/gnunet-service-mesh.c index 9e37345d0..54f46379a 100644 --- a/src/mesh/gnunet-service-mesh.c +++ b/src/mesh/gnunet-service-mesh.c @@ -1496,7 +1496,7 @@ path_remove_from_peer (struct MeshPeerInfo *peer, if (NULL != aux) { /* No callback, as peer will be already disconnected */ - tree_add_path(peer->tunnels[i]->tree, aux, NULL); + tree_add_path(peer->tunnels[i]->tree, aux, NULL, NULL); } else { @@ -1808,30 +1808,30 @@ tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid) * Callback used to notify a client owner of a tunnel that a peer has * disconnected, most likely because of a path change. * - * @param n Node in the tree representing the disconnected peer - * - * FIXME: pass tunnel via cls, make param just a peer identity + * @param cls Closure (tunnel this notification is about). + * @param peer_id Short ID of disconnected peer. */ void -notify_peer_disconnected (const struct MeshTunnelTreeNode *n) +notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id) { + struct MeshTunnel *t = cls; struct MeshPeerInfo *peer; struct MeshPathInfo *path_info; - if (NULL != n->t->client && NULL != nc) + if (NULL != t->client && NULL != nc) { struct GNUNET_MESH_PeerControl msg; msg.header.size = htons (sizeof (msg)); msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL); - msg.tunnel_id = htonl (n->t->local_tid); - GNUNET_PEER_resolve (n->peer, &msg.peer); - GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle, + msg.tunnel_id = htonl (t->local_tid); + GNUNET_PEER_resolve (peer_id, &msg.peer); + GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, &msg.header, GNUNET_NO); } - peer = peer_info_get_short(n->peer); + peer = peer_info_get_short(peer_id); path_info = GNUNET_malloc (sizeof (struct MeshPathInfo)); path_info->peer = peer; - path_info->t = n->t; + path_info->t = t; GNUNET_SCHEDULER_add_now(&peer_info_connect_task, path_info); } @@ -1880,7 +1880,7 @@ tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer) } p = p->next; } - tree_add_path (t->tree, best_p, ¬ify_peer_disconnected); + tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t); if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task) t->path_refresh_task = GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t); @@ -1910,7 +1910,7 @@ tunnel_add_path (struct MeshTunnel *t, struct GNUNET_PeerIdentity id; GNUNET_assert (0 != own_pos); - tree_add_path(t->tree, p, NULL); + tree_add_path(t->tree, p, NULL, NULL); if (NULL == t->tree->me) t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]); if (own_pos < p->length - 1) @@ -1944,7 +1944,8 @@ tunnel_notify_connection_broken (struct MeshTunnel *t, pid = tree_notify_connection_broken (t->tree, p1, p2, - ¬ify_peer_disconnected); + ¬ify_peer_disconnected, + t); if (myid != p1 && myid != p2) { return pid; @@ -2171,7 +2172,7 @@ static void tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer) { - GNUNET_break (GNUNET_OK == tree_del_peer (t->tree, peer, NULL)); + GNUNET_break (GNUNET_OK == tree_del_peer (t->tree, peer, NULL, NULL)); if (NULL == t->tree->root) tunnel_destroy (t); } @@ -2513,7 +2514,7 @@ handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer, next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV; t->local_tid = next_local_tid++; next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV; - t->tree = tree_new(t, t->id.oid); + t->tree = tree_new(t->id.oid); GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash); if (GNUNET_OK != @@ -3590,7 +3591,7 @@ handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client, GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } - t->tree = tree_new (t, myid); + t->tree = tree_new (myid); t->tree->refresh = REFRESH_PATH_TIME; t->tree->root->status = MESH_PEER_READY; t->tree->me = t->tree->root; diff --git a/src/mesh/mesh_tunnel_tree.c b/src/mesh/mesh_tunnel_tree.c index d052a3c4d..deb702604 100644 --- a/src/mesh/mesh_tunnel_tree.c +++ b/src/mesh/mesh_tunnel_tree.c @@ -293,7 +293,7 @@ tree_node_destroy (struct MeshTunnelTreeNode *parent) * @return A newly allocated and initialized tunnel tree */ struct MeshTunnelTree * -tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer) +tree_new (GNUNET_PEER_Id peer) { struct MeshTunnelTree *tree; @@ -301,8 +301,6 @@ tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer) tree->first_hops = GNUNET_CONTAINER_multihashmap_create(32); tree->root = tree_node_new(NULL, peer); tree->root->status = MESH_PEER_ROOT; - tree->t = t; - tree->root->t = t; return tree; } @@ -344,7 +342,8 @@ tree_find_peer (struct MeshTunnelTreeNode *parent, GNUNET_PEER_Id peer_id) static void tree_mark_peers_disconnected (struct MeshTunnelTree *tree, struct MeshTunnelTreeNode *parent, - MeshNodeDisconnectCB cb) + MeshNodeDisconnectCB cb, + void *cbcls) { struct GNUNET_PeerIdentity *pi; struct GNUNET_PeerIdentity id; @@ -352,12 +351,12 @@ tree_mark_peers_disconnected (struct MeshTunnelTree *tree, for (n = parent->children_head; NULL != n; n = n->next) { - tree_mark_peers_disconnected (tree, n, cb); + tree_mark_peers_disconnected (tree, n, cb, cbcls); } if (MESH_PEER_READY == parent->status) { if (NULL != cb) - cb (parent); + cb (cbcls, parent->peer); parent->status = MESH_PEER_RECONNECTING; } @@ -448,6 +447,7 @@ tree_update_first_hops (struct MeshTunnelTree *tree, * @param t Tunnel tree where to delete the path from. * @param peer Destination peer whose path we want to remove. * @param cb Callback to use to notify about disconnected peers. + * @param cbcls Closure for cb. * * @return pointer to the pathless node. * NULL when not found @@ -455,7 +455,8 @@ tree_update_first_hops (struct MeshTunnelTree *tree, struct MeshTunnelTreeNode * tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer_id, - MeshNodeDisconnectCB cb) + MeshNodeDisconnectCB cb, + void *cbcls) { struct MeshTunnelTreeNode *parent; struct MeshTunnelTreeNode *node; @@ -510,7 +511,7 @@ tree_del_path (struct MeshTunnelTree *t, GNUNET_i2s (&id)); #endif - tree_mark_peers_disconnected (t, node, cb); + tree_mark_peers_disconnected (t, node, cb, cbcls); return node; } @@ -567,7 +568,8 @@ tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer) * * @param t Tunnel where to add the new path. * @param p Path to be integrated. - * @param cb Callback to use to notify about peers temporarily disconnecting + * @param cb Callback to use to notify about peers temporarily disconnecting. + * @param cbcls Closure for cb. * * @return GNUNET_OK in case of success. * GNUNET_SYSERR in case of error. @@ -579,7 +581,8 @@ tree_get_path_to_peer(struct MeshTunnelTree *t, GNUNET_PEER_Id peer) int tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p, - MeshNodeDisconnectCB cb) + MeshNodeDisconnectCB cb, + void *cbcls) { struct MeshTunnelTreeNode *parent; struct MeshTunnelTreeNode *oldnode; @@ -611,7 +614,7 @@ tree_add_path (struct MeshTunnelTree *t, } if (1 == p->length) return GNUNET_OK; - oldnode = tree_del_path (t, p->peers[p->length - 1], cb); + oldnode = tree_del_path (t, p->peers[p->length - 1], cb, cbcls); /* Look for the first node that is not already present in the tree * * Assuming that the tree is somewhat balanced, O(log n * log N). @@ -689,7 +692,6 @@ tree_add_path (struct MeshTunnelTree *t, 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; if (n->peer == myid) t->me = n; @@ -727,6 +729,7 @@ tree_add_path (struct MeshTunnelTree *t, * @param p1 Short id of one of the peers (order unimportant) * @param p2 Short id of one of the peers (order unimportant) * @param cb Function to call for every peer that is marked as disconnected. + * @param cbcls Closure for cb. * * @return Short ID of the first disconnected peer in the tree. */ @@ -734,7 +737,8 @@ GNUNET_PEER_Id tree_notify_connection_broken (struct MeshTunnelTree *t, GNUNET_PEER_Id p1, GNUNET_PEER_Id p2, - MeshNodeDisconnectCB cb) + MeshNodeDisconnectCB cb, + void *cbcls) { struct MeshTunnelTreeNode *n; struct MeshTunnelTreeNode *c; @@ -744,7 +748,7 @@ tree_notify_connection_broken (struct MeshTunnelTree *t, return 0; if (NULL != n->parent && n->parent->peer == p2) { - tree_mark_peers_disconnected(t, n, cb); + tree_mark_peers_disconnected(t, n, cb, cbcls); GNUNET_CONTAINER_DLL_remove(n->parent->children_head, n->parent->children_tail, n); @@ -757,7 +761,7 @@ tree_notify_connection_broken (struct MeshTunnelTree *t, { if (c->peer == p2) { - tree_mark_peers_disconnected(t, c, cb); + tree_mark_peers_disconnected(t, c, cb, cbcls); GNUNET_CONTAINER_DLL_remove(n->children_head, n->children_tail, c); @@ -780,17 +784,19 @@ tree_notify_connection_broken (struct MeshTunnelTree *t, * @param t Tunnel tree to use. * @param peer Short ID of the peer to remove from the tunnel tree. * @param cb Callback to notify client of disconnected peers. + * @param cbcls Closure for cb. * * @return GNUNET_OK or GNUNET_SYSERR */ int tree_del_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer, - MeshNodeDisconnectCB cb) + MeshNodeDisconnectCB cb, + void *cbcls) { struct MeshTunnelTreeNode *n; - n = tree_del_path(t, peer, cb); + n = tree_del_path(t, peer, cb, cbcls); if (NULL == n) return GNUNET_SYSERR; GNUNET_break_op (NULL == n->children_head); diff --git a/src/mesh/mesh_tunnel_tree.h b/src/mesh/mesh_tunnel_tree.h index 59a60839d..2cb28a28c 100644 --- a/src/mesh/mesh_tunnel_tree.h +++ b/src/mesh/mesh_tunnel_tree.h @@ -60,11 +60,6 @@ struct MeshPeerPath */ struct MeshTunnelTreeNode { - /** - * Tunnel this node belongs to (and therefore tree) - */ - struct MeshTunnel *t; - /** * Peer this node describes */ @@ -112,11 +107,6 @@ struct MeshTunnelTree */ struct GNUNET_TIME_Relative refresh; - /** - * Tunnel this path belongs to - */ - struct MeshTunnel *t; - /** * Root node of peer tree */ @@ -236,9 +226,11 @@ path_destroy (struct MeshPeerPath *p); /** * Method called whenever a node has been marked as disconnected. * - * @param node peer identity the tunnel stopped working with + * @param cls Closure. + * @param peer_id short ID of peer that is no longer reachable. */ -typedef void (*MeshNodeDisconnectCB) (const struct MeshTunnelTreeNode * node); +typedef void (*MeshNodeDisconnectCB) (void *cls, + GNUNET_PEER_Id peer_id); /** @@ -250,7 +242,7 @@ typedef void (*MeshNodeDisconnectCB) (const struct MeshTunnelTreeNode * node); * @return A newly allocated and initialized tunnel tree */ struct MeshTunnelTree * -tree_new (struct MeshTunnel *t, GNUNET_PEER_Id peer); +tree_new (GNUNET_PEER_Id peer); /** @@ -288,6 +280,7 @@ tree_update_first_hops (struct MeshTunnelTree *tree, * @param peer Destination peer whose path we want to remove. * @param cb Callback to use to notify about which peers are going to be * disconnected. + * @param cbcls Closure for cb. * * @return pointer to the pathless node. * NULL when not found @@ -295,7 +288,8 @@ tree_update_first_hops (struct MeshTunnelTree *tree, struct MeshTunnelTreeNode * tree_del_path (struct MeshTunnelTree *t, GNUNET_PEER_Id peer, - MeshNodeDisconnectCB cb); + MeshNodeDisconnectCB cb, + void *cbcls); /** @@ -318,7 +312,8 @@ tree_get_path_to_peer(struct MeshTunnelTree *t, * * @param t Tunnel where to add the new path. * @param p Path to be integrated. - * @param cb Callback to use to notify about peers temporarily disconnecting + * @param cb Callback to use to notify about peers temporarily disconnecting. + * @param cbcls Closure for cb. * * @return GNUNET_OK in case of success. * GNUNET_SYSERR in case of error. @@ -326,7 +321,8 @@ tree_get_path_to_peer(struct MeshTunnelTree *t, int tree_add_path (struct MeshTunnelTree *t, const struct MeshPeerPath *p, - MeshNodeDisconnectCB cb); + MeshNodeDisconnectCB cb, + void *cbcls); /** @@ -337,6 +333,7 @@ tree_add_path (struct MeshTunnelTree *t, * @param p1 Short id of one of the peers (order unimportant) * @param p2 Short id of one of the peers (order unimportant) * @param cb Function to call for every peer that is marked as disconnected. + * @param cbcls Closure for cb. * * @return Short ID of the first disconnected peer in the tree. */ @@ -344,7 +341,8 @@ GNUNET_PEER_Id tree_notify_connection_broken (struct MeshTunnelTree *t, GNUNET_PEER_Id p1, GNUNET_PEER_Id p2, - MeshNodeDisconnectCB cb); + MeshNodeDisconnectCB cb, + void *cbcls); /** @@ -356,13 +354,15 @@ tree_notify_connection_broken (struct MeshTunnelTree *t, * @param t Tunnel tree to use. * @param peer Short ID of the peer to remove from the tunnel tree. * @param cb Callback to notify client of disconnected peers. + * @param cbcls Closure for cb. * * @return GNUNET_OK or GNUNET_SYSERR */ int tree_del_peer (struct MeshTunnelTree *t, GNUNET_PEER_Id peer, - MeshNodeDisconnectCB cb); + MeshNodeDisconnectCB cb, + void *cbcls); /** * Print the tree on stderr diff --git a/src/mesh/test_mesh_path_api.c b/src/mesh/test_mesh_path_api.c index e7c2a6fdf..45c4f3c14 100644 --- a/src/mesh/test_mesh_path_api.c +++ b/src/mesh/test_mesh_path_api.c @@ -40,9 +40,9 @@ struct GNUNET_PeerIdentity* pi[10]; struct MeshTunnelTree *tree; static void -cb (const struct MeshTunnelTreeNode *n) +cb (void *cls, GNUNET_PEER_Id peer_id) { - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: CB: Disconnected %u\n", n->peer); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: CB: Disconnected %u\n", peer_id); if(0 == cb_call) { GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: and it shouldn't!\n"); @@ -181,7 +181,7 @@ main (int argc, char *argv[]) path->length = 4; GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3 4\n"); - tree_add_path(tree, path, &cb); + tree_add_path(tree, path, &cb, NULL); tree_debug(tree); path1 = tree_get_path_to_peer(tree, 4); if (path->length != path1->length || @@ -198,7 +198,7 @@ main (int argc, char *argv[]) GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding second path: 1 2 3\n"); path->length--; - tree_add_path(tree, path, &cb); + tree_add_path(tree, path, &cb, NULL); tree_debug(tree); test_assert (4, MESH_PEER_SEARCHING, 0, 2); @@ -209,7 +209,7 @@ main (int argc, char *argv[]) GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding third path...\n"); path->length++; path->peers[3] = 5; - tree_add_path(tree, path, &cb); + tree_add_path(tree, path, &cb, NULL); tree_debug(tree); test_assert (5, MESH_PEER_SEARCHING, 0, 2); @@ -222,7 +222,7 @@ main (int argc, char *argv[]) GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Deleting third path...\n"); node->status = MESH_PEER_READY; cb_call = 1; - node2 = tree_del_path(tree, 5, &cb); + node2 = tree_del_path(tree, 5, &cb, NULL); tree_debug(tree); if (cb_call != 0) { @@ -249,7 +249,7 @@ main (int argc, char *argv[]) path->peers[1] = 4; cb_call = 1; tree_find_peer(tree->root, 4)->status = MESH_PEER_READY; - tree_add_path(tree, path, cb); + tree_add_path(tree, path, &cb, NULL); tree_debug(tree); if (cb_call != 0) { @@ -287,7 +287,7 @@ main (int argc, char *argv[]) path->length = 3; GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding first path: 1 2 3\n"); - tree_add_path(tree, path, &cb); + tree_add_path(tree, path, &cb, NULL); tree_debug(tree); tree->me = tree_find_peer (tree->root, 2); @@ -296,7 +296,7 @@ main (int argc, char *argv[]) test_assert (1, MESH_PEER_ROOT, 1, 0); GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test: Adding same path: 1 2 3\n"); - tree_add_path(tree, path, &cb); + tree_add_path(tree, path, &cb, NULL); GNUNET_free (path->peers); GNUNET_free (path); -- 2.25.1