clean up, asserts, FIXME
[oweals/gnunet.git] / src / transport / gnunet-service-transport_neighbours.c
index 332260e409772376181530b477cb6f6c871c2009..62fc441a15f20d274f31322b88a68a4effc7f477 100644 (file)
@@ -819,10 +819,12 @@ static void
 disconnect_neighbour (struct NeighbourMapEntry *n)
 {
   struct MessageQueue *mq;
-  int was_connected = is_connected (n);
+  int is_connected;
+
+  is_connected = (n->state == S_CONNECTED);
 
   /* send DISCONNECT MESSAGE */
-  if (is_connected (n) || is_connecting (n))
+  if (is_connected || is_connecting (n))
   {
     if (GNUNET_OK ==
         send_disconnect (&n->id, n->address,
@@ -835,7 +837,7 @@ disconnect_neighbour (struct NeighbourMapEntry *n)
                   GNUNET_i2s (&n->id));
   }
 
-  if (is_connected(n))
+  if (is_connected)
   {
      GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
   }
@@ -869,7 +871,7 @@ disconnect_neighbour (struct NeighbourMapEntry *n)
     n->is_active->n = NULL;
     n->is_active = NULL;
   }
-  if (was_connected)
+  if (is_connected)
   {
     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
     GNUNET_SCHEDULER_cancel (n->keepalive_task);
@@ -948,7 +950,7 @@ neighbour_keepalive_task (void *cls,
   n->keepalive_task =
       GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
                                     &neighbour_keepalive_task, n);
-  GNUNET_assert (is_connected (n));
+  GNUNET_assert (S_CONNECTED == n->state);
   GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
                             GNUNET_NO);
   m.size = htons (sizeof (struct GNUNET_MessageHeader));
@@ -977,7 +979,7 @@ disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
               GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
 #endif
-  if (is_connected (n))
+  if (S_CONNECTED == n->state)
     GNUNET_STATISTICS_update (GST_stats,
                               gettext_noop
                               ("# peers disconnected due to global disconnect"),
@@ -1207,7 +1209,7 @@ GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "ATS tells us to switch to address '%s' session %p for %s peer `%s'\n",
               GST_plugins_a2s (address),
-              session, (is_connected (n) ? "CONNECTED" : "NOT CONNECTED"),
+              session, (S_CONNECTED == n->state) ? "CONNECTED" : "NOT CONNECTED",
               GNUNET_i2s (peer));
 #endif
   if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
@@ -1245,6 +1247,7 @@ GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
   if (n->state == S_CONNECTED) 
   {
     /* mark old address as no longer used */
+    GNUNET_assert (NULL != n->address);
     GST_validation_set_address_use (&n->id,
                                    n->address,
                                    n->session,
@@ -1415,7 +1418,7 @@ GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
 
   if (NULL != n)
   {
-    if ((is_connected (n)) || (is_connecting (n)))
+    if ((S_CONNECTED == n->state) || (is_connecting (n)))
       return;                   /* already connecting or connected */
     if (is_disconnecting (n))
       change_state (n, S_NOT_CONNECTED);
@@ -1452,7 +1455,7 @@ GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
 
   n = lookup_neighbour (target);
 
-  if ((NULL == n) || (!is_connected (n)))
+  if ((NULL == n) || (S_CONNECTED != n->state))
     return GNUNET_NO;           /* not connected */
   return GNUNET_YES;
 }
@@ -1470,9 +1473,9 @@ GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
 {
   struct NeighbourMapEntry *n;
 
-  // This can happen during shutdown
   if (neighbours == NULL)
   {
+    /* This can happen during shutdown */
     return;
   }
 
@@ -1486,17 +1489,22 @@ GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
     return;
   if (session != n->session)
     return;                     /* doesn't affect us */
-
+  if (n->state == S_CONNECTED)
+    GST_validation_set_address_use (&n->id,
+                                   n->address,
+                                   n->session,
+                                   GNUNET_NO);
   n->session = NULL;
   if (NULL != n->address)
   {
     GNUNET_HELLO_address_free (n->address);
     n->address = NULL;
   }
-
+  
   /* not connected anymore anyway, shouldn't matter */
-  if ((!is_connected (n)) && (!is_connecting (n)))
+  if ((S_CONNECTED != n->state) && (!is_connecting (n)))
     return;
+  // FIXME: n->state = S_FAST_RECONNECT;
 
   /* We are connected, so ask ATS to switch addresses */
   GNUNET_SCHEDULER_cancel (n->timeout_task);
@@ -1507,7 +1515,7 @@ GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
   if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (n->ats_suggest);
   n->ats_suggest =
-      GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
+      GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
                                     n);
   GNUNET_ATS_suggest_address (GST_ats, peer);
 }
@@ -2053,6 +2061,7 @@ GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
 
 }
 
+
 void
 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
                            const struct GNUNET_PeerIdentity *peer,
@@ -2063,10 +2072,10 @@ GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
 {
   struct NeighbourMapEntry *n;
   struct QuotaSetMessage q_msg;
-  int was_connected;
 
 #if DEBUG_TRANSPORT
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+             "Received ACK message from peer `%s'\n",
               GNUNET_i2s (peer));
 #endif
 
@@ -2075,7 +2084,6 @@ GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
     GNUNET_break_op (0);
     return;
   }
-
   n = lookup_neighbour (peer);
   if (NULL == n)
   {
@@ -2084,27 +2092,22 @@ GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
     GNUNET_break (0);
     return;
   }
-
-  if (is_connected (n))
+  if (S_CONNECTED == n->state)
     return;
-
   if (!is_connecting(n))
   {
     GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# unexpected ACK messages"), 1,
                               GNUNET_NO);
     return;
   }
-
   if (NULL != session)
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
                      "transport-ats",
                      "Giving ATS session %p of plugin %s for peer %s\n",
                      session, address->transport_name, GNUNET_i2s (peer));
   GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
-
-  was_connected = is_connected (n);
+  GNUNET_assert (n->address != NULL);
   change_state (n, S_CONNECTED);
-
   GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
 
   GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
@@ -2113,26 +2116,22 @@ GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
     n->keepalive_task =
         GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
                                       &neighbour_keepalive_task, n);
-
-  if (!was_connected)
-  {
-    GST_validation_set_address_use (&n->id,
-                                   n->address,
-                                   n->session,
-                                   GNUNET_YES);
-    neighbours_connected++;
-    GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
-                              GNUNET_NO);
-
+  GST_validation_set_address_use (&n->id,
+                                 n->address,
+                                 n->session,
+                                 GNUNET_YES);
+  neighbours_connected++;
+  GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
+                           GNUNET_NO);
+  
 #if DEBUG_TRANSPORT
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
-                GNUNET_i2s (&n->id),
-                GST_plugins_a2s (n->address), n->session,
-                __LINE__);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
+             GNUNET_i2s (&n->id),
+             GST_plugins_a2s (n->address), n->session,
+             __LINE__);
 #endif
-    connect_notify_cb (callback_cls, &n->id, ats, ats_count);
-  }
+  connect_notify_cb (callback_cls, &n->id, ats, ats_count);  
 #if DEBUG_TRANSPORT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
@@ -2253,14 +2252,12 @@ GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
   GNUNET_break_op (ntohl (scm->reserved) == 0);
 
   n = lookup_neighbour (peer);
-  if (n != NULL)
+  if ( (n != NULL) &&
+       (S_CONNECTED == n->state) )
   {
     /* connected peer switches addresses */
-    if (is_connected (n))
-    {
-      GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
-      return;
-    }
+    GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
+    return;
   }
 
   /* we are not connected to this peer */