Provide an estimate of the download time remaining when downloading

bundles.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1763 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2002-09-30 09:36:22 +00:00
parent 5af9b4ac19
commit d08589d88a
2 changed files with 38 additions and 13 deletions
@@ -1,5 +1,5 @@
//
// $Id: DownloadManager.java,v 1.3 2002/07/22 20:45:22 shaper Exp $
// $Id: DownloadManager.java,v 1.4 2002/09/30 09:36:22 shaper Exp $
package com.threerings.resource;
@@ -44,8 +44,11 @@ public class DownloadManager
*
* @param percent the percent completion, in terms of total file
* size, of the download request.
* @param remaining the estimated download time remaining in
* seconds, or <code>-1</code> if the time can not yet be
* determined.
*/
public void downloadProgress (int percent);
public void downloadProgress (int percent, long remaining);
/**
* Called if a failure occurs while checking for an update or
@@ -243,12 +246,16 @@ public class DownloadManager
// download all stale files
size = fetch.size();
long currentSize = 0;
long start = System.currentTimeMillis();
long bytesPerSecond = 0;
boolean complete = false;
for (int ii = 0; ii < size; ii++) {
DownloadDescriptor desc = (DownloadDescriptor)fetch.get(ii);
try {
complete = processDownload(desc, obs, currentSize, totalSize);
complete = processDownload(
desc, obs, currentSize, totalSize, start, bytesPerSecond);
currentSize += desc.fileSize;
bytesPerSecond = calculateXferRate(currentSize, start);
} catch (IOException ioe) {
obs.downloadFailed(desc, ioe);
@@ -261,7 +268,7 @@ public class DownloadManager
// make sure to always let the observer know that we've wrapped up
// by reporting 100% completion
if (!complete) {
obs.downloadProgress(100);
obs.downloadProgress(100, 0L);
}
}
@@ -271,7 +278,7 @@ public class DownloadManager
*/
protected boolean processDownload (
DownloadDescriptor desc, DownloadObserver obs, long currentSize,
long totalSize)
long totalSize, long start, long bytesPerSecond)
throws IOException
{
// download the resource bundle from the specified URL
@@ -288,7 +295,6 @@ public class DownloadManager
while ((read = in.read(_buffer)) != -1) {
// write it out to our local copy
out.write(_buffer, 0, read);
// report our progress to the download observer as a
// percentage of the total file data to be transferred
currentSize += read;
@@ -299,7 +305,10 @@ public class DownloadManager
// file to ensure that any action the observer may take with
// respect to the downloaded files can be safely undertaken
if (!complete) {
obs.downloadProgress(pctdone);
bytesPerSecond = calculateXferRate(currentSize, start);
long remaining = (bytesPerSecond == 0) ? -1 :
(totalSize - currentSize) / bytesPerSecond;
obs.downloadProgress(pctdone, remaining);
}
}
@@ -319,12 +328,23 @@ public class DownloadManager
if (complete) {
// let the observer know we're finished now that we've
// finished all of our work with the file
obs.downloadProgress(100);
obs.downloadProgress(100, 0L);
}
return complete;
}
/**
* Returns the current transfer rate in bytes per second based on
* transferring the specified quantity of data starting at the given
* time.
*/
protected long calculateXferRate (long size, long start)
{
long secs = (System.currentTimeMillis() - start) / 1000L;
return (secs == 0) ? 0 : (size / secs);
}
/**
* A record describing a single download request.
*/
@@ -1,5 +1,5 @@
//
// $Id: ResourceManager.java,v 1.15 2002/07/22 23:19:23 shaper Exp $
// $Id: ResourceManager.java,v 1.16 2002/09/30 09:36:22 shaper Exp $
package com.threerings.resource;
@@ -92,8 +92,13 @@ public class ResourceManager
* completion of the overall bundle downloading task. The caller
* is guaranteed to get at least one call reporting 100%
* completion.
*
* @param percent the percent completion of the download.
* @param remaining the estimated download time remaining in
* seconds, or <code>-1</code> if the time can not yet be
* determined.
*/
public void downloadProgress (int percent);
public void downloadProgress (int percent, long remaining);
/**
* Called if a failure occurs while checking for an update or
@@ -221,7 +226,7 @@ public class ResourceManager
// nothing for now
}
public void downloadProgress (int percent) {
public void downloadProgress (int percent, long remaining) {
if (percent == 100) {
synchronized (lock) {
// wake things up as the download is finished
@@ -268,8 +273,8 @@ public class ResourceManager
obs.checkingForUpdate();
}
public void downloadProgress (int percent) {
obs.downloadProgress(percent);
public void downloadProgress (int percent, long remaining) {
obs.downloadProgress(percent, remaining);
}
public void downloadFailed (DownloadDescriptor desc, Exception e) {