优化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

@@ -0,0 +1,124 @@
/*
* 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.sftp;
import android.text.TextUtils;
import com.arialyy.aria.core.FtpUrlEntity;
import com.arialyy.aria.core.IdEntity;
import com.arialyy.aria.core.loader.IInfoTask;
import com.arialyy.aria.core.loader.ILoaderVisitor;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.ftp.FtpTaskOption;
import com.arialyy.aria.util.CommonUtil;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
/**
* 进行登录获取session获取文件信息
*/
public abstract class AbsSFtpInfoTask<WP extends AbsTaskWrapper> implements IInfoTask {
protected String TAG = CommonUtil.getClassName(this);
protected Callback callback;
private WP wrapper;
private FtpTaskOption option;
public AbsSFtpInfoTask(WP wp) {
this.wrapper = wp;
this.option = (FtpTaskOption) wrapper.getTaskOption();
}
protected abstract void getFileInfo(Session session)
throws JSchException, UnsupportedEncodingException, SftpException;
@Override public void run() {
try {
FtpUrlEntity entity = option.getUrlEntity();
String key = CommonUtil.getStrMd5(entity.hostName + entity.port + entity.user);
Session session = SFtpSessionManager.getInstance().getSession(key);
if (session == null) {
session = login(entity);
}
getFileInfo(session);
} catch (JSchException e) {
e.printStackTrace();
fail(false, null);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
fail(false, null);
} catch (SftpException e) {
e.printStackTrace();
fail(false, null);
}
}
private Session login(FtpUrlEntity entity) throws JSchException, UnsupportedEncodingException {
JSch jSch = new JSch();
IdEntity idEntity = entity.idEntity;
if (idEntity.prvKey != null) {
if (idEntity.pubKey == null) {
jSch.addIdentity(idEntity.prvKey,
entity.password == null ? null : entity.password.getBytes("UTF-8"));
} else {
jSch.addIdentity(idEntity.prvKey, idEntity.pubKey,
entity.password == null ? null : entity.password.getBytes("UTF-8"));
}
}
Session session;
if (TextUtils.isEmpty(entity.user)) {
session = jSch.getSession(entity.url, entity.hostName, Integer.parseInt(entity.port));
} else {
session = jSch.getSession(entity.hostName);
}
if (!TextUtils.isEmpty(entity.password)) {
session.setPassword(entity.password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);// 为Session对象设置properties
session.setTimeout(3000);// 设置超时
session.setIdentityRepository(jSch.getIdentityRepository());
session.connect();
return session;
}
protected FtpTaskOption getOption() {
return option;
}
protected WP getWrapper() {
return wrapper;
}
protected void fail(boolean needRetry, BaseException e) {
callback.onFail(getWrapper().getEntity(), e, needRetry);
}
@Override public void setCallback(Callback callback) {
this.callback = callback;
}
@Override public void accept(ILoaderVisitor visitor) {
visitor.addComponent(this);
}
}

View File

@@ -1,42 +0,0 @@
/*
* 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.sftp;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import java.util.Vector;
/**
* sftp文件信息适配器
*/
public abstract class BaseInfoThreadAdapter<WRAPPER extends AbsTaskWrapper> {
private WRAPPER mWrapper;
public BaseInfoThreadAdapter(WRAPPER taskWrapper) {
mWrapper = taskWrapper;
}
public WRAPPER getWrapper() {
return mWrapper;
}
/**
* 处理文件
*
* @return true 处理文件成功false 处理文件失败,结束任务
*/
protected abstract boolean handlerFile(Vector vector);
}

View File

@@ -1,100 +0,0 @@
///*
// * 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.sftp;
//
//import com.arialyy.aria.core.AriaConfig;
//import com.arialyy.aria.core.FtpUrlEntity;
//import com.arialyy.aria.core.common.AbsNormalEntity;
//import com.arialyy.aria.core.inf.OnFileInfoCallback;
//import com.arialyy.aria.core.upload.UploadEntity;
//import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
//import com.arialyy.aria.ftp.FtpTaskOption;
//import com.arialyy.aria.util.ALog;
//import com.arialyy.aria.util.CommonUtil;
//import com.jcraft.jsch.ChannelSftp;
//import com.jcraft.jsch.JSchException;
//import com.jcraft.jsch.SftpException;
//import java.util.Vector;
//
///**
// * https://cloud.tencent.com/developer/article/1354612
// *
// * @author lyy
// */
//public class SFtpInfoThread<ENTITY extends AbsNormalEntity, TASK_WRAPPER extends AbsTaskWrapper<ENTITY>>
// implements Runnable {
//
// private final String TAG = CommonUtil.getClassName(getClass());
// protected ENTITY mEntity;
// protected TASK_WRAPPER mTaskWrapper;
// protected FtpTaskOption mTaskOption;
// private int mConnectTimeOut;
// protected OnFileInfoCallback mCallback;
// protected long mSize = 0;
// protected String charSet = "UTF-8";
// private boolean isUpload = false;
// private SFtpUtil mSFtpUtil;
// private BaseInfoThreadAdapter mAdapter;
//
// public SFtpInfoThread(SFtpUtil ftpUtil, TASK_WRAPPER taskWrapper,
// OnFileInfoCallback callback) {
// mSFtpUtil = ftpUtil;
// mTaskWrapper = taskWrapper;
// mEntity = taskWrapper.getEntity();
// mTaskOption = (FtpTaskOption) taskWrapper.getTaskOption();
// mConnectTimeOut = AriaConfig.getInstance().getDConfig().getConnectTimeOut();
// mCallback = callback;
// if (mEntity instanceof UploadEntity) {
// isUpload = true;
// }
// }
//
// public void setAdapter(BaseInfoThreadAdapter adapter) {
// mAdapter = adapter;
// }
//
// @Override public void run() {
// if (mAdapter == null) {
// ALog.e(TAG, "adapter为空");
// return;
// }
// try {
// ChannelSftp channelSftp =
// (ChannelSftp) mSFtpUtil.getSession().openChannel(SFtpUtil.CMD_TYPE_SFTP);
// Vector files = channelSftp.ls(getUrlEntity().remotePath);
//
// if (files.isEmpty()) {
// ALog.e(TAG, String.format("路径【%s】没有文件", getUrlEntity().remotePath));
// mCallback.onFail(mEntity, null, false);
// return;
// }
//
// if (!mAdapter.handlerFile(files)) {
// ALog.e(TAG, "文件处理失败");
// mCallback.onFail(mEntity, null, false);
// return;
// }
// } catch (JSchException ex) {
// ex.printStackTrace();
// } catch (SftpException e) {
// e.printStackTrace();
// }
// }
//
// private FtpUrlEntity getUrlEntity() {
// return mTaskOption.getUrlEntity();
// }
//}

View File

@@ -0,0 +1,98 @@
/*
* 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.sftp;
import android.text.TextUtils;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import com.jcraft.jsch.Session;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* SFTP session 管理器
* 1、管理session
* 2、定时清除无效的session
*/
public class SFtpSessionManager {
private String TAG = CommonUtil.getClassName(this);
private static volatile SFtpSessionManager INSTANCE = null;
private Map<String, Session> sessionDeque = new HashMap<>();
public synchronized static SFtpSessionManager getInstance() {
if (INSTANCE == null) {
synchronized (SFtpSessionManager.class) {
INSTANCE = new SFtpSessionManager();
}
}
return INSTANCE;
}
private SFtpSessionManager() {
}
/**
* 获取session获取完成session后检查map中的所有session移除所有失效的session
*
* @param key md5(host + port + userName )
* @return 如果session不可用返回null
*/
public Session getSession(String key) {
if (TextUtils.isEmpty(key)) {
ALog.e(TAG, "获取session失败key为空");
return null;
}
Session session = sessionDeque.get(key);
if (session == null) {
ALog.w(TAG, "获取session失败key" + key);
}
cleanIdleSession();
return session;
}
/**
* 添加session
*/
public void addSession(Session session) {
if (session == null) {
ALog.e(TAG, "添加session到管理器失败session 为空");
return;
}
String key =
CommonUtil.getStrMd5(session.getHost() + session.getPort() + session.getUserName());
sessionDeque.put(key, session);
}
/**
* 移除所有闲置的session闲置条件
* 1、session 为空
* 2、session未连接
*/
private void cleanIdleSession() {
if (sessionDeque.size() > 0) {
Iterator<Map.Entry<String, Session>> i = sessionDeque.entrySet().iterator();
while (i.hasNext()) {
Map.Entry<String, Session> entry = i.next();
Session session = entry.getValue();
if (session == null || !session.isConnected()) {
i.remove();
}
}
}
}
}

View File

@@ -13,19 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.sftp.download;
package com.arialyy.aria.sftp;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.sftp.BaseInfoThreadAdapter;
import java.util.Vector;
import com.arialyy.aria.core.common.SubThreadConfig;
final class DSFtpInfoThreadAdapter extends BaseInfoThreadAdapter<DTaskWrapper> {
DSFtpInfoThreadAdapter(DTaskWrapper taskWrapper) {
super(taskWrapper);
}
@Override protected boolean handlerFile(Vector vector) {
return false;
}
public class SFtpSubThreadCOnfig extends SubThreadConfig {
}

View File

@@ -0,0 +1,49 @@
/*
* 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.sftp.download;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.sftp.AbsSFtpInfoTask;
import com.arialyy.aria.util.CommonUtil;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import java.io.UnsupportedEncodingException;
/**
* 进行登录获取session获取文件信息
*/
final class SFtpDInfoTask extends AbsSFtpInfoTask<DTaskWrapper> {
SFtpDInfoTask(DTaskWrapper wrapper) {
super(wrapper);
}
@Override protected void getFileInfo(Session session) throws JSchException,
UnsupportedEncodingException, SftpException {
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
SftpATTRS attr = channel.stat(
CommonUtil.convertFtpChar(getOption().getCharSet(), getWrapper().getEntity().getUrl()));
getWrapper().getEntity().setFileSize(attr.getSize());
CompleteInfo info = new CompleteInfo();
info.code = 200;
info.obj = channel;
callback.onSucceed(getWrapper().getKey(), info);
}
}

View File

@@ -0,0 +1,163 @@
/*
* 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.sftp.download;
import android.os.Handler;
import android.os.Looper;
import com.arialyy.aria.core.common.AbsEntity;
import com.arialyy.aria.core.common.AbsNormalEntity;
import com.arialyy.aria.core.common.CompleteInfo;
import com.arialyy.aria.core.event.EventMsgUtil;
import com.arialyy.aria.core.inf.IThreadStateManager;
import com.arialyy.aria.core.listener.IDLoadListener;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.IInfoTask;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.loader.IThreadTaskBuilder;
import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.manager.ThreadTaskManager;
import com.arialyy.aria.core.task.IThreadTask;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.util.FileUtil;
import com.jcraft.jsch.ChannelSftp;
import java.io.File;
final class SFtpDLoader extends AbsNormalLoader {
private int startThreadNum; //启动的线程数
private boolean isComplete = false;
private Looper looper;
private ChannelSftp channelSftp;
SFtpDLoader(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
mTempFile = new File(getEntity().getFilePath());
EventMsgUtil.getDefault().register(this);
setUpdateInterval(wrapper.getConfig().getUpdateInterval());
}
private AbsNormalEntity getEntity() {
return (AbsNormalEntity) mTaskWrapper.getEntity();
}
@Override public long getFileSize() {
return getEntity().getFileSize();
}
/**
* 设置最大下载/上传速度AbsFtpInfoThread
*
* @param maxSpeed 单位为kb
*/
protected void setMaxSpeed(int maxSpeed) {
for (IThreadTask threadTask : getTaskList()) {
if (threadTask != null && startThreadNum > 0) {
threadTask.setMaxSpeed(maxSpeed / startThreadNum);
}
}
}
@Override public void onDestroy() {
super.onDestroy();
EventMsgUtil.getDefault().unRegister(this);
}
/**
* 启动单线程任务
*/
@Override
public void handleTask(Looper looper) {
if (isBreak() || isComplete) {
return;
}
this.looper = looper;
mInfoTask.run();
}
private void startThreadTask() {
if (getListener() instanceof IDLoadListener) {
((IDLoadListener) getListener()).onPostPre(getEntity().getFileSize());
}
File file = new File(getEntity().getFilePath());
if (file.getParentFile() != null && !file.getParentFile().exists()) {
FileUtil.createDir(file.getPath());
}
// 处理记录、初始化状态管理器
mRecord = mRecordHandler.getRecord(getFileSize());
mStateManager.setLooper(mRecord, looper);
// 创建线程任务
SFtpDTTBuilderAdapter ttBuild =
(SFtpDTTBuilderAdapter) ((NormalTTBuilder) mTTBuilder).getAdapter();
ttBuild.setChannel(channelSftp);
getTaskList().addAll(mTTBuilder.buildThreadTask(mRecord,
new Handler(looper, mStateManager.getHandlerCallback())));
startThreadNum = mTTBuilder.getCreatedThreadNum();
mStateManager.updateCurrentProgress(getEntity().getCurrentProgress());
if (mStateManager.getCurrentProgress() > 0) {
getListener().onResume(mStateManager.getCurrentProgress());
} else {
getListener().onStart(mStateManager.getCurrentProgress());
}
// 启动线程任务
for (IThreadTask threadTask : getTaskList()) {
ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), threadTask);
}
// 启动定时器
startTimer();
}
@Override public long getCurrentProgress() {
return isRunning() ? mStateManager.getCurrentProgress() : getEntity().getCurrentProgress();
}
@Override public void addComponent(IRecordHandler recordHandler) {
mRecordHandler = recordHandler;
if (recordHandler.checkTaskCompleted()) {
mRecord.deleteData();
isComplete = true;
getListener().onComplete();
}
}
@Override public void addComponent(IInfoTask infoTask) {
mInfoTask = infoTask;
infoTask.setCallback(new IInfoTask.Callback() {
@Override public void onSucceed(String key, CompleteInfo info) {
channelSftp = (ChannelSftp) info.obj;
startThreadTask();
}
@Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) {
getListener().onFail(needRetry, e);
}
});
}
@Override public void addComponent(IThreadStateManager threadState) {
mStateManager = threadState;
}
@Override public void addComponent(IThreadTaskBuilder builder) {
mTTBuilder = builder;
}
}

