Brunet.Node.ComputeDynamicTimeout C# (CSharp) Method

ComputeDynamicTimeout() protected method

protected ComputeDynamicTimeout ( ) : System.TimeSpan
return System.TimeSpan
    protected TimeSpan ComputeDynamicTimeout() {
      TimeSpan timeout;
      //Compute the mean and stddev of LastInPacketDateTime:
      double sum = 0.0;
      double sum2 = 0.0;
      int count = 0;
      DateTime now = DateTime.UtcNow;
      foreach(Connection con in _connection_table) {
        Edge e = con.State.Edge;
        double this_int = (now - e.LastInPacketDateTime).TotalMilliseconds;
        sum += this_int;
        sum2 += this_int * this_int;
        count++;
      }
      /*
       * Compute the mean and std.dev:
       */
      if( count > 1 ) {
        double mean = sum / count;
        double s2 = sum2 - count * mean * mean;
        double stddev = Math.Sqrt( s2 /(count - 1) );
        double timeout_d = mean + stddev;
        ProtocolLog.WriteIf(ProtocolLog.NodeLog, String.Format(
          "Connection timeout: {0}, mean: {1} stdev: {2}", timeout_d, 
          mean, stddev));
        timeout = TimeSpan.FromMilliseconds( timeout_d );
        if( timeout > MAX_CONNECTION_TIMEOUT ) {
          timeout = MAX_CONNECTION_TIMEOUT;
        }
      }
      else {
        //Keep the old timeout.  Don't let small number statistics bias us
        timeout = _connection_timeout;
      }
      return timeout;
    }
    /**