DownloadFileReceiverThread.java 5.11 KB
package com.sunvote.xpadcomm;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import com.sunvote.util.LogUtil;

/**
 * Created by Elvis on 2017/12/25 15:55
 * Email:Eluis@psunsky.com
 * 版权所有:长沙中天电子设计开发有限公司
 * Description: 人大通用版XPadAppRD
 * 下载文件,接收数据线程
 */
public class DownloadFileReceiverThread extends Thread {

    public static final String TAG = "ReceiverThread";
    public static final int DEFAULT_PROT = 15155;
    private boolean run = true;
    private DatagramSocket udpSocketServ;
    private int port = DEFAULT_PROT;
    private final int BLOCK_SIZE = 1024 * 50;
    private byte[] udpRecvBuf;

    public DownloadFileReceiverThread(int port) {
        super(TAG);
        this.port = port;
    }

    public interface OnDownloadCmdListener {

        /**
         * 文件信息
         * @param filename 文件名
         * @param filelength 文件长度
         * @param fileFlag 文件标志
         */
        void onReceiverFileInfo(String filename, long filelength, byte fileFlag);

        /**
         * 文件数据包
         * @param blockId 第几包
         * @param datas 数据段落
         * @param start 起始位置
         * @param dataLen 数据长度
         */
        void onReceiverFileData(int blockId, byte[] datas, int start, int dataLen);

        /**
         * 文件下载完成
         */
        void onFileDownloadCompeleted();
    }

    @Override
    public void run() {
        udpRecvBuf = new byte[BLOCK_SIZE + 8];
        DatagramPacket packet = new DatagramPacket(udpRecvBuf, udpRecvBuf.length);
        int cmdType;
        long fileLen;
        while (isRun()) {
            if (udpSocketServ == null) {
                try {
                    udpSocketServ = new DatagramSocket(port);
                    udpSocketServ.setSoTimeout(15000);
                } catch (Exception ex) {
                    LogUtil.e(TAG, ex);
                    udpSocketServ = null;
                }
            }
            if (udpSocketServ != null) {
                try {
                    udpSocketServ.receive(packet);
                    if (packet.getLength() > 0) {
                        LogUtil.i(TAG, "receiver data: ", packet.getData(), packet.getLength());
                        if ((udpRecvBuf[0] & 0xff) == 0xFE && (udpRecvBuf[1] & 0xff) == 0xAA) {
                            byte fileFlag = (byte) (udpRecvBuf[2] & 0xff);
                            cmdType = udpRecvBuf[3];
                            if (cmdType == 1) { //文件信息
                                fileLen = ((udpRecvBuf[8] & 0xff) << 3 * 8)
                                        | ((udpRecvBuf[9] & 0xff) << 2 * 8)
                                        | ((udpRecvBuf[10] & 0xff) << 1 * 8)
                                        | (udpRecvBuf[11] & 0xff);
                                byte fileNameLen = udpRecvBuf[12];
                                byte[] namebuf = new byte[fileNameLen];
                                System.arraycopy(udpRecvBuf, 13, namebuf, 0, fileNameLen);
                                String fileName = new String(namebuf, "unicode");
                                LogUtil.d(TAG, "File Len:" + fileLen + ",fileName:" + fileName + ", fileFlag:" + fileFlag);
                                if(onDownloadCmdListener != null){
                                    onDownloadCmdListener.onReceiverFileInfo(fileName,fileLen,fileFlag);
                                }
                            } else if (cmdType == 2) {
                                int blockId = ((udpRecvBuf[4] & 0xff) << 8) | (udpRecvBuf[5] & 0xff);
                                int dataLen = ((udpRecvBuf[6] & 0xff) << 8) | (udpRecvBuf[7] & 0xff);
                                LogUtil.i(TAG, "Rece blockid:" + blockId + " ,datalen:" + dataLen);
                                if(onDownloadCmdListener != null){
                                    onDownloadCmdListener.onReceiverFileData(blockId,udpRecvBuf,8,dataLen);
                                }
                            }else if(cmdType == 3){
                                if(onDownloadCmdListener != null){
                                    onDownloadCmdListener.onFileDownloadCompeleted();
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    LogUtil.e(TAG, e);
                    if(udpSocketServ != null) {
                        udpSocketServ.close();
                        udpSocketServ = null;
                    }
                }
            }
        }
        LogUtil.i(TAG,"receiver thread finish.");
    }

    public void close() {
        run = false;
        if(udpSocketServ != null){
            udpSocketServ.close();
            udpSocketServ = null;
        }
    }

    public boolean isRun() {
        return run;
    }

    private OnDownloadCmdListener onDownloadCmdListener;

    public void setOnDownloadCmdListener(OnDownloadCmdListener onDownloadCmdListener) {
        this.onDownloadCmdListener = onDownloadCmdListener;
    }
}