【Java项目】多文件传输:资源接收服务器

该篇主要完成资源接收。

1、资源接收端通信模式

  1. 资源接收端根据文件长度接收资源片段。每接收一个文件,关闭通信。
  2. 资源接收端需要准备,未接收片段池和文件对象池。
  3. 接收端可能有多个发送端。

接收端建立C/S服务器端。

2、接收片段内容处理

  1. 判断发送端是否发送完毕;发送完毕,关闭通信。
  2. 获取片段内容
  3. 将内容写入文件
  4. 填充未接收片段
  5. 若未接收片段未空,关闭文件资源。
 class SectionProcesser implements ISectionProcesser {
        private RandomAccessFilePool accessFilePool;
        private UnreceivePool unreceivePool;

        public SectionProcesser(RandomAccessFilePool accessFilePool,
                                UnreceivePool unreceivePool) {
            this.accessFilePool = accessFilePool;
            this.unreceivePool = unreceivePool;
        }

        @Override
        public void processerSection(SectionHander sectionHander, byte[] context) throws Exception {
            long fileId = sectionHander.getFileId();
            if (fileId == -1) {
                close();
                return;
            }
            RandomAccessFile raf = accessFilePool.getRandomAccessFile(fileId, "rw");
            FileAccessor.writeSection(raf,sectionHander.getOffset(),context);
            UnreceiveSection unreceiveSection = unreceivePool.getUnreceiveSection(fileId);
            boolean isAllreceive = unreceiveSection.receiveSection(sectionHander);
            if (isAllreceive) {
                this.accessFilePool.closeRaf(fileId);
            }
        }
    }

3. 接收线程

负责对一个接收端内容接收

package man.kuke.core;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.Socket;

/**
 * @author: kuke
 * @date: 2020/11/29 - 20:08
 * @description:
 */
public class Receiver implements Runnable{
    private DataInputStream dis;
    private SectionTransfer sectionTransfer;
    private volatile boolean goon;
    private Socket socket;

    public Receiver(RandomAccessFilePool accessFilePool,
                        UnreceivePool unreceivePool,Socket socket) {
        try {
            dis = new DataInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.socket = socket;
        //片段接收器
        sectionTransfer = new SectionTransfer();
        goon = true;
        this.sectionTransfer.setSectionProcessor(new SectionProcesser(accessFilePool,unreceivePool));
    }

    public void start() {
        new Thread(this).start();
    }

    @Override
    public void run() {
        while(goon) {
            try {
                this.sectionTransfer.receiveSection(this.dis);
            } catch (Exception e) {
                //接收失败,应该考虑断点续传
                if (goon) {
                //TODO dealerror
                }
                close();
            }
        }
        close();
    }
    private void close() {
        goon = false;
        try {
            if (dis != null) {
                dis.close();
            }
        } catch (IOException ignored) {
        } finally {
            dis = null;
        }

        try {
            if (socket != null && socket.isClosed()) {
                socket.close();
            }
        } catch (IOException ignored) {
        } finally {
            socket = null;
        }
    }

    class SectionProcesser implements ISectionProcesser {
        private RandomAccessFilePool accessFilePool;
        private UnreceivePool unreceivePool;

        public SectionProcesser(RandomAccessFilePool accessFilePool,
                                UnreceivePool unreceivePool) {
            this.accessFilePool = accessFilePool;
            this.unreceivePool = unreceivePool;
        }

        @Override
        public void processerSection(SectionHander sectionHander, byte[] context) throws Exception {
            long fileId = sectionHander.getFileId();
            if (fileId == -1) {
                close();
                return;
            }
            RandomAccessFile raf = accessFilePool.getRandomAccessFile(fileId, "rw");
            FileAccessor.writeSection(raf,sectionHander.getOffset(),context);
            UnreceiveSection unreceiveSection = unreceivePool.getUnreceiveSection(fileId);
            boolean isAllreceive = unreceiveSection.receiveSection(sectionHander);
            if (isAllreceive) {
                this.accessFilePool.closeRaf(fileId);
            }
        }
    }
}

4.接收端服务器

  1. 接收端服务器对资源进行分割多个部分,生成未接收片段池
  2. 设置资源存放路径
  3. 根据资源信息,生成相应文件目录
  4. 指定发送端个数。

    public ReceiveServer(int senderCount,String absoluteRoot,
                          ResourceInfo resourceInfo) {

        this.port = DEFAULT_PORT;
        this.senderCount = senderCount;
        this.accessFilePool = new RandomAccessFilePool(resourceInfo);
        this.unreceivePool = newUnreceivePool(resourceInfo);
        resourceInfo.createDirctories(absoluteRoot);
    }

    /**
     * 接收方根据文件列表,准备接收文件。
     * @param resourceInfo
     * @return
     */
    private static UnreceivePool newUnreceivePool(ResourceInfo resourceInfo) {
        UnreceivePool unreceivePool = new UnreceivePool();
        Iterator<FileInfo> fileInfoAccessor = resourceInfo.fileInfoAccessor();
        while (fileInfoAccessor.hasNext()) {
            FileInfo next = fileInfoAccessor.next();
            unreceivePool.addTargetFile(next.getId(),next.getSize());
        }
        return unreceivePool;
    }
  1. 开启服务器

    public void startup() {
//        if (this.goon) {
//            return;
//        }

        try {
            serverSocket = new ServerSocket(port);
            new Thread(this).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  1. 打开接收端线程

    @Override
    public void run() {
        int linkedSenderCount = 0;
        //TODO 这里没有考虑到某一个发送端,在发送前连接时,
        //就已经崩溃了。
        //处理方法提示,进行超时处理即可。
        while (linkedSenderCount < this.senderCount) {
            try {
                Socket socket = serverSocket.accept();
                Receiver receiver = new Receiver(accessFilePool, unreceivePool, socket);
                receiver.start();
                linkedSenderCount++;
            } catch (IOException e) {
//                this.goon = false;
            }
        }

    }
  1. 关闭通信

    public void shutdown() {
        if(!goon) {
            return;
        }
        close();
    }

    private void close() {
        if (serverSocket != null && !serverSocket.isClosed()) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                serverSocket=null;
            }
        }
        goon = false;

    }

3.总结

该篇仅针对接收文件接收。已指派的发送端,用线程相应接收。接收端根据资源信息生成接收列表,根据发送端个数,开辟接收线程。每接收一片内容,都会在未接收片段池有一段记录,这样保证了传输可靠性。但是建立接收服务器前提是:需要知道接收端地址,与接收端协商要发送的资源

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值