-only trigger check config if we actually need it
[oweals/gnunet.git] / src / fs / gnunet-service-fs.c
index 4a70b518d65d0f448f717437e93e1ab9c386ff97..348bab09276536aed5b418a1cf9f415b69e6aa61 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009-2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2014 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -167,7 +167,7 @@ static struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
 /**
  * ID of our task that we use to age the cover counters.
  */
-static GNUNET_SCHEDULER_TaskIdentifier cover_age_task;
+static struct GNUNET_SCHEDULER_Task * cover_age_task;
 
 /**
  * Datastore 'GET' load tracking.
@@ -184,16 +184,15 @@ static struct GNUNET_PeerIdentity my_id;
  * Task that periodically ages our cover traffic statistics.
  *
  * @param cls unused closure
- * @param tc task context
  */
 static void
-age_cover_counters (void *cls,
-                    const struct GNUNET_SCHEDULER_TaskContext *tc)
+age_cover_counters (void *cls)
 {
   GSF_cover_content_count = (GSF_cover_content_count * 15) / 16;
   GSF_cover_query_count = (GSF_cover_query_count * 15) / 16;
   cover_age_task =
-      GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
+      GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY,
+                                   &age_cover_counters,
                                     NULL);
 }
 
@@ -241,14 +240,13 @@ GSF_test_get_load_too_high_ (uint32_t priority)
 /**
  * We've received peer performance information. Update
  * our running average for the P2P latency.
-*
+ *
  * @param cls closure
  * @param address the address
  * @param active is this address in active use
  * @param bandwidth_out assigned outbound bandwidth for the connection
  * @param bandwidth_in assigned inbound bandwidth for the connection
- * @param ats performance data for the address (as far as known)
- * @param ats_count number of performance records in @a ats
+ * @param prop performance data for the address (as far as known)
  */
 static void
 update_latencies (void *cls,
@@ -256,12 +254,8 @@ update_latencies (void *cls,
                  int active,
                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
-                 const struct GNUNET_ATS_Information *ats,
-                 uint32_t ats_count)
+                 const struct GNUNET_ATS_Properties *prop)
 {
-  unsigned int i;
-  struct GNUNET_TIME_Relative latency;
-
   if (NULL == address)
   {
     /* ATS service temporarily disconnected */
@@ -270,22 +264,15 @@ update_latencies (void *cls,
 
   if (GNUNET_YES != active)
     return;
-  for (i = 0; i < ats_count; i++)
-  {
-    if (GNUNET_ATS_QUALITY_NET_DELAY != ntohl (ats[i].type))
-      continue;
-    latency.rel_value_us = ntohl (ats[i].value);
-    GSF_update_peer_latency_ (&address->peer,
-                             latency);
-    GSF_avg_latency.rel_value_us =
-      (GSF_avg_latency.rel_value_us * 31 +
-       GNUNET_MIN (5000, ntohl (ats[i].value))) / 32;
-    GNUNET_STATISTICS_set (GSF_stats,
-                          gettext_noop
-                          ("# running average P2P latency (ms)"),
-                          GSF_avg_latency.rel_value_us / 1000LL, GNUNET_NO);
-    break;
-  }
+  GSF_update_peer_latency_ (&address->peer,
+                            prop->delay);
+  GSF_avg_latency.rel_value_us =
+    (GSF_avg_latency.rel_value_us * 31 +
+     GNUNET_MIN (5000, prop->delay.rel_value_us)) / 32;
+  GNUNET_STATISTICS_set (GSF_stats,
+                         gettext_noop ("# running average P2P latency (ms)"),
+                         GSF_avg_latency.rel_value_us / 1000LL,
+                         GNUNET_NO);
 }
 
 
@@ -367,7 +354,11 @@ consider_forwarding (void *cls,
 {
   if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
     return;                     /* we're done... */
-  GSF_iterate_connected_peers_ (&consider_request_for_forwarding, pr);
+  if (GNUNET_YES !=
+      GSF_pending_request_test_active_ (pr))
+    return; /* request is not actually active, skip! */
+  GSF_iterate_connected_peers_ (&consider_request_for_forwarding,
+                                pr);
 }
 
 
@@ -388,12 +379,14 @@ handle_p2p_get (void *cls,
 {
   struct GSF_PendingRequest *pr;
 
-  pr = GSF_handle_p2p_query_ (other, message);
+  pr = GSF_handle_p2p_query_ (other,
+                              message);
   if (NULL == pr)
-    return GNUNET_SYSERR;
+    return GNUNET_OK; /* exists, identical to existing request, or malformed */
   GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
   GSF_local_lookup_ (pr,
-                     &consider_forwarding, NULL);
+                     &consider_forwarding,
+                     NULL);
   return GNUNET_OK;
 }
 
@@ -451,7 +444,8 @@ start_p2p_processing (void *cls,
 
 
 /**
- * Handle START_SEARCH-message (search request from client).
+ * Handle #GNUNET_MESSAGE_TYPE_FS_START_SEARCH-message (search request
+ * from client).
  *
  * @param cls closure
  * @param client identification of the client
@@ -536,11 +530,9 @@ handle_loc_sign (void *cls,
  * Task run during shutdown.
  *
  * @param cls unused
- * @param tc unused
  */
 static void
-shutdown_task (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
   GSF_cadet_stop_client ();
   GSF_cadet_stop_server ();
@@ -569,10 +561,10 @@ shutdown_task (void *cls,
   block_cfg = NULL;
   GNUNET_STATISTICS_destroy (GSF_stats, GNUNET_NO);
   GSF_stats = NULL;
-  if (GNUNET_SCHEDULER_NO_TASK != cover_age_task)
+  if (NULL != cover_age_task)
   {
     GNUNET_SCHEDULER_cancel (cover_age_task);
-    cover_age_task = GNUNET_SCHEDULER_NO_TASK;
+    cover_age_task = NULL;
   }
   GNUNET_FS_indexing_done ();
   GNUNET_LOAD_value_free (datastore_get_load);
@@ -587,7 +579,7 @@ shutdown_task (void *cls,
  * peer connects, giving us a chance to decide about submitting
  * the existing request to the new peer.
  *
- * @param cls the 'struct GSF_ConnectedPeer' of the new peer
+ * @param cls the `struct GSF_ConnectedPeer` of the new peer
  * @param key query for the request
  * @param pr handle to the pending request
  * @return #GNUNET_YES to continue to iterate
@@ -600,11 +592,16 @@ consider_peer_for_forwarding (void *cls,
   struct GSF_ConnectedPeer *cp = cls;
   struct GNUNET_PeerIdentity pid;
 
+  if (GNUNET_YES !=
+      GSF_pending_request_test_active_ (pr))
+    return GNUNET_YES; /* request is not actually active, skip! */
   GSF_connected_peer_get_identity_ (cp, &pid);
-  if (GNUNET_YES != GSF_pending_request_test_target_ (pr, &pid))
+  if (GNUNET_YES !=
+      GSF_pending_request_test_target_ (pr, &pid))
   {
     GNUNET_STATISTICS_update (GSF_stats,
-                              gettext_noop ("# Loopback routes suppressed"), 1,
+                              gettext_noop ("# Loopback routes suppressed"),
+                              1,
                               GNUNET_NO);
     return GNUNET_YES;
   }
@@ -620,11 +617,13 @@ consider_peer_for_forwarding (void *cls,
  * @param cp handle to the newly created connected peer record
  */
 static void
-connected_peer_cb (void *cls, struct GSF_ConnectedPeer *cp)
+connected_peer_cb (void *cls,
+                   struct GSF_ConnectedPeer *cp)
 {
   if (NULL == cp)
     return;
-  GSF_iterate_pending_requests_ (&consider_peer_for_forwarding, cp);
+  GSF_iterate_pending_requests_ (&consider_peer_for_forwarding,
+                                 cp);
 }
 
 
@@ -638,8 +637,9 @@ static void
 peer_connect_handler (void *cls,
                       const struct GNUNET_PeerIdentity *peer)
 {
-  if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_id,
-                                            peer))
+  if (0 ==
+      GNUNET_CRYPTO_cmp_peer_identity (&my_id,
+                                       peer))
     return;
   GSF_peer_connect_handler_ (peer,
                              &connected_peer_cb,
@@ -737,6 +737,9 @@ main_init (struct GNUNET_SERVER_Handle *server,
   GNUNET_CRYPTO_eddsa_key_get_public (pk,
                                       &my_id.public_key);
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "I am peer %s\n",
+              GNUNET_i2s (&my_id));
   GSF_core
     = GNUNET_CORE_connect (GSF_cfg, NULL,
                            &peer_init_handler,
@@ -757,13 +760,14 @@ main_init (struct GNUNET_SERVER_Handle *server,
                                    NULL);
   GNUNET_SERVER_add_handlers (server, handlers);
   cover_age_task =
-      GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY, &age_cover_counters,
+      GNUNET_SCHEDULER_add_delayed (COVER_AGE_FREQUENCY,
+                                   &age_cover_counters,
                                     NULL);
   datastore_get_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
   GSF_cadet_start_server ();
   GSF_cadet_start_client ();
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+                                NULL);
   return GNUNET_OK;
 }
 
@@ -815,7 +819,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
       (GNUNET_OK != main_init (server, cfg)))
   {
     GNUNET_SCHEDULER_shutdown ();
-    shutdown_task (NULL, NULL);
+    shutdown_task (NULL);
     return;
   }
 }