+ /* The progress bar is supposed to display the "current download
+ speed". The first version of the progress bar calculated it by
+ dividing the total amount of data with the total time needed to
+ download it. The problem with this was that stalled or suspended
+ download could unduly influence the "current" time. Taking just
+ the time needed to download the current packet would not work
+ either because packets arrive too fast and the varitions would be
+ too jerky.
+
+ It would be preferrable to show the speed that pertains to a
+ recent period, say over the past several seconds. But to do this
+ accurately, we would have to record all the packets received
+ during the last five seconds.
+
+ What we do instead is maintain a history of a fixed number of
+ packets. It actually makes sense if you think about it -- faster
+ downloads will have a faster response to speed changes. */
+
+ rec_index = bp->recent.count % RECENT_ARRAY_SIZE;
+ ++bp->recent.count;
+
+ /* Instead of calculating the sum of times[] and bytes[], we
+ maintain the summed quantities. To maintain each sum, we must
+ make sure that it gets increased by the newly downloaded amount,
+ but also that it gets decreased by the amount we're overwriting
+ in (erasing from) the cyclical buffer. */
+ bp->recent.summed_times -= bp->recent.times[rec_index];
+ bp->recent.summed_bytes -= bp->recent.bytes[rec_index];
+
+ bp->recent.times[rec_index] = dltime - bp->recent.previous_time;
+ bp->recent.bytes[rec_index] = howmuch;
+
+ bp->recent.summed_times += bp->recent.times[rec_index];
+ bp->recent.summed_bytes += bp->recent.bytes[rec_index];
+
+ bp->recent.previous_time = dltime;
+
+#if 0
+ /* Sledgehammer check that summed_times and summed_bytes are
+ accurate. */
+ {
+ int num = bp->recent.count;
+ int i;
+ int upper = num < RECENT_ARRAY_SIZE ? num : RECENT_ARRAY_SIZE;
+ long sumt = 0, sumb = 0;
+ for (i = 0; i < upper; i++)
+ {
+ sumt += bp->recent.times[i];
+ sumb += bp->recent.bytes[i];
+ }
+ assert (sumt == bp->recent.summed_times);
+ assert (sumb == bp->recent.summed_bytes);
+ }
+#endif
+