Path: blob/master/src/java.base/share/classes/java/nio/channels/MembershipKey.java
41159 views
/*1* Copyright (c) 2007, 2013, 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 java.nio.channels;2627import java.net.InetAddress;28import java.net.NetworkInterface;29import java.io.IOException;3031/**32* A token representing the membership of an Internet Protocol (IP) multicast33* group.34*35* <p> A membership key may represent a membership to receive all datagrams sent36* to the group, or it may be <em>source-specific</em>, meaning that it37* represents a membership that receives only datagrams from a specific source38* address. Whether or not a membership key is source-specific may be determined39* by invoking its {@link #sourceAddress() sourceAddress} method.40*41* <p> A membership key is valid upon creation and remains valid until the42* membership is dropped by invoking the {@link #drop() drop} method, or43* the channel is closed. The validity of the membership key may be tested44* by invoking its {@link #isValid() isValid} method.45*46* <p> Where a membership key is not source-specific and the underlying operation47* system supports source filtering, then the {@link #block block} and {@link48* #unblock unblock} methods can be used to block or unblock multicast datagrams49* from particular source addresses.50*51* @see MulticastChannel52*53* @since 1.754*/55public abstract class MembershipKey {5657/**58* Initializes a new instance of this class.59*/60protected MembershipKey() {61}6263/**64* Tells whether or not this membership is valid.65*66* <p> A multicast group membership is valid upon creation and remains67* valid until the membership is dropped by invoking the {@link #drop() drop}68* method, or the channel is closed.69*70* @return {@code true} if this membership key is valid, {@code false}71* otherwise72*/73public abstract boolean isValid();7475/**76* Drop membership.77*78* <p> If the membership key represents a membership to receive all datagrams79* then the membership is dropped and the channel will no longer receive any80* datagrams sent to the group. If the membership key is source-specific81* then the channel will no longer receive datagrams sent to the group from82* that source address.83*84* <p> After membership is dropped it may still be possible to receive85* datagrams sent to the group. This can arise when datagrams are waiting to86* be received in the socket's receive buffer. After membership is dropped87* then the channel may {@link MulticastChannel#join join} the group again88* in which case a new membership key is returned.89*90* <p> Upon return, this membership object will be {@link #isValid() invalid}.91* If the multicast group membership is already invalid then invoking this92* method has no effect. Once a multicast group membership is invalid,93* it remains invalid forever.94*/95public abstract void drop();9697/**98* Block multicast datagrams from the given source address.99*100* <p> If this membership key is not source-specific, and the underlying101* operating system supports source filtering, then this method blocks102* multicast datagrams from the given source address. If the given source103* address is already blocked then this method has no effect.104* After a source address is blocked it may still be possible to receive105* datagrams from that source. This can arise when datagrams are waiting to106* be received in the socket's receive buffer.107*108* @param source109* The source address to block110*111* @return This membership key112*113* @throws IllegalArgumentException114* If the {@code source} parameter is not a unicast address or115* is not the same address type as the multicast group116* @throws IllegalStateException117* If this membership key is source-specific or is no longer valid118* @throws UnsupportedOperationException119* If the underlying operating system does not support source120* filtering121* @throws IOException122* If an I/O error occurs123*/124public abstract MembershipKey block(InetAddress source) throws IOException;125126/**127* Unblock multicast datagrams from the given source address that was128* previously blocked using the {@link #block(InetAddress) block} method.129*130* @param source131* The source address to unblock132*133* @return This membership key134*135* @throws IllegalStateException136* If the given source address is not currently blocked or the137* membership key is no longer valid138*/139public abstract MembershipKey unblock(InetAddress source);140141/**142* Returns the channel for which this membership key was created. This143* method will continue to return the channel even after the membership144* becomes {@link #isValid invalid}.145*146* @return the channel147*/148public abstract MulticastChannel channel();149150/**151* Returns the multicast group for which this membership key was created.152* This method will continue to return the group even after the membership153* becomes {@link #isValid invalid}.154*155* @return the multicast group156*/157public abstract InetAddress group();158159/**160* Returns the network interface for which this membership key was created.161* This method will continue to return the network interface even after the162* membership becomes {@link #isValid invalid}.163*164* @return the network interface165*/166public abstract NetworkInterface networkInterface();167168/**169* Returns the source address if this membership key is source-specific,170* or {@code null} if this membership is not source-specific.171*172* @return The source address if this membership key is source-specific,173* otherwise {@code null}174*/175public abstract InetAddress sourceAddress();176}177178179