author | alanb |
Sat, 30 Nov 2019 16:21:19 +0000 | |
changeset 59329 | 289000934908 |
parent 59146 | 455612b3161a |
permissions | -rw-r--r-- |
1152 | 1 |
/* |
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2008, 2019, 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.net.InetAddress; |
|
29 |
import java.net.NetworkInterface; |
|
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
30 |
import java.nio.channels.MembershipKey; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
31 |
import java.util.HashMap; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
32 |
import java.util.Iterator; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
33 |
import java.util.LinkedList; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
34 |
import java.util.List; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
35 |
import java.util.Map; |
1152 | 36 |
|
37 |
/** |
|
38 |
* Simple registry of membership keys for a MulticastChannel. |
|
39 |
* |
|
40 |
* Instances of this object are not safe by multiple concurrent threads. |
|
41 |
*/ |
|
42 |
||
43 |
class MembershipRegistry { |
|
44 |
||
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
45 |
// map multicast group to list of keys |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
46 |
private Map<InetAddress, List<MembershipKeyImpl>> groups; |
1152 | 47 |
|
48 |
MembershipRegistry() { |
|
49 |
} |
|
50 |
||
51 |
/** |
|
52 |
* Checks registry for membership of the group on the given |
|
53 |
* network interface. |
|
54 |
*/ |
|
55 |
MembershipKey checkMembership(InetAddress group, NetworkInterface interf, |
|
56 |
InetAddress source) |
|
57 |
{ |
|
58 |
if (groups != null) { |
|
59 |
List<MembershipKeyImpl> keys = groups.get(group); |
|
60 |
if (keys != null) { |
|
61 |
for (MembershipKeyImpl key: keys) { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
62 |
if (key.networkInterface().equals(interf)) { |
1152 | 63 |
// already a member to receive all packets so return |
64 |
// existing key or detect conflict |
|
65 |
if (source == null) { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
66 |
if (key.sourceAddress() == null) |
1152 | 67 |
return key; |
68 |
throw new IllegalStateException("Already a member to receive all packets"); |
|
69 |
} |
|
70 |
||
71 |
// already have source-specific membership so return key |
|
72 |
// or detect conflict |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
73 |
if (key.sourceAddress() == null) |
1152 | 74 |
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
|
75 |
if (source.equals(key.sourceAddress())) |
1152 | 76 |
return key; |
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
return null; |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Add membership to the registry, returning a new membership key. |
|
86 |
*/ |
|
87 |
void add(MembershipKeyImpl key) { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
88 |
InetAddress group = key.group(); |
1152 | 89 |
List<MembershipKeyImpl> keys; |
90 |
if (groups == null) { |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
91 |
groups = new HashMap<>(); |
1152 | 92 |
keys = null; |
93 |
} else { |
|
94 |
keys = groups.get(group); |
|
95 |
} |
|
96 |
if (keys == null) { |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
97 |
keys = new LinkedList<>(); |
1152 | 98 |
groups.put(group, keys); |
99 |
} |
|
100 |
keys.add(key); |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Remove a key from the registry |
|
105 |
*/ |
|
106 |
void remove(MembershipKeyImpl key) { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
107 |
InetAddress group = key.group(); |
1152 | 108 |
List<MembershipKeyImpl> keys = groups.get(group); |
109 |
if (keys != null) { |
|
110 |
Iterator<MembershipKeyImpl> i = keys.iterator(); |
|
111 |
while (i.hasNext()) { |
|
112 |
if (i.next() == key) { |
|
113 |
i.remove(); |
|
114 |
break; |
|
115 |
} |
|
116 |
} |
|
117 |
if (keys.isEmpty()) { |
|
118 |
groups.remove(group); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
||
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
123 |
@FunctionalInterface |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
124 |
interface ThrowingConsumer<T, X extends Throwable> { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
125 |
void accept(T action) throws X; |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
126 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
127 |
|
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
128 |
/** |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
129 |
* Invoke an action for each key in the registry |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
130 |
*/ |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
131 |
<X extends Throwable> |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
132 |
void forEach(ThrowingConsumer<MembershipKeyImpl, X> action) throws X { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
133 |
if (groups != null) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
134 |
for (List<MembershipKeyImpl> keys : groups.values()) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
135 |
for (MembershipKeyImpl key : keys) { |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
136 |
action.accept(key); |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
137 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
138 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
139 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
140 |
} |
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
141 |
|
1152 | 142 |
/** |
143 |
* Invalidate all keys in the registry |
|
144 |
*/ |
|
145 |
void invalidateAll() { |
|
59146
455612b3161a
8231259: (dc) DatagramChannel::disconnect re-binds socket to the wildcard address (macOS)
alanb
parents:
47216
diff
changeset
|
146 |
forEach(MembershipKeyImpl::invalidate); |
1152 | 147 |
} |
148 |
} |