package com.qst.dms.net;

import com.qst.dms.entity.MatchedLogRec;
import com.qst.dms.entity.MatchedTransport;
import com.qst.dms.service.LogRecService;
import com.qst.dms.service.TransportService;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class DmsNetServer {
    public static void main(String[] args) {
        AcceptLogThread acceptLogThread = new AcceptLogThread(10000);
        AcceptTranThread acceptTranThread = new AcceptTranThread(10001);
        System.out.println("Start listening port 10000/10001");
        acceptLogThread.start();
        acceptTranThread.start();
    }
}

class AcceptLogThread extends Thread{
    Socket socket;
    ObjectInputStream ois;
    ServerSocket serverSocket;
    LogRecService logRecService;

    AcceptLogThread(int port){
        logRecService = new LogRecService();
        try{
            serverSocket = new ServerSocket(port);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        while(this.isAlive())
        {
            try{
                socket = serverSocket.accept();
                if(socket!=null)
                {
                    ois = new ObjectInputStream(socket.getInputStream());
                    ArrayList<MatchedLogRec>matchedLogRecs = (ArrayList<MatchedLogRec>) ois.readObject();
                    if (matchedLogRecs == null) {
                        continue;
                    }
                    logRecService.saveMatchLogToDB(matchedLogRecs);
                    System.out.println("完成一次日志数据接收");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}
class AcceptTranThread extends Thread{
    ServerSocket serverSocket;
    Socket socket;
    ObjectInputStream ois;
    TransportService transportService;

    AcceptTranThread(int port){
        transportService = new TransportService();
        try{
            serverSocket = new ServerSocket(port);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    @Override
    public void run() {
        while(this.isAlive())
        {
            try{
                socket = serverSocket.accept();
                if(socket!=null)
                {
                    ois = new ObjectInputStream(socket.getInputStream());
                    ArrayList<MatchedTransport>matchedTransports = (ArrayList<MatchedTransport>) ois.readObject();
                    if (matchedTransports == null) {
                        continue;
                    }
                    transportService.saveMatchTransportToDB(matchedTransports);
                    System.out.println("完成一次物流数据接收");
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

