LRN: fix gnuent_fs_Start arguments
[oweals/gnunet.git] / src / fs / gnunet-service-fs.c
index bf6984dd672930f188d1a4b18e6d5b0563016f88..f588ee9c47d48081a0c7b4d0c15f233360a9a878 100644 (file)
@@ -93,6 +93,12 @@ struct GNUNET_DHT_Handle *GSF_dht;
  */
 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 };
+
 /**
  * Typical priorities we're seeing from other peers right now.  Since
  * most priorities will be zero, this value is the weighted average of
@@ -172,7 +178,6 @@ age_cover_counters (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
-
 /**
  * We've just now completed a datastore request.  Update our
  * datastore load calculations.
@@ -212,6 +217,35 @@ GSF_test_get_load_too_high_ (uint32_t priority)
 }
 
 
+/**
+ * We've received peer performance information. Update
+ * our running average for the P2P latency.
+ *
+ * @param atsi performance information
+ * @param atsi_count number of 'atsi' records
+ */
+static void
+update_latencies (const struct GNUNET_ATS_Information *atsi,
+                  unsigned int atsi_count)
+{
+  unsigned int i;
+
+  for (i = 0; i < atsi_count; i++)
+  {
+    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;
+      GNUNET_STATISTICS_set (GSF_stats,
+                             gettext_noop
+                             ("# running average P2P latency (ms)"),
+                             GSF_avg_latency.rel_value, GNUNET_NO);
+      break;
+    }
+  }
+}
+
+
 /**
  * Handle P2P "PUT" message.
  *
@@ -220,13 +254,15 @@ GSF_test_get_load_too_high_ (uint32_t priority)
  *        for loopback messages where we are both sender and receiver)
  * @param message the actual message
  * @param atsi performance information
+ * @param atsi_count number of records in 'atsi'
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_p2p_put (void *cls, const struct GNUNET_PeerIdentity *other,
                 const struct GNUNET_MessageHeader *message,
-                const struct GNUNET_ATS_Information *atsi)
+                const struct GNUNET_ATS_Information *atsi,
+                unsigned int atsi_count)
 {
   struct GSF_ConnectedPeer *cp;
 
@@ -237,6 +273,7 @@ handle_p2p_put (void *cls, const struct GNUNET_PeerIdentity *other,
     return GNUNET_OK;
   }
   GSF_cover_content_count++;
+  update_latencies (atsi, atsi_count);
   return GSF_handle_p2p_content_ (cp, message);
 }
 
@@ -258,6 +295,13 @@ consider_request_for_forwarding (void *cls,
 {
   struct GSF_PendingRequest *pr = cls;
 
+  if (GNUNET_YES != GSF_pending_request_test_target_ (pr, peer))
+  {
+    GNUNET_STATISTICS_update (GSF_stats,
+                              gettext_noop ("# Loopback routes suppressed"), 1,
+                              GNUNET_NO);
+    return;
+  }
   GSF_plan_add_ (cp, pr);
 }
 
@@ -290,13 +334,15 @@ consider_forwarding (void *cls, struct GSF_PendingRequest *pr,
  *        for loopback messages where we are both sender and receiver)
  * @param message the actual message
  * @param atsi performance information
+ * @param atsi_count number of records in 'atsi'
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_p2p_get (void *cls, const struct GNUNET_PeerIdentity *other,
                 const struct GNUNET_MessageHeader *message,
-                const struct GNUNET_ATS_Information *atsi)
+                const struct GNUNET_ATS_Information *atsi,
+                unsigned int atsi_count)
 {
   struct GSF_PendingRequest *pr;
 
@@ -304,6 +350,7 @@ handle_p2p_get (void *cls, const struct GNUNET_PeerIdentity *other,
   if (NULL == pr)
     return GNUNET_SYSERR;
   GSF_local_lookup_ (pr, &consider_forwarding, NULL);
+  update_latencies (atsi, atsi_count);
   return GNUNET_OK;
 }
 
@@ -427,10 +474,13 @@ consider_peer_for_forwarding (void *cls, const GNUNET_HashCode * key,
   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);
     return GNUNET_YES;
+  }
   GSF_plan_add_ (cp, pr);
   return GNUNET_YES;
 }
@@ -442,16 +492,18 @@ consider_peer_for_forwarding (void *cls, const GNUNET_HashCode * key,
  * @param cls closure, not used
  * @param peer peer identity this notification is about
  * @param atsi performance information
+ * @param atsi_count number of records in 'atsi'
  */
 static void
 peer_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
-                      const struct GNUNET_ATS_Information *atsi)
+                      const struct GNUNET_ATS_Information *atsi,
+                      unsigned int atsi_count)
 {
   struct GSF_ConnectedPeer *cp;
 
   if (0 == memcmp (&my_id, peer, sizeof (struct GNUNET_PeerIdentity)))
     return;
-  cp = GSF_peer_connect_handler_ (peer, atsi);
+  cp = GSF_peer_connect_handler_ (peer, atsi, atsi_count);
   if (NULL == cp)
     return;
   GSF_iterate_pending_requests_ (&consider_peer_for_forwarding, cp);
@@ -513,9 +565,8 @@ main_init (struct GNUNET_SERVER_Handle *server,
   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_peer_disconnect_handler_, NULL,
+                                  GNUNET_NO, NULL, GNUNET_NO, p2p_handlers);
   if (NULL == GSF_core)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,