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 String host;
private final int clientRevision;
private DownloadWatcher watcher;
private ClientState state;
@@ -89,6 +90,12 @@ public class CacheClient implements AutoCloseable
this.clientRevision = clientRevision;
}
public CacheClient(Store store, int clientRevision, DownloadWatcher watcher)
{
this(store, clientRevision);
this.watcher = watcher;
}
public void connect()
{
Bootstrap b = new Bootstrap();
@@ -226,8 +233,9 @@ public class CacheClient implements AutoCloseable
}
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());
continue;
}
logger.info("Downloading index {}", i);
@@ -250,11 +258,10 @@ public class CacheClient implements AutoCloseable
{
index = store.addIndex(i);
}
else
{
// update index crc
index.setCrc(crc);
}
// update index crc and revision
index.setCrc(crc);
index.setRevision(revision);
logger.info("Index {} has {} archives", i, index.getArchives().size());
@@ -262,50 +269,58 @@ public class CacheClient implements AutoCloseable
{
Archive existing = index.getArchive(ad.getId());
if (existing == null || existing.getRevision() != ad.getRevision())
if (existing != null && existing.getRevision() == ad.getRevision())
{
if (existing == null)
{
logger.info("Archive {}/{} in index {} is out of date, downloading",
ad.getId(), indexData.getArchives().length, index.getId());
}
else if (ad.getRevision() < existing.getRevision())
{
logger.warn("Archive {}/{} in index {} revision is going BACKWARDS! (our revision {}, their revision {})",
ad.getId(), indexData.getArchives().length, index.getId(),
existing.getRevision(), ad.getRevision());
}
else
{
logger.info("Archive {}/{} in index {} is out of date ({} != {}), downloading",
ad.getId(), indexData.getArchives().length, index.getId(),
existing.getRevision(), ad.getRevision());
}
logger.info("Archive {}/{} in index {} is up to date",
ad.getId(), indexData.getArchives().length, index.getId());
continue;
}
final Archive archive = existing == null
? index.addArchive(ad.getId())
: existing;
// Add files
archive.getFiles().clear();
for (FileData fd : ad.getFiles())
{
FSFile file = archive.addFile(fd.getId());
file.setNameHash(fd.getNameHash());
}
CompletableFuture<FileResult> future = requestFile(index.getId(), ad.getId(), false);
future.handle((fr, ex) ->
{
archive.setData(fr.getCompressedData());
return null;
});
if (existing == null)
{
logger.info("Archive {}/{} in index {} is out of date, downloading",
ad.getId(), indexData.getArchives().length, index.getId());
}
else if (ad.getRevision() < existing.getRevision())
{
logger.warn("Archive {}/{} in index {} revision is going BACKWARDS! (our revision {}, their revision {})",
ad.getId(), indexData.getArchives().length, index.getId(),
existing.getRevision(), ad.getRevision());
}
else
{
logger.info("Active {}/{} in index {} is up to date",
ad.getId(), indexData.getArchives().length, index.getId());
logger.info("Archive {}/{} in index {} is out of date ({} != {}), downloading",
ad.getId(), indexData.getArchives().length, index.getId(),
existing.getRevision(), ad.getRevision());
}
final Archive archive = existing == null
? index.addArchive(ad.getId())
: existing;
archive.setRevision(ad.getRevision());
archive.setCrc(ad.getCrc());
archive.setNameHash(ad.getNameHash());
// Add files
archive.clearFiles();
for (FileData fd : ad.getFiles())
{
FSFile file = archive.addFile(fd.getId());
file.setNameHash(fd.getNameHash());
}
CompletableFuture<FileResult> future = requestFile(index.getId(), ad.getId(), false);
future.handle((fr, ex) ->
{
archive.setData(fr.getCompressedData());
if (watcher != null)
{
watcher.downloadComplete(archive);
}
return null;
});
}
}

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);
}