修复组合任务在获取子任务信息的过程中,手动停止或删除,没有回调的问题

This commit is contained in:
laoyuyu
2020-12-09 21:22:01 +08:00
parent 35e66caab5
commit 9db8f1e9c0
16 changed files with 174 additions and 54 deletions

View File

@@ -27,6 +27,7 @@ import aria.apache.commons.net.ftp.FTPSClient;
import com.arialyy.aria.core.AriaConfig;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.common.AbsEntity;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.common.FtpConnectionMode;
import com.arialyy.aria.core.loader.IInfoTask;
import com.arialyy.aria.core.loader.ILoaderVisitor;
@@ -55,7 +56,8 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
private int mConnectTimeOut;
protected long mSize = 0;
protected String charSet = "UTF-8";
protected Callback callback;
private Callback callback;
private boolean isStop = false, isCancel = false;
public AbsFtpInfoTask(TASK_WRAPPER taskWrapper) {
mTaskWrapper = taskWrapper;
@@ -81,6 +83,14 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
protected abstract void handelFileInfo(FTPClient client, FTPFile[] files,
String convertedRemotePath) throws IOException;
@Override public void stop() {
isStop = true;
}
@Override public void cancel() {
isCancel = true;
}
@Override public void setCallback(Callback callback) {
this.callback = callback;
}
@@ -101,10 +111,10 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
handelFileInfo(client, files, convertedRemotePath);
} catch (IOException e) {
e.printStackTrace();
failDownload(client, "FTP错误信息", e, true);
handleFail(client, "FTP错误信息", e, true);
} catch (InterruptedException e) {
e.printStackTrace();
failDownload(client, "FTP错误信息", e, true);
handleFail(client, "FTP错误信息", e, true);
} finally {
closeClient(client);
}
@@ -146,7 +156,7 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
}
if (client == null) {
failDownload(client, String.format("链接失败, url: %s", mTaskOption.getUrlEntity().url), null,
handleFail(client, String.format("链接失败, url: %s", mTaskOption.getUrlEntity().url), null,
true);
return null;
}
@@ -168,14 +178,14 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
}
if (!loginSuccess) {
failDownload(client, "登录失败", null, false);
handleFail(client, "登录失败", null, false);
client.disconnect();
return null;
}
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
failDownload(client, String.format("无法连接到ftp服务器filePath: %s, url: %s", mEntity.getKey(),
handleFail(client, String.format("无法连接到ftp服务器filePath: %s, url: %s", mEntity.getKey(),
mTaskOption.getUrlEntity().url), null, true);
client.disconnect();
return null;
@@ -299,7 +309,7 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
for (FTPFile file : files) {
if (file.isFile()) {
size += file.getSize();
handleFile(path + file.getName(), file);
handleFile(client, path + file.getName(), file);
} else {
String remotePath = CommonUtil.convertFtpChar(charSet, path + file.getName());
size += getFileSize(client.listFiles(remotePath), client, path + file.getName());
@@ -314,10 +324,13 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
* @param remotePath ftp服务器文件夹路径
* @param ftpFile ftp服务器上对应的文件
*/
protected void handleFile(String remotePath, FTPFile ftpFile) {
protected void handleFile(FTPClient client, String remotePath, FTPFile ftpFile) {
}
protected void failDownload(FTPClient client, String msg, Exception e, boolean needRetry) {
protected void handleFail(FTPClient client, String msg, Exception e, boolean needRetry) {
if (isStop || isCancel) {
return;
}
if (callback != null) {
if (client == null) {
msg = "创建ftp客户端失败";
@@ -332,6 +345,10 @@ public abstract class AbsFtpInfoTask<ENTITY extends AbsEntity, TASK_WRAPPER exte
}
}
protected void onSucceed(CompleteInfo info){
callback.onSucceed(mEntity.getKey(), info);
}
@Override public void accept(ILoaderVisitor visitor) {
visitor.addComponent(this);
}

View File

@@ -38,10 +38,10 @@ final class FtpDFileInfoTask extends AbsFtpInfoTask<DownloadEntity, DTaskWrapper
super(taskEntity);
}
@Override protected void handleFile(String remotePath, FTPFile ftpFile) {
super.handleFile(remotePath, ftpFile);
@Override protected void handleFile(FTPClient client, String remotePath, FTPFile ftpFile) {
super.handleFile(client, remotePath, ftpFile);
if (!FileUtil.checkMemorySpace(mEntity.getFilePath(), ftpFile.getSize())) {
callback.onFail(mEntity, new AriaFTPException(
handleFail(client, "内存空间不足", new AriaFTPException(
String.format("获取ftp文件信息失败内存空间不足, filePath: %s", mEntity.getFilePath())),
false);
}
@@ -72,7 +72,7 @@ final class FtpDFileInfoTask extends AbsFtpInfoTask<DownloadEntity, DTaskWrapper
}
closeClient(client);
failDownload(client,
handleFail(client,
String.format("文件不存在url: %s, remotePath%s", mTaskOption.getUrlEntity().url,
getRemotePath()), null, false);
return;
@@ -90,7 +90,7 @@ final class FtpDFileInfoTask extends AbsFtpInfoTask<DownloadEntity, DTaskWrapper
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
closeClient(client);
failDownload(client, "获取文件信息错误url: " + mTaskOption.getUrlEntity().url, null, true);
handleFail(client, "获取文件信息错误url: " + mTaskOption.getUrlEntity().url, null, true);
return;
}
mTaskWrapper.setCode(reply);
@@ -112,6 +112,6 @@ final class FtpDFileInfoTask extends AbsFtpInfoTask<DownloadEntity, DTaskWrapper
mTaskWrapper.setNewTask(true);
}
mEntity.setFileSize(mSize);
callback.onSucceed(mEntity.getUrl(), new CompleteInfo(code, mTaskWrapper));
onSucceed(new CompleteInfo(code, mTaskWrapper));
}
}

View File

@@ -49,7 +49,7 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
@Override public void run() {
if (mTaskWrapper.getEntity().getFileSize() > 1 && checkSubOption()) {
callback.onSucceed(mEntity.getKey(), new CompleteInfo(200, mTaskWrapper));
onSucceed(new CompleteInfo(200, mTaskWrapper));
} else {
super.run();
}
@@ -80,7 +80,7 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
}
closeClient(client);
failDownload(client,
handleFail(client,
String.format("文件不存在url: %s, remotePath%s", mTaskOption.getUrlEntity().url,
getRemotePath()), null, false);
return;
@@ -98,7 +98,7 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
closeClient(client);
failDownload(client, "获取文件信息错误url: " + mTaskOption.getUrlEntity().url, null, true);
handleFail(client, "获取文件信息错误url: " + mTaskOption.getUrlEntity().url, null, true);
return;
}
mTaskWrapper.setCode(reply);
@@ -127,8 +127,8 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
return mTaskOption.getUrlEntity().remotePath;
}
@Override protected void handleFile(String remotePath, FTPFile ftpFile) {
super.handleFile(remotePath, ftpFile);
@Override protected void handleFile(FTPClient client, String remotePath, FTPFile ftpFile) {
super.handleFile(client, remotePath, ftpFile);
addEntity(remotePath, ftpFile);
}
@@ -138,7 +138,7 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
for (DTaskWrapper wrapper : mTaskWrapper.getSubTaskWrapper()) {
cloneInfo(wrapper);
}
callback.onSucceed(mEntity.getKey(), new CompleteInfo(code, mTaskWrapper));
onSucceed(new CompleteInfo(code, mTaskWrapper));
}
private void cloneInfo(DTaskWrapper subWrapper) {
@@ -234,8 +234,8 @@ final class FtpDGInfoTask extends AbsFtpInfoTask<DownloadGroupEntity, DGTaskWrap
}
@Override
protected void failDownload(FTPClient client, String msg, Exception e, boolean needRetry) {
super.failDownload(client, msg, e, needRetry);
protected void handleFail(FTPClient client, String msg, Exception e, boolean needRetry) {
super.handleFail(client, msg, e, needRetry);
DeleteDGRecord.getInstance().deleteRecord(mTaskWrapper.getEntity(), true, true);
}
}

View File

@@ -60,7 +60,7 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
return;
}
handleFile(getRemotePath(), files.length == 0 ? null : files[0]);
handleFile(client, getRemotePath(), files.length == 0 ? null : files[0]);
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
//服务器上没有该文件路径,表示该任务为新的上传任务
@@ -133,8 +133,8 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
* @param remotePath ftp服务器文件夹路径
* @param ftpFile ftp服务器上对应的文件
*/
@Override protected void handleFile(String remotePath, FTPFile ftpFile) {
super.handleFile(remotePath, ftpFile);
@Override protected void handleFile(FTPClient client,String remotePath, FTPFile ftpFile) {
super.handleFile(client, remotePath, ftpFile);
this.ftpFile = ftpFile;
if (ftpFile != null && ftpFile.getSize() == mEntity.getFileSize()) {
isComplete = true;
@@ -145,6 +145,6 @@ final class FtpUFileInfoTask extends AbsFtpInfoTask<UploadEntity, UTaskWrapper>
super.onPreComplete(code);
CompleteInfo info = new CompleteInfo(isComplete ? CODE_COMPLETE : code, mTaskWrapper);
info.obj = ftpFile;
callback.onSucceed(mEntity.getKey(), info);
onSucceed(info);
}
}