author | rriggs |
Fri, 01 Dec 2017 16:40:08 -0500 | |
changeset 48224 | be0df5ab3093 |
parent 47216 | 71c04702a3d5 |
child 59146 | 455612b3161a |
permissions | -rw-r--r-- |
1152 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. |
1152 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
1152 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
1152 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
1152 | 24 |
*/ |
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.nio.channels.*; |
|
29 |
import java.net.InetAddress; |
|
30 |
import java.net.NetworkInterface; |
|
31 |
import java.util.*; |
|
32 |
||
33 |
/** |
|
34 |
* Simple registry of membership keys for a MulticastChannel. |
|
35 |
* |
|
36 |
* Instances of this object are not safe by multiple concurrent threads. |
|
37 |
*/ |
|
38 |
||
39 |
class MembershipRegistry { |
|
40 |
||
41 |
// map multicast group to keys |
|
42 |
private Map<InetAddress,List<MembershipKeyImpl>> groups = null; |
|
43 |
||
44 |
MembershipRegistry() { |
|
45 |
} |
|
46 |
||
47 |
/** |
|
48 |
* Checks registry for membership of the group on the given |
|
49 |
* network interface. |
|
50 |
*/ |
|
51 |
MembershipKey checkMembership(InetAddress group, NetworkInterface interf, |
|
52 |
InetAddress source) |
|
53 |
{ |
|
54 |
if (groups != null) { |
|
55 |
List<MembershipKeyImpl> keys = groups.get(group); |
|
56 |
if (keys != null) { |
|
57 |
for (MembershipKeyImpl key: keys) { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
58 |
if (key.networkInterface().equals(interf)) { |
1152 | 59 |
// already a member to receive all packets so return |
60 |
// existing key or detect conflict |
|
61 |
if (source == null) { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
62 |
if (key.sourceAddress() == null) |
1152 | 63 |
return key; |
64 |
throw new IllegalStateException("Already a member to receive all packets"); |
|
65 |
} |
|
66 |
||
67 |
// already have source-specific membership so return key |
|
68 |
// or detect conflict |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
69 |
if (key.sourceAddress() == null) |
1152 | 70 |
throw new IllegalStateException("Already have source-specific membership"); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
71 |
if (source.equals(key.sourceAddress())) |
1152 | 72 |
return key; |
73 |
} |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
return null; |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Add membership to the registry, returning a new membership key. |
|
82 |
*/ |
|
83 |
void add(MembershipKeyImpl key) { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
84 |
InetAddress group = key.group(); |
1152 | 85 |
List<MembershipKeyImpl> keys; |
86 |
if (groups == null) { |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
87 |
groups = new HashMap<>(); |
1152 | 88 |
keys = null; |
89 |
} else { |
|
90 |
keys = groups.get(group); |
|
91 |
} |
|
92 |
if (keys == null) { |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
93 |
keys = new LinkedList<>(); |
1152 | 94 |
groups.put(group, keys); |
95 |
} |
|
96 |
keys.add(key); |
|
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* Remove a key from the registry |
|
101 |
*/ |
|
102 |
void remove(MembershipKeyImpl key) { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
103 |
InetAddress group = key.group(); |
1152 | 104 |
List<MembershipKeyImpl> keys = groups.get(group); |
105 |
if (keys != null) { |
|
106 |
Iterator<MembershipKeyImpl> i = keys.iterator(); |
|
107 |
while (i.hasNext()) { |
|
108 |
if (i.next() == key) { |
|
109 |
i.remove(); |
|
110 |
break; |
|
111 |
} |
|
112 |
} |
|
113 |
if (keys.isEmpty()) { |
|
114 |
groups.remove(group); |
|
115 |
} |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Invalidate all keys in the registry |
|
121 |
*/ |
|
122 |
void invalidateAll() { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
123 |
if (groups != null) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
124 |
for (InetAddress group: groups.keySet()) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
125 |
for (MembershipKeyImpl key: groups.get(group)) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
126 |
key.invalidate(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
127 |
} |
1152 | 128 |
} |
129 |
} |
|
130 |
} |
|
131 |
} |