Pu Zhibing
7 天以前 1ee76c252f2dbba62e0ec34cccf9eaac51de9083
ruoyi-service/ruoyi-dataInterchange/src/main/java/com/ruoyi/dataInterchange/netty/client/ChannelMap.java
@@ -7,8 +7,8 @@
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * @author zhibing.pu
@@ -16,17 +16,17 @@
 */
public class ChannelMap {
   //存储主链路通道
   private static final ChannelGroup SERVER_GROUP = new DefaultChannelGroup("Jtt809Server", GlobalEventExecutor.INSTANCE);
   private static ChannelGroup SERVER_GROUP = new DefaultChannelGroup("Jtt809Server", GlobalEventExecutor.INSTANCE);
   //存储主链路ID
   private static final Map<Integer, ChannelId> SERVER_ID_MAP = new ConcurrentHashMap<>();
   private static Map<Integer, ChannelId> SERVER_ID_MAP = new HashMap<>();
   //存储从链路通道
   private static final ChannelGroup CLIENT_GROUP = new DefaultChannelGroup("Jtt809Client", GlobalEventExecutor.INSTANCE);
   private static ChannelGroup CLIENT_GROUP = new DefaultChannelGroup("Jtt809Client", GlobalEventExecutor.INSTANCE);
   //存储从链路ID
   private static final Map<Integer, ChannelId> CLIENT_ID_MAP = new ConcurrentHashMap<>();
   private static Map<Integer, ChannelId> CLIENT_ID_MAP = new HashMap<>();
   //存储从链路连接地址和端口号
   private static final Map<Integer, UPConnect> IP_PORT = new ConcurrentHashMap<>();
   private static Map<Integer, UPConnect> IP_PORT = new HashMap<>();
   //存储从链路连接重试次数
   private static final Map<Integer, Integer> TIMES = new ConcurrentHashMap<>();
   private static Map<Integer, Integer> TIMES = new HashMap<>();
   
   /**
    * 保存通道
@@ -34,8 +34,15 @@
    * @param key
    * @param channel
    */
   public static void addClientChannel(int key, ChannelId channel) {
      CLIENT_ID_MAP.put(key, channel);
   public static void addClientChannel(int key, Channel channel) {
      if(null == CLIENT_ID_MAP){
         CLIENT_ID_MAP = new HashMap<>();
      }
      CLIENT_ID_MAP.put(key, channel.id());
      if(null == CLIENT_GROUP){
         CLIENT_GROUP = new DefaultChannelGroup("Jtt809Client", GlobalEventExecutor.INSTANCE);
      }
      CLIENT_GROUP.add(channel);
   }
   
   
@@ -46,7 +53,16 @@
    * @return
    */
   public static Channel getClientChannel(int key) {
      if(null == CLIENT_ID_MAP){
         CLIENT_ID_MAP = new HashMap<>();
      }
      ChannelId channelId = CLIENT_ID_MAP.get(key);
      if (null == channelId) {
         return null;
      }
      if(null == CLIENT_GROUP){
         CLIENT_GROUP = new DefaultChannelGroup("Jtt809Client", GlobalEventExecutor.INSTANCE);
      }
      Channel channel = CLIENT_GROUP.find(channelId);
      return channel;
   }
@@ -58,8 +74,15 @@
    * @param key
    * @param channel
    */
   public static void addServerChannel(int key, ChannelId channel) {
      SERVER_ID_MAP.put(key, channel);
   public static void addServerChannel(int key, Channel channel) {
      if(null == SERVER_ID_MAP){
         SERVER_ID_MAP = new HashMap<>();
      }
      SERVER_ID_MAP.put(key, channel.id());
      if(null == SERVER_GROUP){
         SERVER_GROUP = new DefaultChannelGroup("Jtt809Server", GlobalEventExecutor.INSTANCE);
      }
      SERVER_GROUP.add(channel);
   }
   
   
@@ -71,6 +94,9 @@
    */
   public static Channel getServerChannel(int key) {
      ChannelId channelId = SERVER_ID_MAP.get(key);
      if (null == channelId) {
         return null;
      }
      Channel channel = SERVER_GROUP.find(channelId);
      return channel;
   }
@@ -83,6 +109,9 @@
    * @param req
    */
   public static void addIpAndPort(int key, UPConnect req) {
      if(null == IP_PORT){
         IP_PORT = new HashMap<>();
      }
      IP_PORT.put(key, req);
   }
   
@@ -93,6 +122,9 @@
    * @param key
    */
   public static UPConnect getIpAndPort(int key) {
      if(null == IP_PORT){
         IP_PORT = new HashMap<>();
      }
      return IP_PORT.get(key);
   }
   
@@ -102,7 +134,10 @@
    * @param key
    * @return
    */
   public static int getTimes(int key) {
   public static Integer getTimes(int key) {
      if(null == TIMES){
         TIMES = new HashMap<>();
      }
      return TIMES.get(key);
   }
   
@@ -113,6 +148,9 @@
    * @return
    */
   public static int saveTimes(int key, int t) {
      if(null == TIMES){
         TIMES = new HashMap<>();
      }
      return TIMES.put(key, t);
   }
}