/
ChannelManager.java
86 lines (69 loc) · 2.61 KB
/
ChannelManager.java
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
package com.baidu.brpc.server;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.commons.lang3.StringUtils;
import com.baidu.brpc.server.push.PushChannelContextHolder;
import io.netty.channel.Channel;
import io.netty.util.Attribute;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ChannelManager {
private static volatile ChannelManager instance;
private static ChannelStoreManager storeManager;
private ReadWriteLock lock = new ReentrantReadWriteLock();
private ChannelStoreManager innerStoreManager;
public static void setStoreManager(ChannelStoreManager manager) {
storeManager = manager;
}
public static ChannelManager getInstance() {
if (instance == null) {
synchronized (ChannelManager.class) {
if (instance == null) {
if (storeManager != null) {
instance = new ChannelManager(storeManager);
} else {
instance = new ChannelManager(new DefaultChannelStoreManager());
}
}
}
}
return instance;
}
private ChannelManager(ChannelStoreManager storeManager) {
this.innerStoreManager = storeManager;
}
public void putChannel(String clientName, Channel channel) {
lock.writeLock().lock();
innerStoreManager.putChannel(clientName, channel);
lock.writeLock().unlock();
}
public Channel getChannel(String clientName) {
if (log.isDebugEnabled()) {
for (Map.Entry<String, List<Channel>> entry : innerStoreManager.getChannelMap().entrySet()) {
log.debug("participantName={}, channelNum={}",
entry.getKey(),
entry.getValue() == null ? 0 : entry.getValue().size());
}
}
lock.readLock().lock();
try {
return innerStoreManager.getChannel(clientName);
} finally {
lock.readLock().unlock();
}
}
public void removeChannel(Channel channel) {
Attribute<String> participant = channel.attr(PushChannelContextHolder.CLIENTNAME_KEY);
String participantName = participant.get();
if (StringUtils.isNotBlank(participantName)) {
lock.writeLock().lock();
innerStoreManager.removeChannel(channel);
lock.writeLock().unlock();
}
}
public Map<String, List<Channel>> getChannelMap() {
return innerStoreManager.getChannelMap();
}
}