use of lee.study.down.model.ChunkInfo in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method startChunkDown.
protected void startChunkDown(ChunkInfo chunkInfo, int updateStatus) throws Exception {
HttpRequestInfo requestInfo = (HttpRequestInfo) httpDownInfo.getRequest();
RequestProto requestProto = requestInfo.requestProto();
LOGGER.debug("开始下载:" + chunkInfo);
Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(clientLoopGroup).handler(new HttpDownInitializer(requestProto.getSsl(), this, chunkInfo));
if (httpDownInfo.getProxyConfig() != null) {
// 代理服务器解析DNS和连接
bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
}
if (callback != null) {
callback.onChunkConnecting(httpDownInfo, chunkInfo);
}
ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
chunkInfo.setStatus(updateStatus);
// 重置最后下载时间
chunkInfo.setLastDownTime(System.currentTimeMillis());
cf.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
synchronized (chunkInfo) {
setChannel(chunkInfo, future.channel());
}
synchronized (requestInfo) {
LOGGER.debug("下载连接成功:channelId[" + future.channel().id() + "]\t" + chunkInfo);
if (httpDownInfo.getTaskInfo().isSupportRange()) {
requestInfo.headers().set(HttpHeaderNames.RANGE, "bytes=" + chunkInfo.getNowStartPosition() + "-" + chunkInfo.getEndPosition());
} else {
requestInfo.headers().remove(HttpHeaderNames.RANGE);
}
future.channel().writeAndFlush(httpDownInfo.getRequest());
}
if (requestInfo.content() != null) {
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(requestInfo.content());
future.channel().writeAndFlush(content);
}
} else {
LOGGER.debug("下载连接失败:" + chunkInfo);
chunkInfo.setStatus(HttpDownStatus.FAIL);
future.channel().close();
}
});
}
use of lee.study.down.model.ChunkInfo in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method startDown.
public void startDown() throws Exception {
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
taskInfo.buildChunkInfoList();
if (!FileUtil.exists(taskInfo.getFilePath())) {
FileUtil.createDirSmart(taskInfo.getFilePath());
}
if (!FileUtil.canWrite(taskInfo.getFilePath())) {
throw new BootstrapException("无权访问下载路径,请修改路径或开放目录写入权限");
}
// 磁盘空间不足
if (taskInfo.getTotalSize() > FileUtil.getDiskFreeSize(taskInfo.getFilePath())) {
throw new BootstrapException("磁盘空间不足,请修改路径");
}
// 有文件同名
if (new File(taskInfo.buildTaskFilePath()).exists()) {
throw new BootstrapException("文件名已存在,请修改文件名");
}
// 创建文件
try (RandomAccessFile randomAccessFile = new RandomAccessFile(taskInfo.buildTaskFilePath(), "rw")) {
randomAccessFile.setLength(taskInfo.getTotalSize());
}
// 文件下载开始回调
taskInfo.reset();
taskInfo.setStatus(HttpDownStatus.RUNNING);
taskInfo.setStartTime(System.currentTimeMillis());
for (int i = 0; i < taskInfo.getChunkInfoList().size(); i++) {
ChunkInfo chunkInfo = taskInfo.getChunkInfoList().get(i);
// 设置状态和时间
chunkInfo.setStartTime(System.currentTimeMillis());
startChunkDown(chunkInfo, HttpDownStatus.CONNECTING_NORMAL);
}
if (callback != null) {
callback.onStart(httpDownInfo);
}
afterStart();
}
use of lee.study.down.model.ChunkInfo in project proxyee-down by monkeyWie.
the class HttpDownProgressEventTask method run.
@Override
public void run() {
while (true) {
try {
for (TaskInfo taskInfo : ContentManager.DOWN.getStartTasks()) {
if (taskInfo.getStatus() != HttpDownStatus.DONE && taskInfo.getStatus() != HttpDownStatus.FAIL && taskInfo.getStatus() != HttpDownStatus.PAUSE) {
taskInfo.setLastTime(System.currentTimeMillis());
for (ChunkInfo chunkInfo : taskInfo.getChunkInfoList()) {
if (chunkInfo.getStatus() != HttpDownStatus.DONE && chunkInfo.getStatus() != HttpDownStatus.PAUSE) {
chunkInfo.setLastTime(System.currentTimeMillis());
}
}
// 保存任务进度记录
synchronized (taskInfo) {
if (taskInfo.getStatus() != HttpDownStatus.DONE) {
ContentManager.DOWN.saveTask(taskInfo.getId());
}
}
}
}
ContentManager.WS.sendMsg(ContentManager.DOWN.buildDowningWsForm());
if (HttpDownController.updateBootstrap != null && HttpDownController.updateBootstrap.getHttpDownInfo().getTaskInfo().getStatus() != HttpDownStatus.DONE) {
ContentManager.WS.sendMsg(new WsForm(WsDataType.UPDATE_PROGRESS, HttpDownController.updateBootstrap.getHttpDownInfo().getTaskInfo()));
}
TimeUnit.MILLISECONDS.sleep(1000);
} catch (Exception e) {
LOGGER.error("eventTask:", e);
}
}
}
use of lee.study.down.model.ChunkInfo in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method pauseDown.
/**
* 暂停下载
*/
public void pauseDown() throws Exception {
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
synchronized (taskInfo) {
taskInfo.setStatus(HttpDownStatus.PAUSE);
for (ChunkInfo chunkInfo : taskInfo.getChunkInfoList()) {
synchronized (chunkInfo) {
if (chunkInfo.getStatus() != HttpDownStatus.DONE) {
chunkInfo.setStatus(HttpDownStatus.PAUSE);
}
}
}
close();
}
if (callback != null) {
callback.onPause(httpDownInfo);
}
}
use of lee.study.down.model.ChunkInfo in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method continueDown.
/**
* 继续下载
*/
public void continueDown() throws Exception {
TaskInfo taskInfo = httpDownInfo.getTaskInfo();
synchronized (taskInfo) {
if (!FileUtil.exists(taskInfo.buildTaskFilePath())) {
close();
startDown();
} else {
taskInfo.setStatus(HttpDownStatus.RUNNING);
taskInfo.getChunkInfoList().forEach((chunk) -> chunk.setErrorCount(0));
long curTime = System.currentTimeMillis();
taskInfo.setPauseTime(taskInfo.getPauseTime() + (curTime - taskInfo.getLastTime()));
taskInfo.setLastTime(curTime);
afterStart();
for (ChunkInfo chunkInfo : taskInfo.getChunkInfoList()) {
synchronized (chunkInfo) {
if (chunkInfo.getStatus() == HttpDownStatus.PAUSE || chunkInfo.getStatus() == HttpDownStatus.CONNECTING_FAIL) {
chunkInfo.setPauseTime(taskInfo.getPauseTime());
chunkInfo.setLastTime(curTime);
retryChunkDown(chunkInfo, HttpDownStatus.CONNECTING_NORMAL);
}
}
}
}
}
if (callback != null) {
callback.onContinue(httpDownInfo);
}
}
Aggregations