增加中断条件判断

pull/330/head
laoyuyu 7 years ago
parent 2e4ad16f3e
commit 426c148370
  1. 2
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFtpThreadTask.java
  2. 109
      Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  3. 8
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpThreadTask.java
  4. 10
      Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java
  5. 6
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java
  6. 7
      app/src/main/AndroidManifest.xml
  7. 2
      app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java

@ -87,7 +87,7 @@ public abstract class AbsFtpThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTI
int reply = client.getReplyCode(); int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) { if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect(); client.disconnect();
fail(STATE.CURRENT_LOCATION, fail(mChildCurrentLocation,
"无法连接到ftp服务器,错误码为:" + reply + ",msg:" + client.getReplyString(), null); "无法连接到ftp服务器,错误码为:" + reply + ",msg:" + client.getReplyString(), null);
return null; return null;
} }

@ -24,7 +24,6 @@ import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.ErrorHelp; import com.arialyy.aria.util.ErrorHelp;
import com.arialyy.aria.util.NetUtils; import com.arialyy.aria.util.NetUtils;
import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@ -61,15 +60,12 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
protected int mConnectTimeOut; //连接超时时间 protected int mConnectTimeOut; //连接超时时间
protected int mReadTimeOut; //流读取的超时时间 protected int mReadTimeOut; //流读取的超时时间
protected boolean isNotNetRetry = false; //断网情况是否重试 protected boolean isNotNetRetry = false; //断网情况是否重试
protected boolean taskBreak = false; //任务中断
private Thread mConfigThread = new Thread(new Runnable() { private Thread mConfigThread = new Thread(new Runnable() {
@Override public void run() { @Override public void run() {
final long currentTemp = mChildCurrentLocation; final long currentTemp = mChildCurrentLocation;
try { writeConfig(false, currentTemp);
writeConfig(false, currentTemp);
} catch (IOException e) {
e.printStackTrace();
}
} }
}); });
@ -108,35 +104,43 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
} }
} }
/**
* 任务是否中断中断条件
* 1任务取消
* 2任务停止
* 3手动中断 {@link #taskBreak}
*
* @return {@code true} 中断{@code false} 不是中断
*/
protected boolean isBreak() {
return STATE.isCancel || STATE.isStop || taskBreak;
}
/** /**
* 停止任务 * 停止任务
*/ */
public void stop() { public void stop() {
synchronized (AriaManager.LOCK) { synchronized (AriaManager.LOCK) {
try { if (mConfig.SUPPORT_BP) {
if (mConfig.SUPPORT_BP) { final long currentTemp = mChildCurrentLocation;
final long currentTemp = mChildCurrentLocation; STATE.STOP_NUM++;
STATE.STOP_NUM++; ALog.d(TAG, "任务【"
ALog.d(TAG, "任务【" + mConfig.TEMP_FILE.getName()
+ mConfig.TEMP_FILE.getName() + "】thread__"
+ "】thread__" + mConfig.THREAD_ID
+ mConfig.THREAD_ID + "__停止【停止位置: "
+ "__停止【停止位置: " + currentTemp
+ currentTemp + "】");
+ "】"); writeConfig(false, currentTemp);
writeConfig(false, currentTemp); if (STATE.isStop()) {
if (STATE.isStop()) {
ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】已停止");
STATE.isRunning = false;
mListener.onStop(STATE.CURRENT_LOCATION);
}
} else {
ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】已停止"); ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】已停止");
STATE.isRunning = false; STATE.isRunning = false;
mListener.onStop(STATE.CURRENT_LOCATION); mListener.onStop(STATE.CURRENT_LOCATION);
} }
} catch (IOException e) { } else {
e.printStackTrace(); ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】已停止");
STATE.isRunning = false;
mListener.onStop(STATE.CURRENT_LOCATION);
} }
} }
} }
@ -146,6 +150,11 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
*/ */
protected void progress(long len) { protected void progress(long len) {
synchronized (AriaManager.LOCK) { synchronized (AriaManager.LOCK) {
if (STATE.CURRENT_LOCATION > mEntity.getFileSize()) {
taskBreak = true;
fail(mChildCurrentLocation, "下载失败,下载长度超出文件大小", null, false);
return;
}
mChildCurrentLocation += len; mChildCurrentLocation += len;
STATE.CURRENT_LOCATION += len; STATE.CURRENT_LOCATION += len;
if (System.currentTimeMillis() - mLastSaveTime > 5000 if (System.currentTimeMillis() - mLastSaveTime > 5000
@ -182,27 +191,36 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
} }
} }
/**
* 线程任务失败
*
* @param subCurrentLocation 当前线程下载进度
* @param msg 自定义信息
* @param ex 异常信息
*/
protected void fail(final long subCurrentLocation, String msg, Exception ex) {
fail(subCurrentLocation, msg, ex, true);
}
/** /**
* 任务失败 * 任务失败
*
* @param subCurrentLocation 当前子线程进度
*/ */
protected void fail(final long currentLocation, String msg, Exception ex) { protected void fail(final long subCurrentLocation, String msg, Exception ex, boolean needRetry) {
synchronized (AriaManager.LOCK) { synchronized (AriaManager.LOCK) {
try { if (ex != null) {
if (ex != null) { ALog.e(TAG, msg + "\n" + ALog.getExceptionString(ex));
ALog.e(TAG, msg + "\n" + ALog.getExceptionString(ex)); } else {
} else { ALog.e(TAG, msg);
ALog.e(TAG, msg); }
} if (mConfig.SUPPORT_BP) {
if (mConfig.SUPPORT_BP) { writeConfig(false, subCurrentLocation);
writeConfig(false, currentLocation); retryThis(STATE.START_THREAD_NUM != 1);
retryThis(STATE.START_THREAD_NUM != 1); } else {
} else { ALog.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
ALog.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败"); mListener.onFail(true);
mListener.onFail(true); ErrorHelp.saveError(TAG, "", ALog.getExceptionString(ex));
ErrorHelp.saveError(TAG, "", ALog.getExceptionString(ex));
}
} catch (IOException e) {
e.printStackTrace();
} }
} }
} }
@ -233,9 +251,8 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
mFailNum++; mFailNum++;
ALog.w(TAG, ALog.w(TAG,
"任务【" + mConfig.TEMP_FILE.getName() + "】thread__" + mConfig.THREAD_ID + "__正在重试"); "任务【" + mConfig.TEMP_FILE.getName() + "】thread__" + mConfig.THREAD_ID + "__正在重试");
final long retryLocation = mConfig.START_LOCATION =
mChildCurrentLocation == 0 ? mConfig.START_LOCATION : mChildCurrentLocation; mChildCurrentLocation == 0 ? mConfig.START_LOCATION : mChildCurrentLocation;
mConfig.START_LOCATION = retryLocation;
AbsThreadTask.this.run(); AbsThreadTask.this.run();
} }
}, RETRY_INTERVAL); }, RETRY_INTERVAL);
@ -253,7 +270,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY
/** /**
* 将记录写入到配置文件 * 将记录写入到配置文件
*/ */
protected void writeConfig(boolean isComplete, final long record) throws IOException { protected void writeConfig(boolean isComplete, final long record) {
if (mConfig.THREAD_RECORD != null) { if (mConfig.THREAD_RECORD != null) {
mConfig.THREAD_RECORD.isComplete = isComplete; mConfig.THREAD_RECORD.isComplete = isComplete;
if (0 < record && record < mConfig.END_LOCATION) { if (0 < record && record < mConfig.END_LOCATION) {

@ -101,7 +101,9 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
readNormal(is); readNormal(is);
} }
if (STATE.isCancel || STATE.isStop) return; if (isBreak()){
return;
}
ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__下载完毕"); ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__下载完毕");
writeConfig(true, 1); writeConfig(true, 1);
STATE.COMPLETE_THREAD_NUM++; STATE.COMPLETE_THREAD_NUM++;
@ -146,7 +148,7 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
fic = Channels.newChannel(is); fic = Channels.newChannel(is);
ByteBuffer bf = ByteBuffer.allocate(mBufSize); ByteBuffer bf = ByteBuffer.allocate(mBufSize);
while ((len = fic.read(bf)) != -1) { while ((len = fic.read(bf)) != -1) {
if (STATE.isCancel || STATE.isStop) { if (isBreak()) {
break; break;
} }
if (mSleepTime > 0) { if (mSleepTime > 0) {
@ -189,7 +191,7 @@ class FtpThreadTask extends AbsFtpThreadTask<DownloadEntity, DownloadTaskEntity>
byte[] buffer = new byte[mBufSize]; byte[] buffer = new byte[mBufSize];
int len; int len;
while ((len = is.read(buffer)) != -1) { while ((len = is.read(buffer)) != -1) {
if (STATE.isCancel || STATE.isStop) { if (isBreak()) {
break; break;
} }
if (mSleepTime > 0) Thread.sleep(mSleepTime); if (mSleepTime > 0) Thread.sleep(mSleepTime);

@ -101,7 +101,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
} }
} }
if (STATE.isCancel || STATE.isStop) { if (isBreak()) {
return; return;
} }
handleComplete(); handleComplete();
@ -142,7 +142,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
fic = Channels.newChannel(is); fic = Channels.newChannel(is);
ByteBuffer bf = ByteBuffer.allocate(mBufSize); ByteBuffer bf = ByteBuffer.allocate(mBufSize);
while ((len = fic.read(bf)) != -1) { while ((len = fic.read(bf)) != -1) {
if (STATE.isCancel || STATE.isStop) { if (isBreak()) {
break; break;
} }
if (mSleepTime > 0) { if (mSleepTime > 0) {
@ -192,7 +192,7 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
byte[] buffer = new byte[mBufSize]; byte[] buffer = new byte[mBufSize];
int len; int len;
while ((len = is.read(buffer)) != -1) { while ((len = is.read(buffer)) != -1) {
if (STATE.isCancel || STATE.isStop) { if (isBreak()) {
break; break;
} }
if (mSleepTime > 0) { if (mSleepTime > 0) {
@ -205,10 +205,8 @@ final class HttpThreadTask extends AbsThreadTask<DownloadEntity, DownloadTaskEnt
/** /**
* 处理完成配置文件的更新或事件回调 * 处理完成配置文件的更新或事件回调
*
* @throws IOException
*/ */
private void handleComplete() throws IOException { private void handleComplete() {
//支持断点的处理 //支持断点的处理
if (mConfig.SUPPORT_BP) { if (mConfig.SUPPORT_BP) {
if (mChildCurrentLocation == mConfig.END_LOCATION) { if (mChildCurrentLocation == mConfig.END_LOCATION) {

@ -82,7 +82,9 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
file.seek(mConfig.START_LOCATION); file.seek(mConfig.START_LOCATION);
} }
upload(client, file); upload(client, file);
if (STATE.isCancel || STATE.isStop) return; if (isBreak()) {
return;
}
ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__上传完毕"); ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】线程__" + mConfig.THREAD_ID + "__上传完毕");
writeConfig(true, 1); writeConfig(true, 1);
STATE.COMPLETE_THREAD_NUM++; STATE.COMPLETE_THREAD_NUM++;
@ -131,7 +133,7 @@ class FtpThreadTask extends AbsFtpThreadTask<UploadEntity, UploadTaskEntity> {
@Override public void onFtpInputStream(FTPClient client, long totalBytesTransferred, @Override public void onFtpInputStream(FTPClient client, long totalBytesTransferred,
int bytesTransferred, long streamSize) { int bytesTransferred, long streamSize) {
if ((STATE.isCancel || STATE.isStop) && !isStoped) { if (isBreak() && !isStoped) {
try { try {
isStoped = true; isStoped = true;
client.abor(); client.abor();

@ -2,7 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arialyy.simple"> package="com.arialyy.simple">
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
@ -15,10 +14,10 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar"> android:theme="@style/AppTheme.NoActionBar">
<!--android:name=".test.TestGroupActivity"--> <!--android:name=".test.TestGroupActivity"-->
<!--android:name=".test.TestActivity"--> <!--android:name=".test.TestActivity"-->
<!--android:name=".test.AnyRunActivity"--> <!--android:name=".MainActivity"-->
<activity <activity
android:name=".MainActivity" android:name=".test.AnyRunActivity"
android:label="@string/app_name"> android:label="@string/app_name">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>

@ -68,7 +68,7 @@ public class AnyRunnModule {
.load(url) .load(url)
//.addHeader("Accept-Encoding", "gzip") //.addHeader("Accept-Encoding", "gzip")
.setRequestMode(RequestEnum.GET) .setRequestMode(RequestEnum.GET)
.setFilePath(Environment.getExternalStorageDirectory().getPath() + "/abcd.mp4") .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/abcds.exe")
//.resetState() //.resetState()
.start(); .start();
//String[] urls = new String[] { //String[] urls = new String[] {

Loading…
Cancel
Save