cache client: download indexes even if revision is up to date

Also add DownloadWatcher to watch download progress
This commit is contained in:
Adam
2017-09-11 16:06:49 -04:00
parent 218333e97c
commit ed05ab93a5
2 changed files with 92 additions and 44 deletions

View File

@@ -68,6 +68,7 @@ public class CacheClient implements AutoCloseable
private final Store store; // store cache will be written to private final Store store; // store cache will be written to
private final String host; private final String host;
private final int clientRevision; private final int clientRevision;
private DownloadWatcher watcher;
private ClientState state; private ClientState state;
@@ -89,6 +90,12 @@ public class CacheClient implements AutoCloseable
this.clientRevision = clientRevision; this.clientRevision = clientRevision;
} }
public CacheClient(Store store, int clientRevision, DownloadWatcher watcher)
{
this(store, clientRevision);
this.watcher = watcher;
}
public void connect() public void connect()
{ {
Bootstrap b = new Bootstrap(); Bootstrap b = new Bootstrap();
@@ -226,8 +233,9 @@ public class CacheClient implements AutoCloseable
} }
else else
{ {
// despite the index being up to date, not everything
// can be downloaded, eg. for tracks.
logger.info("Index {} is up to date", index.getId()); logger.info("Index {} is up to date", index.getId());
continue;
} }
logger.info("Downloading index {}", i); logger.info("Downloading index {}", i);
@@ -250,11 +258,10 @@ public class CacheClient implements AutoCloseable
{ {
index = store.addIndex(i); index = store.addIndex(i);
} }
else
{ // update index crc and revision
// update index crc
index.setCrc(crc); index.setCrc(crc);
} index.setRevision(revision);
logger.info("Index {} has {} archives", i, index.getArchives().size()); logger.info("Index {} has {} archives", i, index.getArchives().size());
@@ -262,8 +269,13 @@ public class CacheClient implements AutoCloseable
{ {
Archive existing = index.getArchive(ad.getId()); Archive existing = index.getArchive(ad.getId());
if (existing == null || existing.getRevision() != ad.getRevision()) if (existing != null && existing.getRevision() == ad.getRevision())
{ {
logger.info("Archive {}/{} in index {} is up to date",
ad.getId(), indexData.getArchives().length, index.getId());
continue;
}
if (existing == null) if (existing == null)
{ {
logger.info("Archive {}/{} in index {} is out of date, downloading", logger.info("Archive {}/{} in index {} is out of date, downloading",
@@ -286,8 +298,12 @@ public class CacheClient implements AutoCloseable
? index.addArchive(ad.getId()) ? index.addArchive(ad.getId())
: existing; : existing;
archive.setRevision(ad.getRevision());
archive.setCrc(ad.getCrc());
archive.setNameHash(ad.getNameHash());
// Add files // Add files
archive.getFiles().clear(); archive.clearFiles();
for (FileData fd : ad.getFiles()) for (FileData fd : ad.getFiles())
{ {
FSFile file = archive.addFile(fd.getId()); FSFile file = archive.addFile(fd.getId());
@@ -298,15 +314,14 @@ public class CacheClient implements AutoCloseable
future.handle((fr, ex) -> future.handle((fr, ex) ->
{ {
archive.setData(fr.getCompressedData()); archive.setData(fr.getCompressedData());
if (watcher != null)
{
watcher.downloadComplete(archive);
}
return null; return null;
}); });
} }
else
{
logger.info("Active {}/{} in index {} is up to date",
ad.getId(), indexData.getArchives().length, index.getId());
}
}
} }
// flush any pending requests // flush any pending requests

View File

@@ -0,0 +1,33 @@
/*
* Copyright (c) 2016-2017, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.cache.client;
import net.runelite.cache.fs.Archive;
@FunctionalInterface
public interface DownloadWatcher
{
void downloadComplete(Archive archive);
}