Path: blob/master/src/java.base/share/classes/sun/nio/ch/MembershipRegistry.java
41159 views
/*1* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.net.InetAddress;28import java.net.NetworkInterface;29import java.nio.channels.MembershipKey;30import java.util.HashMap;31import java.util.Iterator;32import java.util.LinkedList;33import java.util.List;34import java.util.Map;3536/**37* Simple registry of membership keys for a MulticastChannel.38*39* Instances of this object are not safe by multiple concurrent threads.40*/4142class MembershipRegistry {4344// map multicast group to list of keys45private Map<InetAddress, List<MembershipKeyImpl>> groups;4647MembershipRegistry() {48}4950/**51* Checks registry for membership of the group on the given52* network interface.53*/54MembershipKey checkMembership(InetAddress group, NetworkInterface interf,55InetAddress source)56{57if (groups != null) {58List<MembershipKeyImpl> keys = groups.get(group);59if (keys != null) {60for (MembershipKeyImpl key: keys) {61if (key.networkInterface().equals(interf)) {62// already a member to receive all packets so return63// existing key or detect conflict64if (source == null) {65if (key.sourceAddress() == null)66return key;67throw new IllegalStateException("Already a member to receive all packets");68}6970// already have source-specific membership so return key71// or detect conflict72if (key.sourceAddress() == null)73throw new IllegalStateException("Already have source-specific membership");74if (source.equals(key.sourceAddress()))75return key;76}77}78}79}80return null;81}8283/**84* Add membership to the registry, returning a new membership key.85*/86void add(MembershipKeyImpl key) {87InetAddress group = key.group();88List<MembershipKeyImpl> keys;89if (groups == null) {90groups = new HashMap<>();91keys = null;92} else {93keys = groups.get(group);94}95if (keys == null) {96keys = new LinkedList<>();97groups.put(group, keys);98}99keys.add(key);100}101102/**103* Remove a key from the registry104*/105void remove(MembershipKeyImpl key) {106InetAddress group = key.group();107List<MembershipKeyImpl> keys = groups.get(group);108if (keys != null) {109Iterator<MembershipKeyImpl> i = keys.iterator();110while (i.hasNext()) {111if (i.next() == key) {112i.remove();113break;114}115}116if (keys.isEmpty()) {117groups.remove(group);118}119}120}121122@FunctionalInterface123interface ThrowingConsumer<T, X extends Throwable> {124void accept(T action) throws X;125}126127/**128* Invoke an action for each key in the registry129*/130<X extends Throwable>131void forEach(ThrowingConsumer<MembershipKeyImpl, X> action) throws X {132if (groups != null) {133for (List<MembershipKeyImpl> keys : groups.values()) {134for (MembershipKeyImpl key : keys) {135action.accept(key);136}137}138}139}140141/**142* Invalidate all keys in the registry143*/144void invalidateAll() {145forEach(MembershipKeyImpl::invalidate);146}147}148149150