From 0ad5e615b78002f794092fe9a7698d72461145fa Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 16 Jan 2003 22:50:29 +0000 Subject: [PATCH] Provide means by which resource resolution and download observers can be notified on the AWT thread rather than the download thread. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2167 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/resource/DownloadManager.java | 67 ++++++++++++++++--- .../threerings/resource/ResourceManager.java | 18 ++++- 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/java/com/threerings/resource/DownloadManager.java b/src/java/com/threerings/resource/DownloadManager.java index b09a911b3..8ec3673cc 100644 --- a/src/java/com/threerings/resource/DownloadManager.java +++ b/src/java/com/threerings/resource/DownloadManager.java @@ -1,8 +1,10 @@ // -// $Id: DownloadManager.java,v 1.5 2002/10/08 23:42:21 shaper Exp $ +// $Id: DownloadManager.java,v 1.6 2003/01/16 22:50:29 mdb Exp $ package com.threerings.resource; +import java.awt.EventQueue; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -31,6 +33,14 @@ public class DownloadManager */ public interface DownloadObserver { + /** + * If this method returns true the download observer callbacks + * will be called on the AWT thread, allowing the observer to do + * things like safely update user interfaces, etc. If false, it + * will be called on the download thread. + */ + public boolean notifyOnAWTThread (); + /** * Called when the download manager is about to check all * downloads to see whether they are in need of an update. @@ -173,11 +183,19 @@ public class DownloadManager * Processes a single download request. */ protected void processDownloadRequest ( - List descriptors, DownloadObserver obs, boolean fragile) + List descriptors, final DownloadObserver obs, boolean fragile) { // let the observer know that we're about to resolve all files to // be downloaded - obs.resolvingDownloads(); + if (obs.notifyOnAWTThread()) { + EventQueue.invokeLater(new Runnable() { + public void run () { + obs.resolvingDownloads(); + } + }); + } else { + obs.resolvingDownloads(); + } // check the size and last-modified information for each file to // ascertain whether our local copy needs to be refreshed @@ -235,8 +253,8 @@ public class DownloadManager "[url=" + desc.sourceURL + "]."); } - } catch (IOException ioe) { - obs.downloadFailed(null, ioe); + } catch (final IOException ioe) { + notifyFailed(obs, null, ioe); if (fragile) { return; } @@ -251,7 +269,7 @@ public class DownloadManager try { processDownload(desc, obs, pinfo); } catch (IOException ioe) { - obs.downloadFailed(desc, ioe); + notifyFailed(obs, desc, ioe); if (fragile) { return; } @@ -261,7 +279,38 @@ public class DownloadManager // make sure to always let the observer know that we've wrapped up // by reporting 100% completion if (!pinfo.complete) { - obs.downloadProgress(100, 0L); + notifyProgress(obs, 100, 0L); + } + } + + /** Helper function. */ + protected void notifyProgress (final DownloadObserver obs, + final int progress, final long remaining) + { + if (obs.notifyOnAWTThread()) { + EventQueue.invokeLater(new Runnable() { + public void run () { + obs.downloadProgress(progress, remaining); + } + }); + } else { + obs.downloadProgress(progress, remaining); + } + } + + /** Helper function. */ + protected void notifyFailed (final DownloadObserver obs, + final DownloadDescriptor desc, + final Exception e) + { + if (obs.notifyOnAWTThread()) { + EventQueue.invokeLater(new Runnable() { + public void run () { + obs.downloadFailed(desc, e); + } + }); + } else { + obs.downloadFailed(desc, e); } } @@ -305,7 +354,7 @@ public class DownloadManager if ((now - pinfo.lastUpdate) >= UPDATE_DELAY) { pinfo.lastUpdate = now; long remaining = pinfo.getXferTimeRemaining(); - obs.downloadProgress(pctdone, remaining); + notifyProgress(obs, pctdone, remaining); } } } @@ -326,7 +375,7 @@ public class DownloadManager if (pinfo.complete) { // let the observer know we're finished now that we've // finished all of our work with the file - obs.downloadProgress(100, 0L); + notifyProgress(obs, 100, 0L); } } diff --git a/src/java/com/threerings/resource/ResourceManager.java b/src/java/com/threerings/resource/ResourceManager.java index ef59c3bbd..32a426aed 100644 --- a/src/java/com/threerings/resource/ResourceManager.java +++ b/src/java/com/threerings/resource/ResourceManager.java @@ -1,5 +1,5 @@ // -// $Id: ResourceManager.java,v 1.19 2003/01/13 22:50:36 mdb Exp $ +// $Id: ResourceManager.java,v 1.20 2003/01/16 22:50:29 mdb Exp $ package com.threerings.resource; @@ -86,6 +86,14 @@ public class ResourceManager */ public interface BundleDownloadObserver { + /** + * If this method returns true the download observer callbacks + * will be called on the AWT thread, allowing the observer to do + * things like safely update user interfaces, etc. If false, it + * will be called on a special download thread. + */ + public boolean notifyOnAWTThread (); + /** * Called when the resource manager is about to check for an * update of any of our resource sets. @@ -227,6 +235,10 @@ public class ResourceManager // create the observer that will notify us when all is finished DownloadObserver obs = new DownloadObserver() { + public boolean notifyOnAWTThread () { + return false; + } + public void resolvingDownloads () { // nothing for now } @@ -274,6 +286,10 @@ public class ResourceManager { // pass the descriptors on to the download manager dlmgr.download(dlist, true, new DownloadObserver() { + public boolean notifyOnAWTThread () { + return obs.notifyOnAWTThread(); + } + public void resolvingDownloads () { obs.checkingForUpdate(); }