Przeglądaj źródła

多Socket初步实现。

tom 3 miesięcy temu
rodzic
commit
a474b4b99e

+ 46 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/FailedCountService.java

@@ -0,0 +1,46 @@
+package com.ruoyi.sim.service.impl;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+@Service
+public class FailedCountService {
+
+    private static final Logger l = LoggerFactory.getLogger(FailedCountService.class);
+
+    private static final int COUNT_0 = 0;
+    private static final int COUNT_ADD_1 = 1;
+    private static final int INIT_SIZE = 16;
+    private static HashMap<String, AtomicInteger> failedMap = new HashMap<>(INIT_SIZE);
+
+    public int plus1(final String ip) {
+        if (!failedMap.containsKey(ip)) {
+            failedMap.put(ip, new AtomicInteger(COUNT_ADD_1));
+        } else {
+            failedMap.get(ip).addAndGet(COUNT_ADD_1);
+        }
+        return failedMap.get(ip).get();
+    }
+
+    public int get(final String ip) {
+        if (failedMap.containsKey(ip)) {
+            return failedMap.get(ip).get();
+        } else {
+            return COUNT_0;
+        }
+    }
+
+    public void reset0(final String ip) {
+        failedMap.get(ip).set(COUNT_0);
+    }
+
+    public void resetAll() {
+        for (String ip : failedMap.keySet()) {
+            failedMap.remove(ip);
+        }
+    }
+}

+ 29 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/IpService.java

@@ -0,0 +1,29 @@
+package com.ruoyi.sim.service.impl;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+public class IpService {
+
+
+    /**
+     * 等同于ping命令。
+     *
+     * @param ip
+     * @return
+     * @throws IOException
+     */
+    public boolean isPingOk(final String ip) {
+        try {
+            InetAddress ia = InetAddress.getByName(ip);
+            return ia.isReachable(2048);
+        } catch (UnknownHostException e) {
+            e.printStackTrace();
+            return false;
+        } catch (IOException e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+}

+ 123 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/SocketService.java

@@ -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() {
+
+    }
+
+}