- FIX: GNUNET_SET_STATUS_HALF_DONE is never called only GNUNET_SET_STATUS_DONE
[oweals/gnunet.git] / src / dv / gnunet-service-dv.c
index 31512b2b344f2e080cfd346ee520fd2a2dfc3dc7..84bdc11c23020c69c34710f043ee0140921206ff 100644 (file)
@@ -249,8 +249,16 @@ struct DirectNeighbor
    */
   unsigned int pm_queue_size;
 
+  /**
+   * Elements in consensus
+   */
   unsigned int consensus_elements;
 
+  /**
+   * Direct one hop route
+   */
+  struct Route *direct_route;
+
   /**
    * Flag set within 'check_target_removed' to trigger full global route refresh.
    */
@@ -764,7 +772,6 @@ build_set (void *cls)
   struct DirectNeighbor *neighbor = cls;
   struct GNUNET_SET_Element element;
   struct Target *target;
-
   target = NULL;
   while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
          (consensi[neighbor->consensus_insertion_distance].array_length == neighbor->consensus_insertion_offset) )
@@ -772,7 +779,6 @@ build_set (void *cls)
     /* If we reached the last element of a consensus array element: increase distance and start with next array */
     neighbor->consensus_insertion_offset = 0;
     neighbor->consensus_insertion_distance++;
-
     /* skip over NULL entries */
     while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
            (consensi[neighbor->consensus_insertion_distance].array_length  > neighbor->consensus_insertion_offset) &&
@@ -794,19 +800,9 @@ build_set (void *cls)
   }
 
   target = &consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]->target;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Adding peer `%s' with distance %u to SET\n",
-              GNUNET_i2s (&target->peer),
-              ntohl (target->distance));
   element.size = sizeof (struct Target);
   element.type = htons (0); /* do we need this? */
   element.data = target;
-  neighbor->consensus_elements++;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Adding element to SET\n");
-  GNUNET_SET_add_element (neighbor->my_set,
-                          &element,
-                          &build_set, neighbor);
 
   /* Find next non-NULL entry */
   neighbor->consensus_insertion_offset++;
@@ -814,7 +810,25 @@ build_set (void *cls)
   while ( (DEFAULT_FISHEYE_DEPTH - 1 > neighbor->consensus_insertion_distance) &&
          (consensi[neighbor->consensus_insertion_distance].array_length > neighbor->consensus_insertion_offset) &&
          (NULL == consensi[neighbor->consensus_insertion_distance].targets[neighbor->consensus_insertion_offset]) )
+  {
     neighbor->consensus_insertion_offset++;
+  }
+
+  if ( (0 != memcmp(&target->peer, &my_identity, sizeof (my_identity))) &&
+       (0 != memcmp(&target->peer, &neighbor->peer, sizeof (neighbor->peer))) )
+  {
+    /* Add target if it is not the neighbor or this peer */
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Adding peer `%s' with distance %u to SET\n",
+                GNUNET_i2s (&target->peer),
+                ntohl (target->distance));
+    GNUNET_SET_add_element (neighbor->my_set,
+                            &element,
+                            &build_set, neighbor);
+    neighbor->consensus_elements++;
+  }
+  else
+    build_set(neighbor);
 }
 
 
@@ -846,15 +860,15 @@ handle_direct_connect (struct DirectNeighbor *neighbor)
     GNUNET_free (route);
   }
 
-  route = GNUNET_new (struct Route);
-  route->next_hop = neighbor;
-  route->target.peer= neighbor->peer;
-  route->target.distance = DIRECT_NEIGHBOR_COST;
-  allocate_route (route, DIRECT_NEIGHBOR_COST);
+  neighbor->direct_route = GNUNET_new (struct Route);
+  neighbor->direct_route->next_hop = neighbor;
+  neighbor->direct_route->target.peer= neighbor->peer;
+  neighbor->direct_route->target.distance = DIRECT_NEIGHBOR_COST;
+  allocate_route (neighbor->direct_route, DIRECT_NEIGHBOR_COST);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Adding direct route to %s\n",
-              GNUNET_i2s (&route->target.peer));
+              GNUNET_i2s (&neighbor->direct_route->target.peer));
 
 
   /* construct session ID seed as XOR of both peer's identities */
