puzhibing
2025-05-06 4e87f5f570a84621734035217f08882f52809c48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.ruoyi.dataInterchange.netty.client;
 
import com.ruoyi.dataInterchange.wapper.UPConnect;
import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import io.netty.channel.group.ChannelGroup;
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
 * @Date 2025/3/4 19:17
 */
public class ChannelMap {
    //存储主链路通道
    private static ChannelGroup SERVER_GROUP = new DefaultChannelGroup("Jtt809Server", GlobalEventExecutor.INSTANCE);
    //存储主链路ID
    private static Map<Integer, ChannelId> SERVER_ID_MAP = new HashMap<>();
    //存储从链路通道
    private static ChannelGroup CLIENT_GROUP = new DefaultChannelGroup("Jtt809Client", GlobalEventExecutor.INSTANCE);
    //存储从链路ID
    private static Map<Integer, ChannelId> CLIENT_ID_MAP = new HashMap<>();
    //存储从链路连接地址和端口号
    private static Map<Integer, UPConnect> IP_PORT = new HashMap<>();
    //存储从链路连接重试次数
    private static Map<Integer, Integer> TIMES = new HashMap<>();
    
    /**
     * 保存通道
     *
     * @param key
     * @param channel
     */
    public static void addClientChannel(int key, Channel channel) {
        CLIENT_ID_MAP.put(key, channel.id());
        CLIENT_GROUP.add(channel);
    }
    
    
    /**
     * 获取通道
     *
     * @param key
     * @return
     */
    public static Channel getClientChannel(int key) {
        ChannelId channelId = CLIENT_ID_MAP.get(key);
        if (null == channelId) {
            return null;
        }
        Channel channel = CLIENT_GROUP.find(channelId);
        return channel;
    }
    
    
    /**
     * 保存通道
     *
     * @param key
     * @param channel
     */
    public static void addServerChannel(int key, Channel channel) {
        SERVER_ID_MAP.put(key, channel.id());
        SERVER_GROUP.add(channel);
    }
    
    
    /**
     * 获取通道
     *
     * @param key
     * @return
     */
    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;
    }
    
    
    /**
     * 缓存从链路的IP地址和端口号
     *
     * @param key
     * @param req
     */
    public static void addIpAndPort(int key, UPConnect req) {
        IP_PORT.put(key, req);
    }
    
    
    /**
     * 获取从链路的IP地址和端口号
     *
     * @param key
     */
    public static UPConnect getIpAndPort(int key) {
        return IP_PORT.get(key);
    }
    
    /**
     * 获取从链路连接重试次数
     *
     * @param key
     * @return
     */
    public static Integer getTimes(int key) {
        return TIMES.get(key);
    }
    
    /**
     * 保存从链路连接重试次数
     *
     * @param key
     * @return
     */
    public static int saveTimes(int key, int t) {
        return TIMES.put(key, t);
    }
}