优化ttbuilder

This commit is contained in:
laoyuyu
2020-01-11 13:18:13 +08:00
parent 36c226702d
commit 6f53235807
36 changed files with 931 additions and 456 deletions

View File

@@ -78,16 +78,6 @@ public class FtpUrlEntity implements Cloneable {
*/
public InetAddress validAddr;
/**
* 证书路径
*/
public String storePath;
/**
* 证书密码
*/
public String storePass;
/**
* 连接协议
* {@link ProtocolType}
@@ -99,10 +89,7 @@ public class FtpUrlEntity implements Cloneable {
*/
public boolean isImplicit = true;
/**
* 私钥别名
*/
public String keyAlias;
public IdEntity idEntity;
@Override public FtpUrlEntity clone() {
FtpUrlEntity entity = null;

View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core;
/**
* 证书信息
*/
public class IdEntity {
/**
* 私钥证书内容(非路径)
*/
public String prvKey;
/**
* 私钥证书密码
*/
public String prvPass;
/**
* 公钥证书内容(非路径)
*/
public String pubKey;
/**
* 私钥证书路径
*/
public String storePath;
/**
* 私钥别名
*/
public String keyAlias;
}

View File

@@ -48,6 +48,8 @@ public class SubThreadConfig {
public int threadType = TYPE_HTTP;
// 进度更新间隔,单位:毫秒
public long updateInterval = 1000;
// 扩展数据
public Object obj;
/**
* 转换线程任务类型

View File

@@ -0,0 +1,90 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.loader;
import android.os.Handler;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.ThreadRecord;
import com.arialyy.aria.core.common.AbsNormalEntity;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
public abstract class AbsNormalTTBuilderAdapter {
protected String TAG = CommonUtil.getClassName(this);
protected AbsTaskWrapper wrapper;
private File tempFile;
public AbsNormalTTBuilderAdapter() {
}
protected void setWrapper(AbsTaskWrapper wrapper) {
this.wrapper = wrapper;
tempFile = new File(((AbsNormalEntity) wrapper.getEntity()).getFilePath());
}
/**
* 创建线程任务适配器
*/
public abstract IThreadTaskAdapter getAdapter(SubThreadConfig config);
/**
* 处理新任务
*
* @param record 任务记录
* @param totalThreadNum 任务的线程总数
* @return {@code true}创建新任务成功
*/
public abstract boolean handleNewTask(TaskRecord record, int totalThreadNum);
/**
* SubThreadConfig 模版,如果不使用该方法创建配置,则默认使用{@link #createNormalSubThreadConfig(Handler, ThreadRecord,
* boolean, int)}创建配置
*/
protected SubThreadConfig getSubThreadConfig(Handler stateHandler, ThreadRecord threadRecord,
boolean isBlock, int startNum) {
return createNormalSubThreadConfig(stateHandler, threadRecord, isBlock, startNum);
}
private SubThreadConfig createNormalSubThreadConfig(Handler stateHandler,
ThreadRecord threadRecord,
boolean isBlock, int startNum) {
SubThreadConfig config = new SubThreadConfig();
config.url = getEntity().isRedirect() ? getEntity().getRedirectUrl() : getEntity().getUrl();
config.tempFile =
isBlock ? new File(
String.format(IRecordHandler.SUB_PATH, tempFile.getPath(), threadRecord.threadId))
: tempFile;
config.isBlock = isBlock;
config.startThreadNum = startNum;
config.taskWrapper = wrapper;
config.record = threadRecord;
config.stateHandler = stateHandler;
config.threadType = SubThreadConfig.getThreadType(wrapper.getRequestType());
config.updateInterval = SubThreadConfig.getUpdateInterval(wrapper.getRequestType());
return config;
}
protected AbsNormalEntity getEntity() {
return (AbsNormalEntity) wrapper.getEntity();
}
protected File getTempFile() {
return tempFile;
}
}

View File

@@ -10,61 +10,46 @@ import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.inf.IThreadStateManager;
import com.arialyy.aria.core.task.IThreadTask;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.task.ThreadTask;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public abstract class AbsNormalTTBuilder implements IThreadTaskBuilder {
public final class NormalTTBuilder implements IThreadTaskBuilder {
protected String TAG = CommonUtil.getClassName(this);
private Handler mStateHandler;
private AbsTaskWrapper mWrapper;
private TaskRecord mRecord;
private int mTotalThreadNum;
private File mTempFile;
private int mStartThreadNum;
private AbsNormalTTBuilderAdapter mAdapter;
public AbsNormalTTBuilder(AbsTaskWrapper wrapper) {
public NormalTTBuilder(AbsTaskWrapper wrapper, AbsNormalTTBuilderAdapter adapter) {
if (wrapper instanceof DGTaskWrapper) {
throw new AssertionError("NormalTTBuilder 不适用于组合任务");
}
mWrapper = wrapper;
mTempFile = new File(((AbsNormalEntity) wrapper.getEntity()).getFilePath());
}
protected File getTempFile() {
return mTempFile;
mAdapter = adapter;
mAdapter.setWrapper(wrapper);
}
protected AbsNormalEntity getEntity() {
return (AbsNormalEntity) mWrapper.getEntity();
}
/**
* 创建线程任务适配器
*/
public abstract IThreadTaskAdapter getAdapter(SubThreadConfig config);
/**
* 处理新任务
*
* @param record 任务记录
* @param totalThreadNum 任务的线程总数
* @return {@code true}创建新任务成功
*/
public abstract boolean handleNewTask(TaskRecord record, int totalThreadNum);
public AbsNormalTTBuilderAdapter getAdapter() {
return mAdapter;
}
/**
* 创建线程任务
*/
private IThreadTask createThreadTask(SubThreadConfig config) {
ThreadTask task = new ThreadTask(config);
task.setAdapter(getAdapter(config));
task.setAdapter(mAdapter.getAdapter(config));
return task;
}
@@ -75,20 +60,8 @@ public abstract class AbsNormalTTBuilder implements IThreadTaskBuilder {
* @param startNum 启动的线程数
*/
private IThreadTask createSingThreadTask(ThreadRecord record, int startNum) {
SubThreadConfig config = new SubThreadConfig();
config.url = getEntity().isRedirect() ? getEntity().getRedirectUrl() : getEntity().getUrl();
config.tempFile =
mRecord.isBlock ? new File(
String.format(IRecordHandler.SUB_PATH, mTempFile.getPath(), record.threadId))
: mTempFile;
config.isBlock = mRecord.isBlock;
config.startThreadNum = startNum;
config.taskWrapper = mWrapper;
config.record = record;
config.stateHandler = mStateHandler;
config.threadType = SubThreadConfig.getThreadType(mWrapper.getRequestType());
config.updateInterval = SubThreadConfig.getUpdateInterval(mWrapper.getRequestType());
return createThreadTask(config);
return createThreadTask(
mAdapter.getSubThreadConfig(mStateHandler, record, mRecord.isBlock, startNum));
}
/**
@@ -117,7 +90,7 @@ public abstract class AbsNormalTTBuilder implements IThreadTaskBuilder {
List<IThreadTask> threadTasks = new ArrayList<>(mTotalThreadNum);
mRecord.fileLength = fileLength;
if (mWrapper.isNewTask() && !handleNewTask(mRecord, mTotalThreadNum)) {
if (mWrapper.isNewTask() && !mAdapter.handleNewTask(mRecord, mTotalThreadNum)) {
ALog.e(TAG, "初始化线程任务失败");
return null;
}
@@ -138,7 +111,7 @@ public abstract class AbsNormalTTBuilder implements IThreadTaskBuilder {
Message msg = mStateHandler.obtainMessage();
msg.what = IThreadStateManager.STATE_COMPLETE;
Bundle b = msg.getData();
if (b == null){
if (b == null) {
b = new Bundle();
}
b.putString(IThreadStateManager.DATA_THREAD_NAME,

View File

@@ -0,0 +1,28 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.exception;
public class AriaLoginException extends BaseException{
private static final String ARIA_NET_EXCEPTION = "Aria Login Exception:";
public AriaLoginException(String tag, String message) {
super(tag, String.format("%s%s", ARIA_NET_EXCEPTION, message));
}
public AriaLoginException(String tag, String message, Exception e){
super(tag, message, e);
}
}

View File

@@ -179,6 +179,7 @@ public class RecordUtil {
} else if (record.isBlock) { // 删除分块文件
removeBlockFile(record);
}
removeTargetFile(targetFile);
} else if (removeFile) { // 处理任务完成情况
if (recordIsM3U8(record.taskType)) {
removeTsCache(targetFile, record.bandWidth);
@@ -249,6 +250,7 @@ public class RecordUtil {
} else if (record.isBlock) { // 删除分块文件
removeBlockFile(record);
}
removeTargetFile(targetFile);
} else if (removeFile) { // 处理任务完成情况
if (recordIsM3U8(record.taskType)) {
removeTsCache(targetFile, record.bandWidth);