/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2021/12/20
 */
package org.ts.ddcs.tcp;

import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.process.DataProcessHolder;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Callable;


/***
 * Date:2021/12/20
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @mender：（文件的修改者，文件创建者之外的人）
 * @version 1.0
 * Remark：认为有必要的其他信息
 */

public class TcpListener implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        try {
            while (true) {
                try (ServerSocket serverSocket = new ServerSocket(DataProcessHolder.newInstance().dataProcess.config.serverConfig.getTcpPort())) {
                    //侦听并接受到此套接字的连接,返回一个Socket对象
                    Socket socket = serverSocket.accept();
                    InetAddress inetAddress = socket.getInetAddress();
                    String ip = inetAddress.getHostAddress();
                    LogHelper.info("TcpListener " + ip);
                    DataProcessHolder.newInstance().dataProcess.submitSocketConnect(ip, socket);
                    Thread.sleep(200);
                } catch (IOException io) {
                    LogHelper.error(io);
                    break;
                }
            }
        } catch (NullPointerException e) {
            LogHelper.error(e);
        } finally {
            DataProcessHolder.newInstance().dataProcess.startUdpListen();
        }
        return 0;
    }
}
