forked from proxyee-down-org/proxyee-down
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
包结构与配置文件调整,自动重试功能,下载完FileChannel没关闭问题修复。
- Loading branch information
Showing
27 changed files
with
477 additions
and
402 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package lee.study.down; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import lee.study.down.dispatch.HttpDownErrorCheckTask; | ||
import lee.study.down.dispatch.HttpDownProgressPushTask; | ||
import lee.study.down.intercept.BdyBatchDownIntercept; | ||
import lee.study.down.intercept.BdyIntercept; | ||
import lee.study.down.intercept.HttpDownIntercept; | ||
import lee.study.down.intercept.HttpDownSniffIntercept; | ||
import lee.study.down.model.HttpDownInfo; | ||
import lee.study.down.model.TaskInfo; | ||
import lee.study.proxyee.intercept.CertDownIntercept; | ||
import lee.study.proxyee.intercept.HttpProxyInterceptInitializer; | ||
import lee.study.proxyee.intercept.HttpProxyInterceptPipeline; | ||
import lee.study.proxyee.server.HttpProxyServer; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
|
||
@SpringBootApplication | ||
public class HttpDownServer implements InitializingBean { | ||
|
||
public static final NioEventLoopGroup LOOP_GROUP = new NioEventLoopGroup(1); | ||
public static final Bootstrap DOWN_BOOT = new Bootstrap().group(LOOP_GROUP) | ||
.channel(NioSocketChannel.class); | ||
|
||
public static final Map<String, HttpDownInfo> DOWN_CONTENT = new ConcurrentHashMap<>(); | ||
public static final Map<String, WebSocketSession> WS_CONTENT = new ConcurrentHashMap<>(); | ||
|
||
public static int VIEW_SERVER_PORT; | ||
|
||
@Value("${view.server.port}") | ||
private int viewServerPort; | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
VIEW_SERVER_PORT = viewServerPort; | ||
} | ||
|
||
public static void sendMsg(String type, TaskInfo taskInfo) { | ||
try { | ||
for (Entry<String, WebSocketSession> entry : HttpDownServer.WS_CONTENT.entrySet()) { | ||
WebSocketSession session = entry.getValue(); | ||
if (session.isOpen()) { | ||
Map<String, Object> msg = new HashMap<>(); | ||
msg.put("type", type); | ||
msg.put("taskInfo", taskInfo); | ||
TextMessage message = new TextMessage(JSON.toJSONString(msg)); | ||
synchronized (session){ | ||
session.sendMessage(message); | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void start(int port) { | ||
SpringApplication.run(HttpDownServer.class); | ||
new HttpDownProgressPushTask().start(); | ||
new HttpDownErrorCheckTask().start(); | ||
//监听http下载请求 | ||
new HttpProxyServer().proxyInterceptInitializer(new HttpProxyInterceptInitializer() { | ||
@Override | ||
public void init(HttpProxyInterceptPipeline pipeline) { | ||
pipeline.addLast(new CertDownIntercept()); | ||
pipeline.addLast(new BdyIntercept()); | ||
pipeline.addLast(new HttpDownSniffIntercept()); | ||
pipeline.addLast(new BdyBatchDownIntercept()); | ||
pipeline.addLast(new HttpDownIntercept()); | ||
} | ||
}).start(port); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
start(9999); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...java/lee/study/down/HttpDownCallback.java → ...study/down/dispatch/HttpDownCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/lee/study/down/dispatch/HttpDownErrorCheckTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package lee.study.down.dispatch; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.TimeUnit; | ||
import lee.study.down.HttpDownServer; | ||
import lee.study.down.model.ChunkInfo; | ||
import lee.study.down.model.HttpDownInfo; | ||
import lee.study.down.model.TaskInfo; | ||
import lee.study.down.util.HttpDownUtil; | ||
|
||
/** | ||
* 1分钟内没有下载判断为失败,进行重试 | ||
*/ | ||
public class HttpDownErrorCheckTask extends Thread { | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
Map<String, Long> flagMap = new HashMap<>(); | ||
while (true) { | ||
if (HttpDownServer.DOWN_CONTENT != null && HttpDownServer.DOWN_CONTENT.size() > 0) { | ||
for (Entry<String, HttpDownInfo> entry : HttpDownServer.DOWN_CONTENT.entrySet()) { | ||
TaskInfo taskInfo = entry.getValue().getTaskInfo(); | ||
if (taskInfo.getStatus() == 1) { | ||
for (ChunkInfo chunkInfo : taskInfo.getChunkInfoList()) { | ||
if (chunkInfo.getStatus() == 1) { | ||
String key = taskInfo.getId() + "_" + chunkInfo.getIndex(); | ||
Long downSize = flagMap.get(key); | ||
//下载失败 | ||
if (downSize != null && downSize == chunkInfo.getDownSize()) { | ||
System.out.println("60秒内无响应重试:"+chunkInfo.getIndex()+"\t"+chunkInfo.getChannel().id()+"\t"+chunkInfo.getDownSize()); | ||
chunkInfo.setStatus(3); | ||
HttpDownUtil.retryDown(taskInfo, chunkInfo); | ||
} else { | ||
flagMap.put(key, chunkInfo.getDownSize()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
TimeUnit.MILLISECONDS.sleep(60000); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/lee/study/down/dispatch/HttpDownProgressPushTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package lee.study.down.dispatch; | ||
|
||
import java.util.Map.Entry; | ||
import java.util.concurrent.TimeUnit; | ||
import lee.study.down.HttpDownServer; | ||
import lee.study.down.model.ChunkInfo; | ||
import lee.study.down.model.HttpDownInfo; | ||
import lee.study.down.model.TaskInfo; | ||
|
||
public class HttpDownProgressPushTask extends Thread{ | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
while (true) { | ||
if (HttpDownServer.DOWN_CONTENT != null && HttpDownServer.DOWN_CONTENT.size() > 0) { | ||
for (Entry<String, HttpDownInfo> entry : HttpDownServer.DOWN_CONTENT.entrySet()) { | ||
TaskInfo taskInfo = entry.getValue().getTaskInfo(); | ||
if (taskInfo.getStatus() == 1) { | ||
taskInfo.setLastTime(System.currentTimeMillis()); | ||
for (ChunkInfo chunkInfo : taskInfo.getChunkInfoList()) { | ||
if (chunkInfo.getStatus() == 1) { | ||
chunkInfo.setLastTime(System.currentTimeMillis()); | ||
} | ||
} | ||
} | ||
HttpDownServer.sendMsg("progress", taskInfo); | ||
} | ||
TimeUnit.MILLISECONDS.sleep(200); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/lee/study/form/DownForm.java → ...in/java/lee/study/down/form/DownForm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package lee.study.form; | ||
package lee.study.down.form; | ||
|
||
import lombok.Data; | ||
|
||
|
Oops, something went wrong.