package com.xixun.communicate;

import com.xixun.display.MyLog;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * Created by Administrator on 2018/05/21.
 */

public class UdpRece {

    static final int BUFFER_SIZE = 1024;
    int iHostPort=0;

    UdpRece(int iHostPort)
    {   this.iHostPort=iHostPort;

        new Thread() {
            public void run() {
                Process();
            }
        }.start();
    }

    private void Process()
    {
        while (NeedExit()==false) {
            try {
                byte[]  receDataBufferSize = new byte[BUFFER_SIZE];
                DatagramSocket udpReceSocket = new DatagramSocket(iHostPort);
                DatagramPacket recePacket = new DatagramPacket(receDataBufferSize, receDataBufferSize.length);

                while (NeedExit()==false)
                {   udpReceSocket.receive(recePacket);
                    ReceDataProcess(recePacket);
                }

                udpReceSocket.close();
            }catch (Exception e) {
                if (NeedExit()==false)
                    MyLog.i(Thread.currentThread().getStackTrace()[2].getFileName() + "-->"
                            +   this.getClass() + "-->"
                            +   "Function: " +Thread.currentThread().getStackTrace()[2].getMethodName() + "-->"
                            +   "Line" +Thread.currentThread().getStackTrace()[2].getLineNumber() + ":\r\n"
                            +   "    Error:" + e.getMessage() + "\r\n\r\n"
                    );
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }


    public void ReceDataProcess(DatagramPacket recePacket)
    {  // 通过数据报得到发送方的IP.
        InetAddress inetAddress_HostIP=recePacket.getAddress();
        String hostIP=inetAddress_HostIP.getHostAddress();
        //MyLog.i("==================================================== rIP：" + hostIP);
        if (ReceCommand(new String(recePacket.getData(),0,recePacket.getLength()))<0) return;
    }

    public int ReceCommand(String receDataBuffer)
    {   int i=0,iWorkResault=0,iOffset=0,iReceLenth=receDataBuffer.length();

        if (iReceLenth<=0) return -1;

        while(iOffset<iReceLenth)
        {
            String receToatlCommand=receDataBuffer.substring(iOffset);

            MyLog.i("-------------receToatlCommand:" + receToatlCommand);
            int iCommandSize=receToatlCommand.indexOf(';'); // 发送方有可能没有发送时间信息，也就没有分号。

            if (iCommandSize>0) iOffset++;
            else iCommandSize = iReceLenth - iOffset; // 无命令结束符，只有一条命令，或最后一条命令。
            iOffset+=iCommandSize;

            {   String receSubCommand = receToatlCommand.substring(0, iCommandSize);

                int iCommandParamsStart=receSubCommand.indexOf('=');
                String strValue=receSubCommand.substring(iCommandParamsStart + 1);
                //MyLog.i("strValue:" + strValue);
                //long lValue=Long.parseLong(strValue);
                //if (iValue<0) return -1;
                //MyLog.i("lValue:" + lValue);



                if (receSubCommand.indexOf("Time")==0)
                {   Define.SetTime(strValue);
                }
                else
                if (receSubCommand.indexOf("Light")==0)
                {
                }
            }
        }
        //MyLog.i("---------------------------------------Rece:" + receDataBuffer);
        return iWorkResault;
    }

    boolean bNeedExit=false;
    public Boolean NeedExit() { return bNeedExit; }
    public void Exit()
    {    if (NeedExit()) return;
        bNeedExit=true;
    }
}
