-fix #2598
[oweals/gnunet.git] / src / fs / gnunet-service-fs.c
index 40499313079b4d77b8baaf2004d3aec468e0c28e..a129288abcaf8acf2aa8bd519e3ccf6850ef8432 100644 (file)
@@ -25,8 +25,6 @@
  *
  * To use:
  * - consider re-issue GSF_dht_lookup_ after non-DHT reply received
- * - implement 'SUPPORT_DELAYS'
- *
  */
 #include "platform.h"
 #include <float.h>
  */
 #define COVER_AGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
 
+/**
+ * Collect an instane number of statistics?  May cause excessive IPC.
+ */
+#define INSANE_STATISTICS GNUNET_NO
+
 
 /* ****************************** globals ****************************** */
 
@@ -97,7 +100,7 @@ struct GNUNET_LOAD_Value *GSF_rt_entry_lifetime;
  * Running average of the observed latency to other peers (round trip).
  * Initialized to 5s as the initial default.
  */
-struct GNUNET_TIME_Relative GSF_avg_latency = { 5000 };
+struct GNUNET_TIME_Relative GSF_avg_latency = { 500 };
 
 /**
  * Typical priorities we're seeing from other peers right now.  Since
@@ -226,19 +229,21 @@ GSF_test_get_load_too_high_ (uint32_t priority)
  */
 static void
 update_latencies (const struct GNUNET_ATS_Information *atsi,
-                 unsigned int atsi_count)
+                  unsigned int atsi_count)
 {
   unsigned int i;
 
-  for (i=0;i<atsi_count;i++)
+  for (i = 0; i < atsi_count; i++)
   {
-    if (ntohl(atsi[i].type) == GNUNET_ATS_QUALITY_NET_DELAY)
+    if (ntohl (atsi[i].type) == GNUNET_ATS_QUALITY_NET_DELAY)
     {
-      GSF_avg_latency.rel_value = (GSF_avg_latency.rel_value * 31 + ntohl (atsi[i].value)) / 32;
+      GSF_avg_latency.rel_value =
+          (GSF_avg_latency.rel_value * 31 +
+           GNUNET_MIN (5000, ntohl (atsi[i].value))) / 32;
       GNUNET_STATISTICS_set (GSF_stats,
-                            gettext_noop ("# running average P2P latency (ms)"),
-                            GSF_avg_latency.rel_value,
-                            GNUNET_NO);
+                             gettext_noop
+                             ("# running average P2P latency (ms)"),
+                             GSF_avg_latency.rel_value, GNUNET_NO);
       break;
     }
   }
