|
1 /* |
|
2 * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. |
|
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 |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
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 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package java.nio.channels; |
|
27 |
|
28 import java.net.InetAddress; |
|
29 import java.net.NetworkInterface; |
|
30 import java.io.IOException; |
|
31 import java.util.List; |
|
32 |
|
33 /** |
|
34 * A token representing the membership of an Internet Protocol (IP) multicast |
|
35 * group. |
|
36 * |
|
37 * <p> A membership key may represent a membership to receive all datagrams sent |
|
38 * to the group, or it may be <em>source-specific</em>, meaning that it |
|
39 * represents a membership that receives only datagrams from a specific source |
|
40 * address. Whether or not a membership key is source-specific may be determined |
|
41 * by invoking its {@link #getSourceAddress() getSourceAddress} method. |
|
42 * |
|
43 * <p> A membership key is valid upon creation and remains valid until the |
|
44 * membership is dropped by invoking the {@link #drop() drop} method, or |
|
45 * the channel is closed. The validity of the membership key may be tested |
|
46 * by invoking its {@link #isValid() isValid} method. |
|
47 * |
|
48 * <p> Where a membership key is not source-specific and the underlying operation |
|
49 * system supports source filtering, then the {@link #block block} and {@link |
|
50 * #unblock unblock} methods can be used to block or unblock multicast datagrams |
|
51 * from particular source addresses. |
|
52 * |
|
53 * @see MulticastChannel |
|
54 * |
|
55 * @since 1.7 |
|
56 */ |
|
57 public abstract class MembershipKey { |
|
58 |
|
59 /** |
|
60 * Initializes a new instance of this class. |
|
61 */ |
|
62 protected MembershipKey() { |
|
63 } |
|
64 |
|
65 /** |
|
66 * Tells whether or not this membership is valid. |
|
67 * |
|
68 * <p> A multicast group membership is valid upon creation and remains |
|
69 * valid until the membership is dropped by invoking the {@link #drop() drop} |
|
70 * method, or the channel is closed. |
|
71 * |
|
72 * @return {@code true} if this membership key is valid, {@code false} |
|
73 * otherwise |
|
74 */ |
|
75 public abstract boolean isValid(); |
|
76 |
|
77 /** |
|
78 * Drop membership. |
|
79 * |
|
80 * <p> If the membership key represents a membership to receive all datagrams |
|
81 * then the membership is dropped and the channel will no longer receive any |
|
82 * datagrams sent to the group. If the membership key is source-specific |
|
83 * then the channel will no longer receive datagrams sent to the group from |
|
84 * that source address. |
|
85 * |
|
86 * <p> After membership is dropped it may still be possible to receive |
|
87 * datagrams sent to the group. This can arise when datagrams are waiting to |
|
88 * be received in the socket's receive buffer. After membership is dropped |
|
89 * then the channel may {@link MulticastChannel#join join} the group again |
|
90 * in which case a new membership key is returned. |
|
91 * |
|
92 * <p> Upon return, this membership object will be {@link #isValid() invalid}. |
|
93 * If the multicast group membership is already invalid then invoking this |
|
94 * method has no effect. Once a multicast group membership is invalid, |
|
95 * it remains invalid forever. |
|
96 * |
|
97 * @throws IOException |
|
98 * If an I/O error occurs |
|
99 */ |
|
100 public abstract void drop() throws IOException; |
|
101 |
|
102 /** |
|
103 * Block multicast datagrams from the given source address. |
|
104 * |
|
105 * <p> If this membership key is not source-specific, and the underlying |
|
106 * operating system supports source filtering, then this method blocks |
|
107 * multicast datagrams from the given source address. If the given source |
|
108 * address is already blocked then this method has no effect. |
|
109 * After a source address is blocked it may still be possible to receive |
|
110 * datagams from that source. This can arise when datagrams are waiting to |
|
111 * be received in the socket's receive buffer. |
|
112 * |
|
113 * @param source |
|
114 * The source address to block |
|
115 * |
|
116 * @return This membership key |
|
117 * |
|
118 * @throws IllegalArgumentException |
|
119 * If the {@code source} parameter is not a unicast address or |
|
120 * is not the same address type as the multicast group |
|
121 * @throws IllegalStateException |
|
122 * If this membership key is source-specific or is no longer valid |
|
123 * @throws UnsupportedOperationException |
|
124 * If the underlying operating system does not support source |
|
125 * filtering |
|
126 * @throws IOException |
|
127 * If an I/O error occurs |
|
128 */ |
|
129 public abstract MembershipKey block(InetAddress source) throws IOException; |
|
130 |
|
131 /** |
|
132 * Unblock multicast datagrams from the given source address that was |
|
133 * previously blocked using the {@link #block(InetAddress) block} method. |
|
134 * |
|
135 * @param source |
|
136 * The source address to unblock |
|
137 * |
|
138 * @return This membership key |
|
139 * |
|
140 * @throws IllegalStateException |
|
141 * If the given source address is not currently blocked or the |
|
142 * membership key is no longer valid |
|
143 * @throws IOException |
|
144 * If an I/O error occurs |
|
145 */ |
|
146 public abstract MembershipKey unblock(InetAddress source) throws IOException; |
|
147 |
|
148 /** |
|
149 * Returns the channel for which this membership key was created. This |
|
150 * method will continue to return the channel even after the membership |
|
151 * becomes {@link #isValid invalid}. |
|
152 * |
|
153 * @return the channel |
|
154 */ |
|
155 public abstract MulticastChannel getChannel(); |
|
156 |
|
157 /** |
|
158 * Returns the multicast group for which this membership key was created. |
|
159 * This method will continue to return the group even after the membership |
|
160 * becomes {@link #isValid invalid}. |
|
161 * |
|
162 * @return the multicast group |
|
163 */ |
|
164 public abstract InetAddress getGroup(); |
|
165 |
|
166 /** |
|
167 * Returns the network interface for which this membership key was created. |
|
168 * This method will continue to return the network interface even after the |
|
169 * membership becomes {@link #isValid invalid}. |
|
170 * |
|
171 * @return the network interface |
|
172 */ |
|
173 public abstract NetworkInterface getNetworkInterface(); |
|
174 |
|
175 /** |
|
176 * Returns the source address if this membership key is source-specific, |
|
177 * or {@code null} if this membership is not source-specific. |
|
178 * |
|
179 * @return The source address if this membership key is source-specific, |
|
180 * otherwise {@code null} |
|
181 */ |
|
182 public abstract InetAddress getSourceAddress(); |
|
183 } |