Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/ch/MembershipRegistry.java
38918 views
/*1* Copyright (c) 2008, 2009, 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.nio.channels.*;28import java.net.InetAddress;29import java.net.NetworkInterface;30import java.util.*;3132/**33* Simple registry of membership keys for a MulticastChannel.34*35* Instances of this object are not safe by multiple concurrent threads.36*/3738class MembershipRegistry {3940// map multicast group to keys41private Map<InetAddress,List<MembershipKeyImpl>> groups = null;4243MembershipRegistry() {44}4546/**47* Checks registry for membership of the group on the given48* network interface.49*/50MembershipKey checkMembership(InetAddress group, NetworkInterface interf,51InetAddress source)52{53if (groups != null) {54List<MembershipKeyImpl> keys = groups.get(group);55if (keys != null) {56for (MembershipKeyImpl key: keys) {57if (key.networkInterface().equals(interf)) {58// already a member to receive all packets so return59// existing key or detect conflict60if (source == null) {61if (key.sourceAddress() == null)62return key;63throw new IllegalStateException("Already a member to receive all packets");64}6566// already have source-specific membership so return key67// or detect conflict68if (key.sourceAddress() == null)69throw new IllegalStateException("Already have source-specific membership");70if (source.equals(key.sourceAddress()))71return key;72}73}74}75}76return null;77}7879/**80* Add membership to the registry, returning a new membership key.81*/82void add(MembershipKeyImpl key) {83InetAddress group = key.group();84List<MembershipKeyImpl> keys;85if (groups == null) {86groups = new HashMap<InetAddress,List<MembershipKeyImpl>>();87keys = null;88} else {89keys = groups.get(group);90}91if (keys == null) {92keys = new LinkedList<MembershipKeyImpl>();93groups.put(group, keys);94}95keys.add(key);96}9798/**99* Remove a key from the registry100*/101void remove(MembershipKeyImpl key) {102InetAddress group = key.group();103List<MembershipKeyImpl> keys = groups.get(group);104if (keys != null) {105Iterator<MembershipKeyImpl> i = keys.iterator();106while (i.hasNext()) {107if (i.next() == key) {108i.remove();109break;110}111}112if (keys.isEmpty()) {113groups.remove(group);114}115}116}117118/**119* Invalidate all keys in the registry120*/121void invalidateAll() {122if (groups != null) {123for (InetAddress group: groups.keySet()) {124for (MembershipKeyImpl key: groups.get(group)) {125key.invalidate();126}127}128}129}130}131132133