View File

@@ -1,40 +0,0 @@
///*
// * 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.sftp.download;
//
//import com.arialyy.aria.core.common.SubThreadConfig;
//import com.arialyy.aria.core.task.IThreadTask;
//import com.arialyy.aria.core.task.ThreadTask;
//import com.arialyy.aria.core.wrapper.ITaskWrapper;
//import com.arialyy.aria.ftp.download.FtpDTTBuilder;
//
///**
// * sftp下载适配器
// *
// * @author lyy
// */
//final class SFtpDLoaderAdapter extends FtpDTTBuilder {
// SFtpDLoaderAdapter(ITaskWrapper wrapper) {
// super(wrapper);
// }
//
// @Override public IThreadTask createThreadTask(SubThreadConfig config) {
// ThreadTask threadTask = new ThreadTask(config);
// SFtpDThreadTaskAdapter adapter = new SFtpDThreadTaskAdapter(config);
// threadTask.setAdapter(adapter);
// return threadTask;
// }
//}

View File

@@ -1,75 +1,54 @@
///*
// * 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.sftp.download;
//
//import com.arialyy.aria.core.common.AbsEntity;
//import com.arialyy.aria.core.common.CompleteInfo;
//import com.arialyy.aria.core.download.DTaskWrapper;
//import com.arialyy.aria.core.inf.OnFileInfoCallback;
//import com.arialyy.aria.core.listener.IEventListener;
//import com.arialyy.aria.core.loader.AbsNormalLoader;
//import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
//import com.arialyy.aria.core.loader.NormalLoader;
//import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
//import com.arialyy.aria.exception.BaseException;
//import com.arialyy.aria.ftp.FtpTaskOption;
//import com.arialyy.aria.sftp.SFtpInfoThread;
//import com.arialyy.aria.sftp.SFtpUtil;
//
///**
// * sftp下载工具
// *
// * @author lyy
// */
//public class SFtpDLoaderUtil extends AbsNormalLoaderUtil {
//
// private SFtpUtil mSftpUtil;
//
// protected SFtpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
// super(wrapper, listener);
// wrapper.generateTaskOption(FtpTaskOption.class);
// FtpTaskOption option = (FtpTaskOption) wrapper.getTaskOption();
// mSftpUtil = new SFtpUtil.Builder()
// .setIp(option.getUrlEntity().hostName)
// .setPort(Integer.parseInt(option.getUrlEntity().port))
// .setUserName(option.getUrlEntity().user)
// .setPassword(option.getUrlEntity().password)
// .build();
// }
//
// @Override protected AbsNormalLoader createLoader() {
// NormalLoader loader = new NormalLoader(getListener(), getTaskWrapper());
// loader.setAdapter(new SFtpDLoaderAdapter(getTaskWrapper()));
// return loader;
// }
//
// @Override protected Runnable createInfoThread() {
// DSFtpInfoThreadAdapter adapter = new DSFtpInfoThreadAdapter((DTaskWrapper) getTaskWrapper());
// SFtpInfoThread infoThread = new SFtpInfoThread<>(mSftpUtil, (DTaskWrapper) getTaskWrapper(),
// new OnFileInfoCallback() {
// @Override public void onComplete(String key, CompleteInfo info) {
//
// }
//
// @Override public void onFail(AbsEntity entity, BaseException e, boolean needRetry) {
//
// }
// });
// infoThread.setAdapter(adapter);
//
// return infoThread;
// }
//}
/*
* 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.sftp.download;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.FtpTaskOption;
import com.arialyy.aria.ftp.download.FtpDRecordHandler;
/**
* sftp下载工具
*
* @author lyy
*/
public class SFtpDLoaderUtil extends AbsNormalLoaderUtil {
protected SFtpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
wrapper.generateTaskOption(FtpTaskOption.class);
}
@Override public AbsNormalLoader getLoader() {
return mLoader == null ? new SFtpDLoader(getTaskWrapper(), getListener()) : mLoader;
}
@Override public LoaderStructure BuildLoaderStructure() {
LoaderStructure structure = new LoaderStructure();
structure.addComponent(new FtpDRecordHandler((DTaskWrapper) getTaskWrapper()))
.addComponent(new NormalThreadStateManager(getListener()))
.addComponent(new SFtpDInfoTask((DTaskWrapper) getTaskWrapper()))
.addComponent(new NormalTTBuilder(getTaskWrapper(), new SFtpDTTBuilderAdapter()git));
structure.accept(getLoader());
return structure;
}
}