@@ -261,7 +266,7 @@ static int
 handle_p2p_put (void *cls, const struct GNUNET_PeerIdentity *other,
                 const struct GNUNET_MessageHeader *message,
                 const struct GNUNET_ATS_Information *atsi,
-               unsigned int atsi_count)
+                unsigned int atsi_count)
 {
   struct GSF_ConnectedPeer *cp;
 
@@ -294,13 +299,13 @@ consider_request_for_forwarding (void *cls,
 {
   struct GSF_PendingRequest *pr = cls;
 
-  if (GNUNET_YES !=
-      GSF_pending_request_test_target_ (pr,
-                                       peer))
+  if (GNUNET_YES != GSF_pending_request_test_target_ (pr, peer))
   {
+#if INSANE_STATISTICS
     GNUNET_STATISTICS_update (GSF_stats,
-                             gettext_noop ("# Loopback routes suppressed"), 1,
-                             GNUNET_NO);
+                              gettext_noop ("# Loopback routes suppressed"), 1,
+                              GNUNET_NO);
+#endif
     return;
   }
   GSF_plan_add_ (cp, pr);
@@ -343,13 +348,14 @@ static int
 handle_p2p_get (void *cls, const struct GNUNET_PeerIdentity *other,
                 const struct GNUNET_MessageHeader *message,
                 const struct GNUNET_ATS_Information *atsi,
-               unsigned int atsi_count)
+                unsigned int atsi_count)
 {
   struct GSF_PendingRequest *pr;
 
   pr = GSF_handle_p2p_query_ (other, message);
   if (NULL == pr)
     return GNUNET_SYSERR;
+  GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
   GSF_local_lookup_ (pr, &consider_forwarding, NULL);
   update_latencies (atsi, atsi_count);
   return GNUNET_OK;
@@ -374,11 +380,9 @@ start_p2p_processing (void *cls, struct GSF_PendingRequest *pr,
   struct GSF_PendingRequestData *prd;
 
   prd = GSF_pending_request_get_data_ (pr);
-#if DEBUG_FS_CLIENT
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Finished database lookup for local request `%s' with result %d\n",
               GNUNET_h2s (&prd->query), result);
-#endif
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
   if (GNUNET_BLOCK_EVALUATION_OK_LAST == result)
     return;                     /* we're done, 'pr' was already destroyed... */
@@ -404,14 +408,25 @@ handle_start_search (void *cls, struct GNUNET_SERVER_Client *client,
                      const struct GNUNET_MessageHeader *message)
 {
   struct GSF_PendingRequest *pr;
+  int ret;
 
-  pr = GSF_local_client_start_search_handler_ (client, message);
-  if (NULL == pr)
+  pr = NULL;
+  ret = GSF_local_client_start_search_handler_ (client, message, &pr);
+  switch (ret)
   {
-    /* GNUNET_SERVER_receive_done was already called! */
-    return;
+  case GNUNET_SYSERR:
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    break;
+  case GNUNET_NO:
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    break;
+  case GNUNET_YES:
+    GSF_pending_request_get_data_ (pr)->has_started = GNUNET_YES;
+    GSF_local_lookup_ (pr, &start_p2p_processing, client);
+    break;
+  default:
+    GNUNET_assert (0);
   }
-  GSF_local_lookup_ (pr, &start_p2p_processing, client);
 }
 
 
@@ -468,20 +483,18 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @return GNUNET_YES to continue to iterate
  */
 static int
-consider_peer_for_forwarding (void *cls, const GNUNET_HashCode * key,
+consider_peer_for_forwarding (void *cls, const struct GNUNET_HashCode * key,
                               struct GSF_PendingRequest *pr)
 {
   struct GSF_ConnectedPeer *cp = cls;
   struct GNUNET_PeerIdentity pid;
 
   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,
-                             GNUNET_NO);
+                              gettext_noop ("# Loopback routes suppressed"), 1,
+                              GNUNET_NO);
     return GNUNET_YES;
   }
   GSF_plan_add_ (cp, pr);
@@ -500,7 +513,7 @@ consider_peer_for_forwarding (void *cls, const GNUNET_HashCode * key,
 static void
 peer_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
                       const struct GNUNET_ATS_Information *atsi,
-                     unsigned int atsi_count)
+                      unsigned int atsi_count)
 {
   struct GSF_ConnectedPeer *cp;
 
@@ -565,12 +578,10 @@ main_init (struct GNUNET_SERVER_Handle *server,
     {NULL, NULL, 0, 0}
   };
 
-  GSF_core = GNUNET_CORE_connect (GSF_cfg, 2,   /* larger? */
-                                  NULL, &peer_init_handler,
-                                  &peer_connect_handler,
-                                  &GSF_peer_disconnect_handler_,
-                                  NULL, GNUNET_NO,
-                                  NULL, GNUNET_NO, p2p_handlers);
+  GSF_core =
+      GNUNET_CORE_connect (GSF_cfg, NULL, &peer_init_handler,
+                           &peer_connect_handler, &GSF_peer_disconnect_handler_,
+                           NULL, GNUNET_NO, NULL, GNUNET_NO, p2p_handlers);
   if (NULL == GSF_core)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,