From daf12104c81f399b97e946882589d679da792d9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Latner?= <matyas.latner@nic.cz> Date: Tue, 14 Jul 2015 16:37:51 +0200 Subject: [PATCH] #17 Downloading assets package for selected language --- build.gradle | 5 +- .../tablexia/loader/zip/ZipAssetLoader.java | 152 ++++++++++++------ util/checksum/build.gradle | 20 --- 3 files changed, 102 insertions(+), 75 deletions(-) diff --git a/build.gradle b/build.gradle index a16360eff..a8e6019a8 100644 --- a/build.gradle +++ b/build.gradle @@ -163,13 +163,13 @@ task zipAssets(dependsOn: [prepareSoundAssets, prepareGraphicAssets]) { } } -task uploadAssets(dependsOn: ':util:checksum:runAssetsArchivesChecksum') { +task uploadAssets(dependsOn: [':util:checksum:runChecksum', 'zipAssets']) { doLast { def data = new URL(ASSETS_DOWNLOAD_URL).getText() rootProject.ext.assetsPackDir.eachFile() { file -> String fileName = file.getName() String[] fileNameParts = fileName.split("\\."); - String packageName = "${fileNameParts[0]}_${assetsPackageChecksum[fileName]}.${fileNameParts[1]}"; + String packageName = "${fileNameParts[0]}_${assetsChecksum[fileNameParts[0]]}.${fileNameParts[1]}"; if (!data.contains(packageName)) { println "AssetsUploader: FILE: ${packageName} DO NOT EXISTS ON SERVER -> UPLOADING FILE" @@ -297,6 +297,7 @@ project(":core") { apply plugin: "java" sourceSets.test.java.srcDirs = ["test/"] + tasks.processResources.dependsOn ':util:checksum:runChecksum' dependencies { compile project(":util:checksum") diff --git a/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java b/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java index 98c3ae46a..ce9769f57 100644 --- a/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java +++ b/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java @@ -44,6 +44,14 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic private static final Object LOCK = new Object(); + + private static boolean downloadResult = false; + private static Long startTime = null; + + public void load(Locale locale, Map<String, String> buildChecksum) { + setAsyncTask(new ZipAssetLoaderTask(locale, buildChecksum)); + } + private static class ZipAssetLoaderTask implements AsyncTask<Void> { private Locale locale; @@ -54,48 +62,59 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic this.buildChecksums = buildChecksums; } - private boolean checkAssets(String buildChecksum, String extractDestinationDirectory) { - File file = new File(extractDestinationDirectory); - if (file.exists() && file.isDirectory()) { - try { - String runtimeChecksum = Checksum.getMd5OfDir(file); - Log.debug(getClass(), "Comparing assets checksums: [BUILD: " + buildChecksum + "] - [RUNTIME: " + runtimeChecksum + "]"); - return runtimeChecksum.equals(buildChecksum); - } catch (NoSuchAlgorithmException e) { - Log.err(getClass(), "Cannot get checksum for assets!", e); - } catch (IOException e) { - Log.err(getClass(), "Cannot get checksum for assets!", e); - } - } - return false; - } - @Override public Void call() throws Exception { String language = locale.getLanguage(); String extractDestinationDirectory = TablexiaAbstractFileManager.getFileStoragePath(TablexiaAbstractFileManager.AssetsStorageType.EXTERNAL); - Long startTime = null; - if (TablexiaSettings.getInstance().isDebug()) { - startTime = System.nanoTime(); - } - - prepareKeyStore(); - download("cs.zip"); - download("sk.zip"); - - // check current content if (buildChecksums != null) { String buildChecksum = buildChecksums.get(language); if (buildChecksum != null) { + + // check current content + Log.info(ZipAssetLoader.class, "Assets check started..."); + startTimer(); boolean checksumResult = checkAssets((buildChecksum), extractDestinationDirectory); - if (startTime != null) { - Log.debug(getClass(), String.format("Checksum delay: %05f s", (System.nanoTime() - startTime) / 1000000000.0)); - } + resultTimer("Assets check delay"); if (checksumResult) { - Log.info(getClass(), "Assets check OK! --> Continue to loading"); + Log.info(ZipAssetLoader.class, "Assets check OK!"); + Log.info(ZipAssetLoader.class, "Continue to loading"); return null; } + Log.info(ZipAssetLoader.class, "Assets check FAILED!"); + + + String assetsPackageName = language + "_" + buildChecksum + ZIP_FILE_EXTENSION; + FileHandle assetsPackageFileHandle = TablexiaAbstractFileManager.getFileStoragePathFileHandle(ZIP_FILES_STORAGE_TYPE, assetsPackageName); + + // download assets package + if (!assetsPackageFileHandle.exists()) { + Log.info(ZipAssetLoader.class, String.format("Assets package with name %s not found!", assetsPackageName)); + Log.info(ZipAssetLoader.class, String.format("Downloading assets package: %s", assetsPackageName)); + prepareKeyStore(); + startTimer(); + download(assetsPackageName); + resultTimer("Download delay"); + } + + + // delete modified or old content + File eddFile = new File(extractDestinationDirectory); + if (eddFile.exists()) { + Log.info(ZipAssetLoader.class, "Found invalid assets content!"); + Log.info(ZipAssetLoader.class, String.format("Deleting content under: %s", extractDestinationDirectory)); + startTimer(); + deleteDirectory(eddFile); + resultTimer("Delete delay"); + } + + + // extract new content + Log.info(ZipAssetLoader.class, String.format("Extracting new content to: %s", extractDestinationDirectory)); + startTimer(); + unzip(assetsPackageFileHandle, extractDestinationDirectory); + resultTimer("Extract delay"); + } else { Log.err(getClass(), "BUILD CHECKSUM: no build checksum specified for language " + language); } @@ -103,28 +122,30 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic Log.err(getClass(), "BUILD CHECKSUM: no build checksum specified"); } - if (TablexiaSettings.getInstance().isDebug()) { - startTime = System.nanoTime(); - } + return null; + } - // delete modified or old content - File eddFile = new File(extractDestinationDirectory); - if (eddFile.exists()) { - Log.info(getClass(), "Assets check FAILED! --> Deleting content under: " + extractDestinationDirectory); - deleteDirectory(eddFile); - } - // extract new content - Log.info(getClass(), "Assets check FAILED! --> Extracting new assets to: " + extractDestinationDirectory); - String localisedZipFile = language + ZIP_FILE_EXTENSION; - unzip(ZIP_FILES_STORAGE_TYPE.getResolver().resolve(ZIP_FILES_STORAGE_TYPE.getStoragePath() + localisedZipFile), extractDestinationDirectory); +//////////////////////////// CHECK - if (startTime != null) { - Log.debug(getClass(), String.format("Unzip delay: %05f s", (System.nanoTime() - startTime) / 1000000000.0)); + private boolean checkAssets(String buildChecksum, String extractDestinationDirectory) { + File file = new File(extractDestinationDirectory); + if (file.exists() && file.isDirectory()) { + try { + String runtimeChecksum = Checksum.getMd5OfDir(file); + Log.debug(ZipAssetLoader.class, "Comparing assets checksums: [BUILD: " + buildChecksum + "] - [RUNTIME: " + runtimeChecksum + "]"); + return runtimeChecksum.equals(buildChecksum); + } catch (NoSuchAlgorithmException e) { + Log.err(getClass(), "Cannot get checksum for assets!", e); + } catch (IOException e) { + Log.err(getClass(), "Cannot get checksum for assets!", e); + } } + return false; + } - return null; - } + +//////////////////////////// DOWNLOAD private void prepareKeyStore() { FileHandle keystoreExternal = TablexiaAbstractFileManager.getFileStoragePathFileHandle(TablexiaAbstractFileManager.KeystoreStorageType.EXTERNAL, TABLEXIA_TRUST_KEYSTORE_NAME); @@ -135,7 +156,7 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic System.setProperty("javax.net.ssl.trustStorePassword", TABLEXIA_TRUST_KEYSTORE_PASSWORD); } - public void download(final String fileName) { + public boolean download(final String fileName) { Net.HttpRequest request = new Net.HttpRequest(Net.HttpMethods.GET); request.setTimeOut(TABLEXIA_ASSETS_DOWNLOAD_TIMEOUT); @@ -162,11 +183,12 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic ApplicationBus.getInstance().post(new AbstractTablexiaScreen.ScreenInfoEvent("Downloading: ", fileName + " [ " + progress + "% ]")).asynchronously(); } + downloadResult = true; + // download complete synchronized (LOCK) { LOCK.notify(); } - } catch (IOException e) { Log.err(ZipAssetLoader.class, "Cannot download file: " + fileName + " !", e); } @@ -175,11 +197,19 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic @Override public void failed(Throwable t) { Log.err(ZipAssetLoader.class, "Downloading of file: " + fileName + " Failed!", t); + downloadResult = false; + synchronized (LOCK) { + LOCK.notify(); + } } @Override public void cancelled() { Log.info(ZipAssetLoader.class, "Downloading of file: " + fileName + " Canceled!"); + downloadResult = false; + synchronized (LOCK) { + LOCK.notify(); + } } }); @@ -191,8 +221,13 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic Log.err(ZipAssetLoader.class, "Cannot wait to download end!", e); } } + + return downloadResult; } + +//////////////////////////// EXTRACT + public static void unzip(FileHandle zipFile, String extractDestinationDirectory) throws IOException { ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(zipFile.read())); try { @@ -245,9 +280,20 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic return(directory.delete()); } } - - public void load(Locale locale, Map<String, String> buildChecksum) { - setAsyncTask(new ZipAssetLoaderTask(locale, buildChecksum)); - } + + +//////////////////////////// TIMER + + public static void startTimer() { + if (TablexiaSettings.getInstance().isDebug()) { + startTime = System.nanoTime(); + } + } + + public static void resultTimer(String message) { + if (startTime != null) { + Log.debug(ZipAssetLoader.class, String.format("%s: %05f s", message, (System.nanoTime() - startTime) / 1000000000.0)); + } + } } \ No newline at end of file diff --git a/util/checksum/build.gradle b/util/checksum/build.gradle index 84dbc9a66..cdb5da13c 100644 --- a/util/checksum/build.gradle +++ b/util/checksum/build.gradle @@ -24,23 +24,3 @@ task runChecksum(dependsOn: [classes, rootProject.tasks.prepareSoundAssets, root } } } - -task runAssetsArchivesChecksum(dependsOn: [classes, rootProject.tasks.zipAssets]) { - doLast { - rootProject.ext.assetsPackDir.eachFile() { file -> - String fileName = file.getName() - task ("${name}_${fileName}", type: JavaExec) { - main = MAIN_CLASS - args = [file] - classpath sourceSets.main.output.classesDir - classpath += sourceSets.main.runtimeClasspath - def stdout = new ByteArrayOutputStream() - standardOutput = stdout - - doLast { - rootProject.ext.assetsPackageChecksum[fileName] = stdout.toString().trim() - } - }.execute() - } - } -} -- GitLab