View File

@@ -0,0 +1,72 @@
/*
* 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.sftp.download;
import android.os.Handler;
import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.ThreadRecord;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.FileUtil;
import com.jcraft.jsch.ChannelSftp;
import java.io.File;
class SFtpDTTBuilderAdapter extends AbsNormalTTBuilderAdapter {
private ChannelSftp channel;
SFtpDTTBuilderAdapter() {
}
void setChannel(ChannelSftp channel) {
this.channel = channel;
}
@Override public IThreadTaskAdapter getAdapter(SubThreadConfig config) {
return new SFtpDThreadTaskAdapter(config);
}
@Override
protected SubThreadConfig getSubThreadConfig(Handler stateHandler, ThreadRecord threadRecord,
boolean isBlock, int startNum) {
SubThreadConfig config =
super.getSubThreadConfig(stateHandler, threadRecord, isBlock, startNum);
config.obj = channel;
return config;
}
@Override public boolean handleNewTask(TaskRecord record, int totalThreadNum) {
if (!record.isBlock) {
if (getTempFile().exists()) {
FileUtil.deleteFile(getTempFile());
}
//CommonUtil.createFile(mTempFile.getPath());
} else {
for (int i = 0; i < totalThreadNum; i++) {
File blockFile =
new File(String.format(IRecordHandler.SUB_PATH, getTempFile().getPath(), i));
if (blockFile.exists()) {
ALog.d(TAG, String.format("分块【%s】已经存在将删除该分块", i));
FileUtil.deleteFile(blockFile);
}
}
}
return true;
}
}

View File

@@ -23,7 +23,7 @@ import com.arialyy.aria.core.task.AbsThreadTaskAdapter;
*
* @author lyy
*/
public class SFtpDThreadTaskAdapter extends AbsThreadTaskAdapter {
final class SFtpDThreadTaskAdapter extends AbsThreadTaskAdapter {
SFtpDThreadTaskAdapter(SubThreadConfig config) {
super(config);