Pu Zhibing
2025-04-18 9063f00db220dd93c94674d394ddbb38f83abd17
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
package com.sinata.push.util.echo;
 
import io.netty.channel.ChannelHandlerContext;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
 
public class NettyChannelMap {
 
    protected static Map<String, ChannelHandlerContext> map = new ConcurrentHashMap<>();
 
    public static Map<String, ChannelHandlerContext> ctxMap = new HashMap<>();//单点登录存储的通道
 
 
    private NettyChannelMap() {
        // 放置外部实例化
    }
    
    /**
     * Get data from source.
     * 
     * @param key
     * @return
     */
    public static ChannelHandlerContext getData(String key) {
        if(map==null){
            map = new HashMap<String, ChannelHandlerContext>();
        }
        return map.get(key);
    }
 
 
    public static ChannelHandlerContext getData_(String key) {
        if(ctxMap==null){
            ctxMap = new HashMap<String, ChannelHandlerContext>();
        }
        return ctxMap.get(key);
    }
 
    
    /**
     * Save data from source.
     * 
     * @param key
     * @param val
     */
    public static synchronized void saveData(String key, ChannelHandlerContext val) {
        map.put(key, val);
    }
    
    /**
     * Determine whether the cache key contains the key.
     * 
     * @param key
     * @return    true|false
     * @author TaoNingBo
     */
    public static synchronized boolean containsKey(String key) {
        return map.containsKey(key);
    }
    
    /**
     * Determine whether the cache value contains the value.
     * 
     * @param val
     * @return
     */
    public static synchronized boolean containsVal(ChannelHandlerContext val) {
        return map.containsValue(val);
    }
    
    /**
     * Remove the data resources.
     * 
     * @param value
     */
    @SuppressWarnings("rawtypes")
    public static synchronized void remove(ChannelHandlerContext value) {
        Set<String> strings = map.keySet();
        for(String key : strings){
            ChannelHandlerContext channelHandlerContext = map.get(key);
            String s = channelHandlerContext.channel().remoteAddress().toString();
            String s1 = value.channel().remoteAddress().toString();
            if(s.equals(s1)){
                channelHandlerContext.close();//关闭通道
                map.remove(key);
            }
        }
    }
 
 
 
    
    /**
     * Remove the data resources.
     * 
     * @param key
     * @author TaoNingBo
     */
    public static synchronized void remove(String key) {
        map.remove(key);
    }
    
    /**
     * Update the data resources.
     * 
     * @param key
     * @param value
     */
    public static synchronized void update(String key, ChannelHandlerContext value) {
        map.put(key, value);
    }
 
}