Skip to content

Commit

Permalink
refactor: retry from offset saved in db
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianhua Ran committed Mar 5, 2018
1 parent 5646236 commit e45ffa5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ public void onCompleted(DownloadRunnable doneRunnable, long startOffset, long en
return;
}

final int doneConnectionIndex = doneRunnable == null ? -1 : doneRunnable.connectionIndex;
final int doneConnectionIndex = doneRunnable.connectionIndex;
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "the connection has been completed(%d): [%d, %d) %d",
doneConnectionIndex, startOffset, endOffset, model.getTotal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
import android.os.Process;

import com.liulishuo.filedownloader.connection.FileDownloadConnection;
import com.liulishuo.filedownloader.database.FileDownloadDatabase;
import com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException;
import com.liulishuo.filedownloader.model.ConnectionModel;
import com.liulishuo.filedownloader.model.FileDownloadHeader;
import com.liulishuo.filedownloader.model.FileDownloadModel;
import com.liulishuo.filedownloader.util.FileDownloadLog;
import com.liulishuo.filedownloader.util.FileDownloadUtils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.util.List;

/**
* The single download runnable used for establish one connection and fetch data from it.
Expand Down Expand Up @@ -131,7 +135,10 @@ public void run() {
} else {
if (fetchDataTask != null) {
//update currentOffset in ConnectionProfile
connectTask.updateConnectionProfile(fetchDataTask.currentOffset);
final long downloadedOffset = getDownloadedOffset();
if (downloadedOffset > 0) {
connectTask.updateConnectionProfile(fetchDataTask.currentOffset);
}
}
callback.onRetry(e);
}
Expand All @@ -147,6 +154,24 @@ public void run() {

}

private long getDownloadedOffset() {
final FileDownloadDatabase database = CustomComponentHolder.getImpl().getDatabaseInstance();
if (connectionIndex >= 0) {
// is multi connection
List<ConnectionModel> connectionModels = database.findConnectionModel(downloadId);
for (ConnectionModel connectionModel : connectionModels) {
if (connectionModel.getIndex() == connectionIndex) {
return connectionModel.getCurrentOffset();
}
}
} else {
// is single connection
FileDownloadModel downloadModel = database.find(downloadId);
return downloadModel.getSoFar();
}
return 0;
}

public static class Builder {
private final ConnectTask.Builder connectTaskBuilder = new ConnectTask.Builder();
private ProcessCallback callback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private void sync() {
}

if (bufferPersistToDevice) {
final boolean isBelongMultiConnection = hostRunnable != null;
final boolean isBelongMultiConnection = connectionIndex >= 0;
if (isBelongMultiConnection) {
// only need update the connection table.
database.updateConnectionModel(downloadId, connectionIndex, currentOffset);
Expand Down

0 comments on commit e45ffa5

Please sign in to comment.