Skip to content

Commit

Permalink
配置默认分段数,下载速度显示优化
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Jan 16, 2018
1 parent 3d19228 commit 0eea7f3
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 20 deletions.
12 changes: 12 additions & 0 deletions common/src/main/java/lee/study/down/model/TaskInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ public void reset() {
chunkInfo.setPauseTime(0);
});
}

public void refresh() {
lastTime = System.currentTimeMillis();
chunkInfoList.forEach((chunkInfo) -> {
chunkInfo.setLastTime(lastTime);
});
}

public void refresh(ChunkInfo chunkInfo) {
lastTime = System.currentTimeMillis();
chunkInfo.setLastTime(lastTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
synchronized (chunkInfo) {
Channel nowChannel = (Channel) bootstrap
.getAttr(chunkInfo, HttpDownBootstrap.ATTR_CHANNEL);
LargeMappedByteBuffer nowMapBuffer = (LargeMappedByteBuffer) bootstrap
.getAttr(chunkInfo, HttpDownBootstrap.ATTR_MAP_BUFFER);
if (chunkInfo.getStatus() == HttpDownStatus.RUNNING
&& nowChannel == ctx.channel()) {
nowMapBuffer.put(byteBuf.nioBuffer());
mappedBuffer.put(byteBuf.nioBuffer());
//文件已下载大小
chunkInfo.setDownSize(chunkInfo.getDownSize() + readableBytes);
taskInfo.setDownSize(taskInfo.getDownSize() + readableBytes);
Expand All @@ -92,6 +90,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
chunkInfo.setLastTime(System.currentTimeMillis());
LOGGER.debug("分段下载完成:" + chunkInfo.getIndex() + "\t" + chunkInfo.getDownSize() + "\t"
+ taskInfo.getStatus());
taskInfo.refresh(chunkInfo);
callback.onChunkDone(bootstrap.getHttpDownInfo(), chunkInfo);
synchronized (taskInfo) {
if (taskInfo.getStatus() == HttpDownStatus.RUNNING
Expand All @@ -106,6 +105,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
//文件下载完成回调
taskInfo.setStatus(HttpDownStatus.DONE);
LOGGER.debug("下载完成:" + chunkInfo.getIndex() + "\t" + chunkInfo.getDownSize());
taskInfo.refresh();
callback.onDone(bootstrap.getHttpDownInfo());
}
}
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/lee/study/down/util/HttpDownUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public void channelRead(ChannelHandlerContext ctx0, Object msg0)
//206表示支持断点下载
if (httpResponse.status().equals(HttpResponseStatus.PARTIAL_CONTENT)) {
taskInfo.setSupportRange(true);
taskInfo.setConnections(16);
}
ctx0.channel().close();
cdl.countDown();
Expand Down
6 changes: 4 additions & 2 deletions ui/src/main/java/lee/study/down/content/ConfigContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ public void init() {
}
if (configContent == null) {
configContent = new ConfigInfo();
//默认30秒无响应重试
configContent.setTimeout(30);
//默认代理端口
configContent.setProxyPort(9999);
//默认分段数
configContent.setConnections(16);
//默认30秒无响应重试
configContent.setTimeout(30);
save();
}
}
Expand Down
3 changes: 2 additions & 1 deletion ui/src/main/java/lee/study/down/model/ConfigInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import lombok.Data;

@Data
public class ConfigInfo implements Serializable{
public class ConfigInfo implements Serializable {

private static final long serialVersionUID = 4780168673614933999L;

private boolean guideFlag; //是否需要新手引导教程
private int proxyPort; //代理端口号
private int connections; //默认分段数
private int timeout; //超时重试时间
private boolean secProxyEnable; //二级代理开关
private ProxyConfig secProxyConfig; //二级代理设置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public ResultInfo getTask(@RequestParam String id) throws Exception {
if (taskInfo == null) {
resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("任务不存在");
} else {
if (taskInfo.isSupportRange()) {
taskInfo.setConnections(ContentManager.CONFIG.get().getConnections());
}
taskInfo.setFilePath(ContentManager.CONFIG.get().getLastPath());
resultInfo.setData(taskInfo);
}
Expand Down
1 change: 1 addition & 0 deletions ui/src/main/java/lee/study/down/mvc/form/ConfigForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ConfigForm {
private boolean guideFlag; //是否需要新手引导教程
private int proxyPort; //代理端口号
private int timeout; //超时重试时间
private int connections; //默认分段数
private boolean secProxyEnable; //二级代理开关
private ProxyConfigForm secProxyConfig; //二级代理设置
private String lastPath; //最后保存文件的路径
Expand Down
4 changes: 2 additions & 2 deletions ui/src/main/java/lee/study/down/update/GithubUpdateCheck.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package lee.study.down.update;

import lee.study.down.model.HttpDownInfo;
import lee.study.down.model.UpdateInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class GithubUpdateCheck implements UpdateCheck {

@Override
public HttpDownInfo check() throws Exception {
public UpdateInfo check() throws Exception {
Document document = Jsoup.connect("https://github.com/monkeyWie/proxyee-down/releases").get();
Elements elements = document.select(".release-title.text-normal");
elements.forEach((e) -> System.out.println(e.text()));
Expand Down
4 changes: 2 additions & 2 deletions ui/src/main/java/lee/study/down/update/UpdateCheck.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lee.study.down.update;

import lee.study.down.model.HttpDownInfo;
import lee.study.down.model.UpdateInfo;

public interface UpdateCheck {
HttpDownInfo check() throws Exception;
UpdateInfo check() throws Exception;
}
25 changes: 22 additions & 3 deletions ui/view/src/components/ConfigPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
<i class="el-icon-question"></i>
</el-tooltip>
</el-form-item>
<el-form-item label="分段数" prop="connections">
<el-slider
v-model="form.connections"
:min="2"
:max="128"
:step="2"
show-input>
</el-slider>
<el-tooltip class="item" content="创建新任务时默认的分段数" placement="right">
<i class="el-icon-question"></i>
</el-tooltip>
</el-form-item>
<el-form-item label="超时时间" prop="timeout">
<el-input v-model.number="form.timeout" class="num-input" placeholder=""></el-input>
<el-tooltip class="item" content="在该时间段内下载未响应则重新发起连接" placement="right">
Expand Down Expand Up @@ -112,9 +124,6 @@
this.$refs['form'].validate((valid) => {
if (valid) {
this.load = true;
if (!this.form.secProxyEnable) {
}
this.$http.post('api/setConfigInfo', this.form)
.then((response) => {
let result = response.data;
Expand Down Expand Up @@ -152,4 +161,14 @@
.num-input {
width: 15%;
}
.el-slider {
display: inline-block;
padding-left: 5px;
width: 70%;
}
.item {
padding-left: 5px;
}
</style>
35 changes: 29 additions & 6 deletions ui/view/src/components/TaskList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@
if (task.status == 7 || task.status == 5) {
return this.speedAvg(task);
} else {
return this.speedInterval(task);
let speed = this.speedInterval(task);
if (task.speedCount > 5 || speed > 0) {
return speed;
} else {
return this.speedAvg(task);
}
}
},
speedAvg(task) {
Expand All @@ -133,7 +138,7 @@
if (task.status == 5) {
return '暂停中';
}
let speed = this.speedInterval(task);
let speed = this.speed(task);
if (speed) {
return Util.timeFmt((task.totalSize - task.downSize) / speed);
} else {
Expand Down Expand Up @@ -208,7 +213,8 @@
this.$message(result.msg);
}
});
}).catch(()=>{});
}).catch(() => {
});
},
},
created() {
Expand All @@ -218,17 +224,34 @@
}
let msg = eval('(' + e.data + ')');
if (msg) {
/*this.tasks = msg.sort((task1, task2) => {
return task1.startTime - task2.startTime;
});*/
this.tasks = msg.map((task1) => {
this.tasks.forEach((task2) => {
if (task2.id == task1.id) {
task1.intervalTime = task1.lastTime - task2.lastTime;
task1.intervalDownSize = task1.downSize - task2.downSize;
task1.speedCount = task2.speedCount;
if (task1.intervalDownSize == 0) {
if (!task1.speedCount) {
task1.speedCount = 1;
} else {
task1.speedCount++;
}
} else {
task1.speedCount = 1;
}
task1.chunkInfoList.forEach((chunk, index) => {
chunk.intervalTime = chunk.lastTime - task2.chunkInfoList[index].lastTime;
chunk.intervalDownSize = chunk.downSize - task2.chunkInfoList[index].downSize;
chunk.speedCount = task2.chunkInfoList[index].speedCount;
if (chunk.intervalDownSize == 0) {
if (!chunk.speedCount) {
chunk.speedCount = 1;
} else {
chunk.speedCount++;
}
} else {
chunk.speedCount = 1;
}
});
}
return false;
Expand Down

0 comments on commit 0eea7f3

Please sign in to comment.