Pu Zhibing
3 天以前 1ee76c252f2dbba62e0ec34cccf9eaac51de9083
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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;
 
/**
 * @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) {
        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);
    }
    
    
    /**
     * 获取通道
     *
     * @param key
     * @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 key
     * @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 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) {
        if(null == IP_PORT){
            IP_PORT = new HashMap<>();
        }
        IP_PORT.put(key, req);
    }
    
    
    /**
     * 获取从链路的IP地址和端口号
     *
     * @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 Integer getTimes(int key) {
        if(null == TIMES){
            TIMES = new HashMap<>();
        }
        return TIMES.get(key);
    }
    
    /**
     * 保存从链路连接重试次数
     *
     * @param key
     * @return
     */
    public static int saveTimes(int key, int t) {
        if(null == TIMES){
            TIMES = new HashMap<>();
        }
        return TIMES.put(key, t);
    }
}