示例#1
0
        /// <summary>
        /// Returns the number of seconds until request is expected to be satisfied.
        /// Calculates the ETA by looking at the average satisfied time, the current running time of
        /// requests and the position of this request.
        /// </summary>
        /// <param name="requestHandler">The request for which we want the ETA.</param>
        /// <returns>ETA in seconds.</returns>
        public int ETA(LocalRequestHandler requestHandler)
        {
            double avg = _averageTimePerRequest.TotalSeconds;
            int pos = _globalRequests.IndexOf(requestHandler);

            if (requestHandler.RequestStatus == RequestHandler.Status.Downloading)
            {
                // Calculate remaining time
                return (int)(avg - Math.Min(requestHandler.TimeRunning().TotalSeconds, avg));
            }
            else if (requestHandler.RequestStatus == RequestHandler.Status.Pending)
            {
                // Determine request that is running longest.
                TimeSpan max = TimeSpan.Zero;
                for (int i = 0; i < _maxInflightRequests && i < _globalRequests.Count; i++)
                {
                    TimeSpan ts;
                    if ((ts = _globalRequests[i].TimeRunning()) > max)
                    {
                        max = ts;
                    }
                }
                // Calculate remaining time
                return (int)((pos + 1 - _maxInflightRequests) * avg - Math.Min(max.TotalSeconds, avg));
            }
            else // finished or error
            {
                return 0;
            }
        }