Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/channels/MulticastChannel.java
38918 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;30import java.net.ProtocolFamily; // javadoc31import java.net.StandardProtocolFamily; // javadoc32import java.net.StandardSocketOptions; // javadoc3334/**35* A network channel that supports Internet Protocol (IP) multicasting.36*37* <p> IP multicasting is the transmission of IP datagrams to members of38* a <em>group</em> that is zero or more hosts identified by a single destination39* address.40*41* <p> In the case of a channel to an {@link StandardProtocolFamily#INET IPv4} socket,42* the underlying operating system supports <a href="http://www.ietf.org/rfc/rfc2236.txt">43* <i>RFC 2236: Internet Group Management Protocol, Version 2 (IGMPv2)</i></a>.44* It may optionally support source filtering as specified by <a45* href="http://www.ietf.org/rfc/rfc3376.txt"> <i>RFC 3376: Internet Group46* Management Protocol, Version 3 (IGMPv3)</i></a>.47* For channels to an {@link StandardProtocolFamily#INET6 IPv6} socket, the equivalent48* standards are <a href="http://www.ietf.org/rfc/rfc2710.txt"> <i>RFC 2710:49* Multicast Listener Discovery (MLD) for IPv6</i></a> and <a50* href="http://www.ietf.org/rfc/rfc3810.txt"> <i>RFC 3810: Multicast Listener51* Discovery Version 2 (MLDv2) for IPv6</i></a>.52*53* <p> The {@link #join(InetAddress,NetworkInterface)} method is used to54* join a group and receive all multicast datagrams sent to the group. A channel55* may join several multicast groups and may join the same group on several56* {@link NetworkInterface interfaces}. Membership is dropped by invoking the {@link57* MembershipKey#drop drop} method on the returned {@link MembershipKey}. If the58* underlying platform supports source filtering then the {@link MembershipKey#block59* block} and {@link MembershipKey#unblock unblock} methods can be used to block or60* unblock multicast datagrams from particular source addresses.61*62* <p> The {@link #join(InetAddress,NetworkInterface,InetAddress)} method63* is used to begin receiving datagrams sent to a group whose source address matches64* a given source address. This method throws {@link UnsupportedOperationException}65* if the underlying platform does not support source filtering. Membership is66* <em>cumulative</em> and this method may be invoked again with the same group67* and interface to allow receiving datagrams from other source addresses. The68* method returns a {@link MembershipKey} that represents membership to receive69* datagrams from the given source address. Invoking the key's {@link70* MembershipKey#drop drop} method drops membership so that datagrams from the71* source address can no longer be received.72*73* <h2>Platform dependencies</h2>74*75* The multicast implementation is intended to map directly to the native76* multicasting facility. Consequently, the following items should be considered77* when developing an application that receives IP multicast datagrams:78*79* <ol>80*81* <li><p> The creation of the channel should specify the {@link ProtocolFamily}82* that corresponds to the address type of the multicast groups that the channel83* will join. There is no guarantee that a channel to a socket in one protocol84* family can join and receive multicast datagrams when the address of the85* multicast group corresponds to another protocol family. For example, it is86* implementation specific if a channel to an {@link StandardProtocolFamily#INET6 IPv6}87* socket can join an {@link StandardProtocolFamily#INET IPv4} multicast group and receive88* multicast datagrams sent to the group. </p></li>89*90* <li><p> The channel's socket should be bound to the {@link91* InetAddress#isAnyLocalAddress wildcard} address. If the socket is bound to92* a specific address, rather than the wildcard address then it is implementation93* specific if multicast datagrams are received by the socket. </p></li>94*95* <li><p> The {@link StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} option should be96* enabled prior to {@link NetworkChannel#bind binding} the socket. This is97* required to allow multiple members of the group to bind to the same98* address. </p></li>99*100* </ol>101*102* <p> <b>Usage Example:</b>103* <pre>104* // join multicast group on this interface, and also use this105* // interface for outgoing multicast datagrams106* NetworkInterface ni = NetworkInterface.getByName("hme0");107*108* DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET)109* .setOption(StandardSocketOptions.SO_REUSEADDR, true)110* .bind(new InetSocketAddress(5000))111* .setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);112*113* InetAddress group = InetAddress.getByName("225.4.5.6");114*115* MembershipKey key = dc.join(group, ni);116* </pre>117*118* @since 1.7119*/120121public interface MulticastChannel122extends NetworkChannel123{124/**125* Closes this channel.126*127* <p> If the channel is a member of a multicast group then the membership128* is {@link MembershipKey#drop dropped}. Upon return, the {@link129* MembershipKey membership-key} will be {@link MembershipKey#isValid130* invalid}.131*132* <p> This method otherwise behaves exactly as specified by the {@link133* Channel} interface.134*135* @throws IOException136* If an I/O error occurs137*/138@Override void close() throws IOException;139140/**141* Joins a multicast group to begin receiving all datagrams sent to the group,142* returning a membership key.143*144* <p> If this channel is currently a member of the group on the given145* interface to receive all datagrams then the membership key, representing146* that membership, is returned. Otherwise this channel joins the group and147* the resulting new membership key is returned. The resulting membership key148* is not {@link MembershipKey#sourceAddress source-specific}.149*150* <p> A multicast channel may join several multicast groups, including151* the same group on more than one interface. An implementation may impose a152* limit on the number of groups that may be joined at the same time.153*154* @param group155* The multicast address to join156* @param interf157* The network interface on which to join the group158*159* @return The membership key160*161* @throws IllegalArgumentException162* If the group parameter is not a {@link InetAddress#isMulticastAddress163* multicast} address, or the group parameter is an address type164* that is not supported by this channel165* @throws IllegalStateException166* If the channel already has source-specific membership of the167* group on the interface168* @throws UnsupportedOperationException169* If the channel's socket is not an Internet Protocol socket170* @throws ClosedChannelException171* If this channel is closed172* @throws IOException173* If an I/O error occurs174* @throws SecurityException175* If a security manager is set, and its176* {@link SecurityManager#checkMulticast(InetAddress) checkMulticast}177* method denies access to the multiast group178*/179MembershipKey join(InetAddress group, NetworkInterface interf)180throws IOException;181182/**183* Joins a multicast group to begin receiving datagrams sent to the group184* from a given source address.185*186* <p> If this channel is currently a member of the group on the given187* interface to receive datagrams from the given source address then the188* membership key, representing that membership, is returned. Otherwise this189* channel joins the group and the resulting new membership key is returned.190* The resulting membership key is {@link MembershipKey#sourceAddress191* source-specific}.192*193* <p> Membership is <em>cumulative</em> and this method may be invoked194* again with the same group and interface to allow receiving datagrams sent195* by other source addresses to the group.196*197* @param group198* The multicast address to join199* @param interf200* The network interface on which to join the group201* @param source202* The source address203*204* @return The membership key205*206* @throws IllegalArgumentException207* If the group parameter is not a {@link208* InetAddress#isMulticastAddress multicast} address, the209* source parameter is not a unicast address, the group210* parameter is an address type that is not supported by this channel,211* or the source parameter is not the same address type as the group212* @throws IllegalStateException213* If the channel is currently a member of the group on the given214* interface to receive all datagrams215* @throws UnsupportedOperationException216* If the channel's socket is not an Internet Protocol socket or217* source filtering is not supported218* @throws ClosedChannelException219* If this channel is closed220* @throws IOException221* If an I/O error occurs222* @throws SecurityException223* If a security manager is set, and its224* {@link SecurityManager#checkMulticast(InetAddress) checkMulticast}225* method denies access to the multiast group226*/227MembershipKey join(InetAddress group, NetworkInterface interf, InetAddress source)228throws IOException;229}230231232