|  | @@ -0,0 +1,123 @@
 | 
	
		
			
				|  |  | +package com.ruoyi.sim.service.impl;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import com.ruoyi.common.core.domain.AjaxResult;
 | 
	
		
			
				|  |  | +import com.ruoyi.sim.config.SimConfig;
 | 
	
		
			
				|  |  | +import org.slf4j.Logger;
 | 
	
		
			
				|  |  | +import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | +import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.io.IOException;
 | 
	
		
			
				|  |  | +import java.net.InetAddress;
 | 
	
		
			
				|  |  | +import java.net.Socket;
 | 
	
		
			
				|  |  | +import java.net.UnknownHostException;
 | 
	
		
			
				|  |  | +import java.util.HashMap;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import static com.ruoyi.sim.constant.CommConst.SOCKET_TIME_OUT;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +@Service
 | 
	
		
			
				|  |  | +public class SocketService {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static final Logger l = LoggerFactory.getLogger(SocketService.class);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static final int INIT_SIZE = 16;
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * key:
 | 
	
		
			
				|  |  | +     * value:
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private static HashMap<String, Socket> cachedMap = new HashMap<>(INIT_SIZE);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private SimConfig config;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private FailedCountService failedCountService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * @param ip
 | 
	
		
			
				|  |  | +     * @return true:socket ok!
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public boolean isOk(final String ip) {
 | 
	
		
			
				|  |  | +        if (cachedMap.containsKey(ip)) {
 | 
	
		
			
				|  |  | +            Socket s = cachedMap.get(ip);
 | 
	
		
			
				|  |  | +            if (s != null) {
 | 
	
		
			
				|  |  | +                return (s.isConnected() && s.isBound() && !s.isClosed());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return false;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * @param ip   ip v4.
 | 
	
		
			
				|  |  | +     * @param port
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public AjaxResult openOne(final String ip, final int port) {
 | 
	
		
			
				|  |  | +        // check.
 | 
	
		
			
				|  |  | +        if (!config.isCommGlobal()) {
 | 
	
		
			
				|  |  | +            l.warn("isCommGlobal == false [模拟器通信被禁用!]");
 | 
	
		
			
				|  |  | +            return AjaxResult.error("模拟器通信被禁用!");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            if (!isOk(ip)) {
 | 
	
		
			
				|  |  | +                l.info("openSocket cachedSocket is not ok!new socket ip = {}!", ip);
 | 
	
		
			
				|  |  | +                closeOne(ip);
 | 
	
		
			
				|  |  | +                Socket s = new Socket(ip, port);
 | 
	
		
			
				|  |  | +                s.setSoTimeout(SOCKET_TIME_OUT);
 | 
	
		
			
				|  |  | +                cachedMap.put(ip, s);
 | 
	
		
			
				|  |  | +                // failed count reset.
 | 
	
		
			
				|  |  | +                failedCountService.reset0(ip);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } catch (IOException e) {
 | 
	
		
			
				|  |  | +            throw new RuntimeException(e);
 | 
	
		
			
				|  |  | +        } finally {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public AjaxResult closeOne(final String ip) {
 | 
	
		
			
				|  |  | +        String msgOk = "关闭Socket成功!";
 | 
	
		
			
				|  |  | +        if (!config.isCommGlobal()) {
 | 
	
		
			
				|  |  | +            l.warn("isCommGlobal == {} [模拟器通信被禁用!]", config.isCommGlobal());
 | 
	
		
			
				|  |  | +            return AjaxResult.error("模拟器通信被禁用!");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (!cachedMap.containsKey(ip)) {
 | 
	
		
			
				|  |  | +            return AjaxResult.success(msgOk);
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                Socket s = cachedMap.get(ip);
 | 
	
		
			
				|  |  | +                s.getInputStream().close();
 | 
	
		
			
				|  |  | +                s.getOutputStream().close();
 | 
	
		
			
				|  |  | +                s.close();
 | 
	
		
			
				|  |  | +            } catch (IOException e) {
 | 
	
		
			
				|  |  | +                e.printStackTrace();
 | 
	
		
			
				|  |  | +            } finally {
 | 
	
		
			
				|  |  | +                cachedMap.remove(ip);
 | 
	
		
			
				|  |  | +                // failed count reset.
 | 
	
		
			
				|  |  | +                failedCountService.reset0(ip);
 | 
	
		
			
				|  |  | +                return AjaxResult.success(msgOk);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public AjaxResult closeAll(final String[] ipArray) {
 | 
	
		
			
				|  |  | +        String msgOk = "关闭所有Socket成功!";
 | 
	
		
			
				|  |  | +        for (String ip : ipArray) {
 | 
	
		
			
				|  |  | +            AjaxResult ar = closeOne(ip);
 | 
	
		
			
				|  |  | +            if (ar != null && !ar.isSuccess()) {
 | 
	
		
			
				|  |  | +                return ar;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return AjaxResult.success(msgOk);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 初始化。
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public void resetAll() {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 |