-
Notifications
You must be signed in to change notification settings - Fork 668
/
MembershipKeyImpl.java
219 lines (181 loc) · 7.16 KB
/
MembershipKeyImpl.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.nio.ch;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.io.IOException;
import java.nio.channels.MembershipKey;
import java.nio.channels.MulticastChannel;
import java.util.HashSet;
/**
* MembershipKey implementation.
*/
// 组播小组成员的实现
class MembershipKeyImpl extends MembershipKey {
private final MulticastChannel channel; // 当前成员所属的通道
private final InetAddress group; // 组播小组地址
private final NetworkInterface interf; // 接收消息的网络接口(网卡)
private final InetAddress source; // 过滤消息的地址
private volatile boolean invalid; // 指示当前成员是否有效
/** set of source addresses that are blocked */
// 记录屏蔽的消息源
private HashSet<InetAddress> blockedSet;
/** lock used when creating or accessing blockedSet */
private final Object stateLock = new Object();
private MembershipKeyImpl(MulticastChannel channel, InetAddress group, NetworkInterface interf, InetAddress source) {
this.channel = channel;
this.group = group;
this.interf = interf;
this.source = source;
}
// 将当前组播小组成员设置为无效
void invalidate() {
invalid = true;
}
// 判断当前组播小组成员是否已经无效
public boolean isValid() {
return !invalid;
}
// 从通道的组播注册表中移除当前组播小组成员;实际操作是将目标组播Socket从所在的组播小组中移除
public void drop() {
// delegate to channel
((DatagramChannelImpl) channel).drop(this);
}
// 屏蔽source处发来的消息,即禁止从source处接收消息;如果该组播小组已经设置了过滤,则抛异常
@Override
public MembershipKey block(InetAddress source) throws IOException {
// 组播小组已经限定了只从source处接收消息,那么当前屏蔽方法无法执行下去
if(source != null) {
throw new IllegalStateException("key is source-specific");
}
synchronized(stateLock) {
if((blockedSet != null) && blockedSet.contains(source)) {
// already blocked, nothing to do
return this;
}
((DatagramChannelImpl) channel).block(this, source);
// created blocked set if required and add source address
if(blockedSet == null) {
blockedSet = new HashSet<>();
}
blockedSet.add(source);
}
return this;
}
// 解除对source地址的屏蔽,即允许接收source处的消息
@Override
public MembershipKey unblock(InetAddress source) {
synchronized(stateLock) {
if((blockedSet == null) || !blockedSet.contains(source)) {
throw new IllegalStateException("not blocked");
}
((DatagramChannelImpl) channel).unblock(this, source);
blockedSet.remove(source);
}
return this;
}
// 返回当前成员所属的通道
@Override
public MulticastChannel channel() {
return channel;
}
// 返回组播小组地址
@Override
public InetAddress group() {
return group;
}
// 返回接收消息的网络接口(网卡)
@Override
public NetworkInterface networkInterface() {
return interf;
}
// 返回过滤消息的地址
@Override
public InetAddress sourceAddress() {
return source;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(64);
sb.append('<');
sb.append(group.getHostAddress());
sb.append(',');
sb.append(interf.getName());
if(source != null) {
sb.append(',');
sb.append(source.getHostAddress());
}
sb.append('>');
return sb.toString();
}
/**
* MembershipKey will additional context for IPv4 membership
*/
// IP4类型的组播小组成员
static class Type4 extends MembershipKeyImpl {
private final int groupAddress; // 组播小组地址
private final int interfAddress; // 接收消息的网络接口的地址
private final int sourceAddress; // 过滤消息的地址
Type4(MulticastChannel ch, InetAddress group, NetworkInterface interf, InetAddress source, int groupAddress, int interfAddress, int sourceAddress) {
super(ch, group, interf, source);
this.groupAddress = groupAddress;
this.interfAddress = interfAddress;
this.sourceAddress = sourceAddress;
}
int groupAddress() {
return groupAddress;
}
int interfaceAddress() {
return interfAddress;
}
int source() {
return sourceAddress;
}
}
/**
* MembershipKey will additional context for IPv6 membership
*/
// IP6类型的组播小组成员
static class Type6 extends MembershipKeyImpl {
private final byte[] groupAddress; // 组播小组地址
private final int index; // 接收消息的网络接口的索引
private final byte[] sourceAddress; // 过滤消息的地址
Type6(MulticastChannel ch, InetAddress group, NetworkInterface interf, InetAddress source, byte[] groupAddress, int index, byte[] sourceAddress) {
super(ch, group, interf, source);
this.groupAddress = groupAddress;
this.index = index;
this.sourceAddress = sourceAddress;
}
byte[] groupAddress() {
return groupAddress;
}
int index() {
return index;
}
byte[] source() {
return sourceAddress;
}
}
}