@@ -873,18 +887,31 @@ handle_direct_connect (struct DirectNeighbor *neighbor)
                  &my_identity,
                  sizeof (struct GNUNET_PeerIdentity)))
   {
-    neighbor->initiate_task = GNUNET_SCHEDULER_add_now (&initiate_set_union,
+    if (NULL != neighbor->listen_handle)
+    {
+      GNUNET_break (0);
+    }
+    else
+      neighbor->initiate_task = GNUNET_SCHEDULER_add_now (&initiate_set_union,
                                                        neighbor);
   }
   else
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Starting SET listen operation\n");
-    neighbor->listen_handle = GNUNET_SET_listen (cfg,
-                                                GNUNET_SET_OPERATION_UNION,
-                                                &neighbor->real_session_id,
-                                                &listen_set_union,
-                                                neighbor);
+    if (NULL != neighbor->listen_handle)
+    {
+      GNUNET_break (0);
+    }
+    else
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Starting SET listen operation with peer `%s'\n",
+                  GNUNET_i2s(&neighbor->peer));
+      neighbor->listen_handle = GNUNET_SET_listen (cfg,
+                                                   GNUNET_SET_OPERATION_UNION,
+                                                   &neighbor->real_session_id,
+                                                   &listen_set_union,
+                                                   neighbor);
+    }
   }
 }
 
@@ -1096,6 +1123,14 @@ handle_direct_disconnect (struct DirectNeighbor *neighbor)
     GNUNET_CORE_notify_transmit_ready_cancel (neighbor->cth);
     neighbor->cth = NULL;
   }
+
+  if (NULL != neighbor->direct_route)
+  {
+    release_route (neighbor->direct_route);
+    GNUNET_free (neighbor->direct_route);
+    neighbor->direct_route = NULL;
+  }
+
   if (NULL != neighbor->neighbor_table_consensus)
   {
     GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
@@ -1338,10 +1373,32 @@ handle_set_union_result (void *cls,
 {
   struct DirectNeighbor *neighbor = cls;
   struct Target *target;
+  char *status_str;
+
+  switch (status) {
+    case GNUNET_SET_STATUS_OK:
+      status_str = "GNUNET_SET_STATUS_OK";
+      break;
+    case GNUNET_SET_STATUS_TIMEOUT:
+      status_str = "GNUNET_SET_STATUS_TIMEOUT";
+      break;
+    case GNUNET_SET_STATUS_FAILURE:
+      status_str = "GNUNET_SET_STATUS_FAILURE";
+      break;
+    case GNUNET_SET_STATUS_HALF_DONE:
+      status_str = "GNUNET_SET_STATUS_HALF_DONE";
+      break;
+    case GNUNET_SET_STATUS_DONE:
+      status_str = "GNUNET_SET_STATUS_DONE";
+      break;
+    default:
+      status_str = "UNDEFINED";
+      break;
+  }
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Got SET union result: %d\n",
-             status);
+             "Got SET union result: %s\n",
+             status_str);
   switch (status)
   {
   case GNUNET_SET_STATUS_OK:
@@ -1352,6 +1409,11 @@ handle_set_union_result (void *cls,
     }
     target = GNUNET_new (struct Target);
     memcpy (target, element->data, sizeof (struct Target));
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Received information about peer `%s' with distance %u\n",
+                GNUNET_i2s (&target->peer), ntohl(target->distance));
+    if (NULL == neighbor->neighbor_table_consensus)
+      neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
     if (GNUNET_YES !=
        GNUNET_CONTAINER_multipeermap_put (neighbor->neighbor_table_consensus,
                                           &target->peer,
@@ -1383,34 +1445,38 @@ handle_set_union_result (void *cls,
                                                              neighbor);
     break;
   case GNUNET_SET_STATUS_HALF_DONE:
+    break;
+  case GNUNET_SET_STATUS_DONE:
     /* we got all of our updates; integrate routing table! */
     neighbor->target_removed = GNUNET_NO;
-    GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
-                                          &check_target_removed,
-                                          neighbor);
+    if (NULL == neighbor->neighbor_table_consensus)
+      neighbor->neighbor_table_consensus = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
+    if (NULL != neighbor->neighbor_table)
+      GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
+                                           &check_target_removed,
+                                           neighbor);
     if (GNUNET_YES == neighbor->target_removed)
     {
       /* check if we got an alternative for the removed routes */
       GNUNET_CONTAINER_multipeermap_iterate (direct_neighbors,
-                                            &refresh_routes,
-                                            NULL);
+                                             &refresh_routes,
+                                             NULL);
     }
     /* add targets that appeared (and check for improved routes) */
     GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table_consensus,
-                                          &check_target_added,
-                                          neighbor);
+                                           &check_target_added,
+                                           neighbor);
     if (NULL != neighbor->neighbor_table)
     {
       GNUNET_CONTAINER_multipeermap_iterate (neighbor->neighbor_table,
-                                            &free_targets,
-                                            NULL);
+                                             &free_targets,
+                                             NULL);
       GNUNET_CONTAINER_multipeermap_destroy (neighbor->neighbor_table);
       neighbor->neighbor_table = NULL;
     }
     neighbor->neighbor_table = neighbor->neighbor_table_consensus;
     neighbor->neighbor_table_consensus = NULL;
-    break;
-  case GNUNET_SET_STATUS_DONE:
+
     /* operation done, schedule next run! */
     neighbor->set_op = NULL;
     if (0 < memcmp (&neighbor->peer,
@@ -1454,7 +1520,7 @@ listen_set_union (void *cls,
   if (NULL == request)
     return; /* why??? */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Starting to create consensus with %s!\n",
+             "Starting to create consensus with %s\n",
              GNUNET_i2s (&neighbor->peer));
   if (NULL != neighbor->set_op)
   {
@@ -1493,7 +1559,8 @@ initiate_set_union (void *cls,
   struct DirectNeighbor *neighbor = cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Initiating SET union\n");
+             "Initiating SET union with peer `%s'\n",
+             GNUNET_i2s (&neighbor->peer));
   neighbor->initiate_task = GNUNET_SCHEDULER_NO_TASK;
   neighbor->my_set = GNUNET_SET_create (cfg,
                                        GNUNET_SET_OPERATION_UNION);
@@ -1709,6 +1776,7 @@ handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
   neighbor->connected = GNUNET_NO;
   if (DIRECT_NEIGHBOR_COST == neighbor->distance)
   {
+
     GNUNET_STATISTICS_update (stats,
                              "# peers connected (1-hop)",
                              -1, GNUNET_NO);