From 10f4fb585af413a194c6ed1e138b06d1505a1f3d Mon Sep 17 00:00:00 2001 From: Bart Polot Date: Tue, 17 Dec 2013 02:27:28 +0000 Subject: - refactor and improve path creation --- src/mesh/gnunet-service-mesh_connection.c | 87 +++++++++++++++++++++++-------- src/mesh/gnunet-service-mesh_dht.c | 6 ++- src/mesh/gnunet-service-mesh_peer.c | 2 +- 3 files changed, 71 insertions(+), 24 deletions(-) diff --git a/src/mesh/gnunet-service-mesh_connection.c b/src/mesh/gnunet-service-mesh_connection.c index fa7211c12..157f73e1a 100644 --- a/src/mesh/gnunet-service-mesh_connection.c +++ b/src/mesh/gnunet-service-mesh_connection.c @@ -448,7 +448,7 @@ send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force) { LOG (GNUNET_ERROR_TYPE_DEBUG, "connection %s is origin in %s\n", GMC_2s (c), GM_f2s (fwd)); - GNUNET_assert (0); + GNUNET_assert (0); /* FIXME */ return; } @@ -1221,6 +1221,67 @@ add_to_peer (struct MeshConnection *c, struct MeshPeer *peer) GMT_add_connection (c->t, c); } + +/** + * Builds a path from a PeerIdentity array. + * + * @param peers PeerIdentity array. + * @param size Size of the @c peers array. + * @param own_pos Output parameter: own position in the path. + * + * @return Fixed and shortened path. + */ +static struct MeshPeerPath * +build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers, + unsigned int size, + unsigned int *own_pos) +{ + struct MeshPeerPath *path; + GNUNET_PEER_Id shortid; + unsigned int i; + unsigned int j; + unsigned int offset; + + /* Create path */ + LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n"); + path = path_new (size); + *own_pos = 0; + offset = 0; + for (i = 0; i < size; i++) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, " - %u: taking %s\n", + i, GNUNET_i2s (&peers[i])); + shortid = GNUNET_PEER_intern (&peers[i]); + + /* Check for loops / duplicates */ + for (j = 0; j < i - offset; j++) + { + if (path->peers[j] == shortid) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, " already exists at pos %u\n", j); + offset += i - j; + LOG (GNUNET_ERROR_TYPE_DEBUG, " offset now\n", offset); + GNUNET_PEER_change_rc (shortid, -1); + } + } + LOG (GNUNET_ERROR_TYPE_DEBUG, " storing at %u\n", i - offset); + path->peers[i - offset] = shortid; + if (path->peers[i] == myid) + *own_pos = i; + } + path->length -= offset; + + if (path->peers[*own_pos] != myid) + { + /* create path: self not found in path through self */ + GNUNET_break_op (0); + path_destroy (path); + return NULL; + } + + return path; +} + /******************************************************************************/ /******************************** API ***********************************/ /******************************************************************************/ @@ -1248,7 +1309,6 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer, struct MeshConnection *c; unsigned int own_pos; uint16_t size; - uint16_t i; LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n"); LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n"); @@ -1287,25 +1347,10 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer, c = connection_get (cid); if (NULL == c) { - /* Create path */ - LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n"); - path = path_new (size); - own_pos = 0; - for (i = 0; i < size; i++) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n", - GNUNET_i2s (&id[i])); - path->peers[i] = GNUNET_PEER_intern (&id[i]); - if (path->peers[i] == myid) - own_pos = i; - } - if (own_pos == 0 && path->peers[own_pos] != myid) - { - /* create path: self not found in path through self */ - GNUNET_break_op (0); - path_destroy (path); + path = build_path_from_peer_ids ((struct GNUNET_PeerIdentity *) &msg[1], + size, &own_pos); + if (NULL == path) return GNUNET_OK; - } LOG (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos); GMP_add_path_to_all (path, GNUNET_NO); LOG (GNUNET_ERROR_TYPE_DEBUG, " Creating connection\n"); @@ -1585,7 +1630,7 @@ GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer, fwd = is_fwd (c, peer); if (GNUNET_SYSERR == fwd) { - GNUNET_break_op (0); + GNUNET_break_op (0); /* FIXME */ return GNUNET_OK; } if (GNUNET_NO == GMC_is_terminal (c, fwd)) diff --git a/src/mesh/gnunet-service-mesh_dht.c b/src/mesh/gnunet-service-mesh_dht.c index 168706dcd..0e27eb9fa 100644 --- a/src/mesh/gnunet-service-mesh_dht.c +++ b/src/mesh/gnunet-service-mesh_dht.c @@ -110,6 +110,8 @@ static struct GNUNET_CONTAINER_MultiHashMap32 *get_requests; * to obtain a local peer -> destination path and interning the peer ids. * * @return Newly allocated and created path + * + * FIXME refactor and use build_path_from_peer_ids */ static struct MeshPeerPath * path_build_from_dht (const struct GNUNET_PeerIdentity *get_path, @@ -269,7 +271,7 @@ announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) } /** - * Iterator over hash map entries and stop GET requests before disconnecting + * Iterator over hash map entries and stop GET requests before disconnecting * from the DHT. * * @param cls Closure (unused) @@ -389,4 +391,4 @@ GMD_search_stop (struct GMD_search_handle *h) h->peer_id, h)); GNUNET_DHT_get_stop (h->dhtget); GNUNET_free (h); -} \ No newline at end of file +} diff --git a/src/mesh/gnunet-service-mesh_peer.c b/src/mesh/gnunet-service-mesh_peer.c index 23dd22222..9af0d34bf 100644 --- a/src/mesh/gnunet-service-mesh_peer.c +++ b/src/mesh/gnunet-service-mesh_peer.c @@ -783,7 +783,7 @@ queue_send (void *cls, size_t size, void *buf) queue = peer_get_first_message (peer); if (NULL == queue) { - GNUNET_break (0); /* Core tmt_rdy should've been canceled */ + GNUNET_assert (0); /* Core tmt_rdy should've been canceled FIXME */ return 0; } c = queue->c; -- cgit v1.2.3