Search in sources :

Example 1 with ChunkInfo

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();
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) RandomAccessFile(java.io.RandomAccessFile) HttpDownStatus(lee.study.down.constant.HttpDownStatus) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) RequestProto(lee.study.proxyee.util.ProtoUtil.RequestProto) ByteBuffer(java.nio.ByteBuffer) FileUtil(lee.study.down.util.FileUtil) NoopAddressResolverGroup(io.netty.resolver.NoopAddressResolverGroup) TaskInfo(lee.study.down.model.TaskInfo) HttpDownInitializer(lee.study.down.handle.HttpDownInitializer) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) ChunkInfo(lee.study.down.model.ChunkInfo) HttpContent(io.netty.handler.codec.http.HttpContent) Logger(org.slf4j.Logger) SslContext(io.netty.handler.ssl.SslContext) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) IOException(java.io.IOException) HttpDownCallback(lee.study.down.dispatch.HttpDownCallback) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) HttpDownInfo(lee.study.down.model.HttpDownInfo) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Closeable(java.io.Closeable) Data(lombok.Data) BootstrapException(lee.study.down.exception.BootstrapException) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) AllArgsConstructor(lombok.AllArgsConstructor) HttpDownUtil(lee.study.down.util.HttpDownUtil) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Bootstrap(io.netty.bootstrap.Bootstrap) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) RequestProto(lee.study.proxyee.util.ProtoUtil.RequestProto) HttpDownInitializer(lee.study.down.handle.HttpDownInitializer) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 2 with ChunkInfo

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();
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) ChunkInfo(lee.study.down.model.ChunkInfo) RandomAccessFile(java.io.RandomAccessFile) BootstrapException(lee.study.down.exception.BootstrapException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 3 with ChunkInfo

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);
        }
    }
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) ChunkInfo(lee.study.down.model.ChunkInfo) WsForm(lee.study.down.mvc.form.WsForm)

Example 4 with ChunkInfo

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);
    }
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) ChunkInfo(lee.study.down.model.ChunkInfo)

Example 5 with ChunkInfo

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);
    }
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) ChunkInfo(lee.study.down.model.ChunkInfo)

Aggregations

ChunkInfo (lee.study.down.model.ChunkInfo)6 TaskInfo (lee.study.down.model.TaskInfo)6 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 BootstrapException (lee.study.down.exception.BootstrapException)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)1 SslContext (io.netty.handler.ssl.SslContext)1 NoopAddressResolverGroup (io.netty.resolver.NoopAddressResolverGroup)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1