| | |
| | | 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 |
| | |
| | | */ |
| | | 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<>(); |
| | | |
| | | /** |
| | | * 保存通道 |
| | |
| | | * @param 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); |
| | | } |
| | | |
| | |
| | | * @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; |
| | |
| | | * @param 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); |
| | | } |
| | | |
| | |
| | | * @param req |
| | | */ |
| | | public static void addIpAndPort(int key, UPConnect req) { |
| | | if(null == IP_PORT){ |
| | | IP_PORT = new HashMap<>(); |
| | | } |
| | | IP_PORT.put(key, req); |
| | | } |
| | | |
| | |
| | | * @param key |
| | | */ |
| | | public static UPConnect getIpAndPort(int key) { |
| | | if(null == IP_PORT){ |
| | | IP_PORT = new HashMap<>(); |
| | | } |
| | | return IP_PORT.get(key); |
| | | } |
| | | |
| | |
| | | * @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); |
| | | } |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public static int saveTimes(int key, int t) { |
| | | if(null == TIMES){ |
| | | TIMES = new HashMap<>(); |
| | | } |
| | | return TIMES.put(key, t); |
| | | } |
| | | } |