From ebd2105285bd5fbae0956c10d100a6d48727da64 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 2 Jul 2004 17:03:33 +0000 Subject: [PATCH] Mo' better progress. Code and media resource validation partially in place. Next up the download manager. --- .../threerings/getdown/data/Application.java | 58 +++++++++++++++--- .../com/threerings/getdown/data/Resource.java | 60 +++++++++++++++++-- .../threerings/getdown/launcher/Getdown.java | 28 ++++++++- 3 files changed, 130 insertions(+), 16 deletions(-) diff --git a/src/java/com/threerings/getdown/data/Application.java b/src/java/com/threerings/getdown/data/Application.java index f568345..7a71049 100644 --- a/src/java/com/threerings/getdown/data/Application.java +++ b/src/java/com/threerings/getdown/data/Application.java @@ -1,5 +1,5 @@ // -// $Id: Application.java,v 1.2 2004/07/02 15:22:49 mdb Exp $ +// $Id: Application.java,v 1.3 2004/07/02 17:03:33 mdb Exp $ package com.threerings.getdown.data; @@ -17,6 +17,7 @@ import java.security.MessageDigest; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import com.samskivert.io.NestableIOException; @@ -193,14 +194,6 @@ public class Application _appargs.add(appargs[ii]); } } - -// Log.info("Parsed application " + _appbase); -// Log.info("Version: " + _version); -// Log.info("Class: " + _class); -// Log.info("Code: " + StringUtil.toString(_codes.iterator())); -// Log.info("Resources: " + StringUtil.toString(_resources.iterator())); -// Log.info("JVM Args: " + StringUtil.toString(_jvmargs.iterator())); -// Log.info("App Args: " + StringUtil.toString(_appargs.iterator())); } /** @@ -238,6 +231,14 @@ public class Application public boolean verifyMetadata () throws IOException { + Log.info("Verifying application: " + _appbase); + Log.info("Version: " + _version); + Log.info("Class: " + _class); +// Log.info("Code: " + StringUtil.toString(_codes.iterator())); +// Log.info("Resources: " + StringUtil.toString(_resources.iterator())); +// Log.info("JVM Args: " + StringUtil.toString(_jvmargs.iterator())); +// Log.info("App Args: " + StringUtil.toString(_appargs.iterator())); + // create our digester which will read in the contents of the // digest file and validate itself try { @@ -293,6 +294,45 @@ public class Application return _version != _targetVersion; } + /** + * Verifies the code and media resources associated with this + * application. A list of resources that do not exist or fail the + * verification process will be returned. If all resources are ready + * to go, null will be returned and the application is considered + * ready to run. + */ + public List verifyResources () + { + ArrayList failures = new ArrayList(); + verifyResources(_codes.iterator(), failures); + verifyResources(_resources.iterator(), failures); + return (failures.size() == 0) ? null : failures; + } + + /** A helper function used by {@link #verifyResources()}. */ + protected void verifyResources (Iterator rsrcs, List failures) + { + while (rsrcs.hasNext()) { + Resource rsrc = (Resource)rsrcs.next(); + if (rsrc.isMarkedValid()) { + continue; + } + + try { + if (_digest.validateResource(rsrc)) { + // make a note that this file is kosher + rsrc.markAsValid(); + continue; + } + + } catch (Exception e) { + Log.info("Failure validating resource [rsrc=" + rsrc + + ", error=" + e + "]. Requesting redownload..."); + } + failures.add(rsrc); + } + } + /** * Downloads a new copy of the specified control file and, if the * download is successful, moves it over the old file on the diff --git a/src/java/com/threerings/getdown/data/Resource.java b/src/java/com/threerings/getdown/data/Resource.java index 1988399..dc9e9a2 100644 --- a/src/java/com/threerings/getdown/data/Resource.java +++ b/src/java/com/threerings/getdown/data/Resource.java @@ -1,5 +1,5 @@ // -// $Id: Resource.java,v 1.2 2004/07/02 15:22:49 mdb Exp $ +// $Id: Resource.java,v 1.3 2004/07/02 17:03:33 mdb Exp $ package com.threerings.getdown.data; @@ -9,11 +9,12 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; - import java.security.MessageDigest; import com.samskivert.util.StringUtil; +import com.threerings.getdown.Log; + /** * Models a single file resource used by an {@link Application}. */ @@ -27,6 +28,7 @@ public class Resource _path = path; _remote = remote; _local = local; + _marker = new File(_local.getPath() + "v"); } /** @@ -54,19 +56,65 @@ public class Resource return StringUtil.hexlate(md.digest()); } + /** + * Returns true if this resource has an associated "validated" marker + * file. + */ + public boolean isMarkedValid () + { + return _marker.exists(); + } + + /** + * Creates a "validated" marker file for this resource to indicate + * that its MD5 hash has been computed and compared with the value in + * the digest file. + * + * @throws IOException if we fail to create the marker file. + */ + public void markAsValid () + throws IOException + { + _marker.createNewFile(); + } + + /** + * Removes any "validated" marker file associated with this resource. + */ + public void clearMarker () + { + if (_marker.exists()) { + if (!_marker.delete()) { + Log.warning("Failed to erase marker file '" + _marker + "'."); + } + } + } + + /** + * Wipes this resource file along with any "validated" marker file + * that may be associated with it. + */ + public void erase () + { + clearMarker(); + if (_local.exists()) { + if (!_local.delete()) { + Log.warning("Failed to erase resource '" + _local + "'."); + } + } + } + /** * Returns a string representation of this instance. */ public String toString () { - // return _path; - // return _netloc.toString(); - return _local.getPath(); + return _path; } protected String _path; protected URL _remote; - protected File _local; + protected File _local, _marker; protected final static int DIGEST_BUFFER_SIZE = 5 * 1025; } diff --git a/src/java/com/threerings/getdown/launcher/Getdown.java b/src/java/com/threerings/getdown/launcher/Getdown.java index aead770..a5e8dec 100644 --- a/src/java/com/threerings/getdown/launcher/Getdown.java +++ b/src/java/com/threerings/getdown/launcher/Getdown.java @@ -1,9 +1,16 @@ // -// $Id: Getdown.java,v 1.2 2004/07/02 15:22:49 mdb Exp $ +// $Id: Getdown.java,v 1.3 2004/07/02 17:03:33 mdb Exp $ package com.threerings.getdown.launcher; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +import java.util.List; + +import org.apache.commons.io.TeeOutputStream; import com.threerings.getdown.Log; import com.threerings.getdown.data.Application; @@ -26,8 +33,15 @@ public class Getdown if (_app.verifyMetadata()) { Log.info("Application requires update."); + } else { Log.info("Metadata verified."); + List failures = _app.verifyResources(); + if (failures == null) { + Log.info("Resources verified."); + } else { + Log.info(failures.size() + " resources require update."); + } } } catch (Exception e) { @@ -50,6 +64,18 @@ public class Getdown System.exit(-1); } +// // tee our output into a file in the application directory +// File log = new File(appDir, "getdown.log"); +// try { +// FileOutputStream fout = new FileOutputStream(log); +// System.setOut(new PrintStream( +// new TeeOutputStream(System.out, fout))); +// System.setErr(new PrintStream( +// new TeeOutputStream(System.err, fout))); +// } catch (IOException ioe) { +// Log.warning("Unable to redirect output to '" + log + "': " + ioe); +// } + try { Getdown app = new Getdown(appDir); app.run();