Showing the changes to java/pkg/java/nio/channels/AsynchronousServerSocketChannel.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,300 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2007, 2009, Oracle and/or its affiliates. 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. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package java.nio.channels; | |
| 27 | + | |
| 28 | +import java.nio.channels.spi.*; | |
| 29 | +import java.net.SocketOption; | |
| 30 | +import java.net.SocketAddress; | |
| 31 | +import java.util.concurrent.Future; | |
| 32 | +import java.io.IOException; | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * An asynchronous channel for stream-oriented listening sockets. | |
| 36 | + * | |
| 37 | + * <p> An asynchronous server-socket channel is created by invoking the | |
| 38 | + * {@link #open open} method of this class. | |
| 39 | + * A newly-created asynchronous server-socket channel is open but not yet bound. | |
| 40 | + * It can be bound to a local address and configured to listen for connections | |
| 41 | + * by invoking the {@link #bind(SocketAddress,int) bind} method. Once bound, | |
| 42 | + * the {@link #accept(Object,CompletionHandler) accept} method | |
| 43 | + * is used to initiate the accepting of connections to the channel's socket. | |
| 44 | + * An attempt to invoke the <tt>accept</tt> method on an unbound channel will | |
| 45 | + * cause a {@link NotYetBoundException} to be thrown. | |
| 46 | + * | |
| 47 | + * <p> Channels of this type are safe for use by multiple concurrent threads | |
| 48 | + * though at most one accept operation can be outstanding at any time. | |
| 49 | + * If a thread initiates an accept operation before a previous accept operation | |
| 50 | + * has completed then an {@link AcceptPendingException} will be thrown. | |
| 51 | + * | |
| 52 | + * <p> Socket options are configured using the {@link #setOption(SocketOption,Object) | |
| 53 | + * setOption} method. Channels of this type support the following options: | |
| 54 | + * <blockquote> | |
| 55 | + * <table border> | |
| 56 | + * <tr> | |
| 57 | + * <th>Option Name</th> | |
| 58 | + * <th>Description</th> | |
| 59 | + * </tr> | |
| 60 | + * <tr> | |
| 61 | + * <td> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </td> | |
| 62 | + * <td> The size of the socket receive buffer </td> | |
| 63 | + * </tr> | |
| 64 | + * <tr> | |
| 65 | + * <td> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </td> | |
| 66 | + * <td> Re-use address </td> | |
| 67 | + * </tr> | |
| 68 | + * </table> | |
| 69 | + * </blockquote> | |
| 70 | + * Additional (implementation specific) options may also be supported. | |
| 71 | + * | |
| 72 | + * <p> <b>Usage Example:</b> | |
| 73 | + * <pre> | |
| 74 | + * final AsynchronousServerSocketChannel listener = | |
| 75 | + * AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(5000)); | |
| 76 | + * | |
| 77 | + * listener.accept(null, new CompletionHandler<AsynchronousSocketChannel,Void>() { | |
| 78 | + * public void completed(AsynchronousSocketChannel ch, Void att) { | |
| 79 | + * // accept the next connection | |
| 80 | + * listener.accept(null, this); | |
| 81 | + * | |
| 82 | + * // handle this connection | |
| 83 | + * handle(ch); | |
| 84 | + * } | |
| 85 | + * public void failed(Throwable exc, Void att) { | |
| 86 | + * ... | |
| 87 | + * } | |
| 88 | + * }); | |
| 89 | + * </pre> | |
| 90 | + * | |
| 91 | + * @since 1.7 | |
| 92 | + */ | |
| 93 | + | |
| 94 | +public abstract class AsynchronousServerSocketChannel | |
| 95 | + implements AsynchronousChannel, NetworkChannel | |
| 96 | +{ | |
| 97 | + private final AsynchronousChannelProvider provider; | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * Initializes a new instance of this class. | |
| 101 | + */ | |
| 102 | + protected AsynchronousServerSocketChannel(AsynchronousChannelProvider provider) { | |
| 103 | + this.provider = provider; | |
| 104 | + } | |
| 105 | + | |
| 106 | + /** | |
| 107 | + * Returns the provider that created this channel. | |
| 108 | + */ | |
| 109 | + public final AsynchronousChannelProvider provider() { | |
| 110 | + return provider; | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Opens an asynchronous server-socket channel. | |
| 115 | + * | |
| 116 | + * <p> The new channel is created by invoking the {@link | |
| 117 | + * java.nio.channels.spi.AsynchronousChannelProvider#openAsynchronousServerSocketChannel | |
| 118 | + * openAsynchronousServerSocketChannel} method on the {@link | |
| 119 | + * java.nio.channels.spi.AsynchronousChannelProvider} object that created | |
| 120 | + * the given group. If the group parameter is <tt>null</tt> then the | |
| 121 | + * resulting channel is created by the system-wide default provider, and | |
| 122 | + * bound to the <em>default group</em>. | |
| 123 | + * | |
| 124 | + * @param group | |
| 125 | + * The group to which the newly constructed channel should be bound, | |
| 126 | + * or <tt>null</tt> for the default group | |
| 127 | + * | |
| 128 | + * @return A new asynchronous server socket channel | |
| 129 | + * | |
| 130 | + * @throws ShutdownChannelGroupException | |
| 131 | + * If the channel group is shutdown | |
| 132 | + * @throws IOException | |
| 133 | + * If an I/O error occurs | |
| 134 | + */ | |
| 135 | + public static AsynchronousServerSocketChannel open(AsynchronousChannelGroup group) | |
| 136 | + throws IOException | |
| 137 | + { | |
| 138 | + AsynchronousChannelProvider provider = (group == null) ? | |
| 139 | + AsynchronousChannelProvider.provider() : group.provider(); | |
| 140 | + return provider.openAsynchronousServerSocketChannel(group); | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Opens an asynchronous server-socket channel. | |
| 145 | + * | |
| 146 | + * <p> This method returns an asynchronous server socket channel that is | |
| 147 | + * bound to the <em>default group</em>. This method is equivalent to evaluating | |
| 148 | + * the expression: | |
| 149 | + * <blockquote><pre> | |
| 150 | + * open((AsynchronousChannelGroup)null); | |
| 151 | + * </pre></blockquote> | |
| 152 | + * | |
| 153 | + * @return A new asynchronous server socket channel | |
| 154 | + * | |
| 155 | + * @throws IOException | |
| 156 | + * If an I/O error occurs | |
| 157 | + */ | |
| 158 | + public static AsynchronousServerSocketChannel open() | |
| 159 | + throws IOException | |
| 160 | + { | |
| 161 | + return open(null); | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * Binds the channel's socket to a local address and configures the socket to | |
| 166 | + * listen for connections. | |
| 167 | + * | |
| 168 | + * <p> An invocation of this method is equivalent to the following: | |
| 169 | + * <blockquote><pre> | |
| 170 | + * bind(local, 0); | |
| 171 | + * </pre></blockquote> | |
| 172 | + * | |
| 173 | + * @param local | |
| 174 | + * The local address to bind the socket, or <tt>null</tt> to bind | |
| 175 | + * to an automatically assigned socket address | |
| 176 | + * | |
| 177 | + * @return This channel | |
| 178 | + * | |
| 179 | + * @throws AlreadyBoundException {@inheritDoc} | |
| 180 | + * @throws UnsupportedAddressTypeException {@inheritDoc} | |
| 181 | + * @throws SecurityException {@inheritDoc} | |
| 182 | + * @throws ClosedChannelException {@inheritDoc} | |
| 183 | + * @throws IOException {@inheritDoc} | |
| 184 | + */ | |
| 185 | + public final AsynchronousServerSocketChannel bind(SocketAddress local) | |
| 186 | + throws IOException | |
| 187 | + { | |
| 188 | + return bind(local, 0); | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Binds the channel's socket to a local address and configures the socket to | |
| 193 | + * listen for connections. | |
| 194 | + * | |
| 195 | + * <p> This method is used to establish an association between the socket and | |
| 196 | + * a local address. Once an association is established then the socket remains | |
| 197 | + * bound until the associated channel is closed. | |
| 198 | + * | |
| 199 | + * <p> The {@code backlog} parameter is the maximum number of pending | |
| 200 | + * connections on the socket. Its exact semantics are implementation specific. | |
| 201 | + * In particular, an implementation may impose a maximum length or may choose | |
| 202 | + * to ignore the parameter altogther. If the {@code backlog} parameter has | |
| 203 | + * the value {@code 0}, or a negative value, then an implementation specific | |
| 204 | + * default is used. | |
| 205 | + * | |
| 206 | + * @param local | |
| 207 | + * The local address to bind the socket, or {@code null} to bind | |
| 208 | + * to an automatically assigned socket address | |
| 209 | + * @param backlog | |
| 210 | + * The maximum number of pending connections | |
| 211 | + * | |
| 212 | + * @return This channel | |
| 213 | + * | |
| 214 | + * @throws AlreadyBoundException | |
| 215 | + * If the socket is already bound | |
| 216 | + * @throws UnsupportedAddressTypeException | |
| 217 | + * If the type of the given address is not supported | |
| 218 | + * @throws SecurityException | |
| 219 | + * If a security manager has been installed and its {@link | |
| 220 | + * SecurityManager#checkListen checkListen} method denies the operation | |
| 221 | + * @throws ClosedChannelException | |
| 222 | + * If the channel is closed | |
| 223 | + * @throws IOException | |
| 224 | + * If some other I/O error occurs | |
| 225 | + */ | |
| 226 | + public abstract AsynchronousServerSocketChannel bind(SocketAddress local, int backlog) | |
| 227 | + throws IOException; | |
| 228 | + | |
| 229 | + /** | |
| 230 | + * @throws IllegalArgumentException {@inheritDoc} | |
| 231 | + * @throws ClosedChannelException {@inheritDoc} | |
| 232 | + * @throws IOException {@inheritDoc} | |
| 233 | + */ | |
| 234 | + public abstract <T> AsynchronousServerSocketChannel setOption(SocketOption<T> name, T value) | |
| 235 | + throws IOException; | |
| 236 | + | |
| 237 | + /** | |
| 238 | + * Accepts a connection. | |
| 239 | + * | |
| 240 | + * <p> This method initiates an asynchronous operation to accept a | |
| 241 | + * connection made to this channel's socket. The {@code handler} parameter is | |
| 242 | + * a completion handler that is invoked when a connection is accepted (or | |
| 243 | + * the operation fails). The result passed to the completion handler is | |
| 244 | + * the {@link AsynchronousSocketChannel} to the new connection. | |
| 245 | + * | |
| 246 | + * <p> When a new connection is accepted then the resulting {@code | |
| 247 | + * AsynchronousSocketChannel} will be bound to the same {@link | |
| 248 | + * AsynchronousChannelGroup} as this channel. If the group is {@link | |
| 249 | + * AsynchronousChannelGroup#isShutdown shutdown} and a connection is accepted, | |
| 250 | + * then the connection is closed, and the operation completes with an {@code | |
| 251 | + * IOException} and cause {@link ShutdownChannelGroupException}. | |
| 252 | + * | |
| 253 | + * <p> To allow for concurrent handling of new connections, the completion | |
| 254 | + * handler is not invoked directly by the initiating thread when a new | |
| 255 | + * connection is accepted immediately (see <a | |
| 256 | + * href="AsynchronousChannelGroup.html#threading">Threading<a>). | |
| 257 | + * | |
| 258 | + * <p> If a security manager has been installed then it verifies that the | |
| 259 | + * address and port number of the connection's remote endpoint are permitted | |
| 260 | + * by the security manager's {@link SecurityManager#checkAccept checkAccept} | |
| 261 | + * method. The permission check is performed with privileges that are restricted | |
| 262 | + * by the calling context of this method. If the permission check fails then | |
| 263 | + * the connection is closed and the operation completes with a {@link | |
| 264 | + * SecurityException}. | |
| 265 | + * | |
| 266 | + * @param attachment | |
| 267 | + * The object to attach to the I/O operation; can be {@code null} | |
| 268 | + * @param handler | |
| 269 | + * The handler for consuming the result | |
| 270 | + * | |
| 271 | + * @throws AcceptPendingException | |
| 272 | + * If an accept operation is already in progress on this channel | |
| 273 | + * @throws NotYetBoundException | |
| 274 | + * If this channel's socket has not yet been bound | |
| 275 | + * @throws ShutdownChannelGroupException | |
| 276 | + * If the channel group has terminated | |
| 277 | + */ | |
| 278 | + public abstract <A> void accept(A attachment, | |
| 279 | + CompletionHandler<AsynchronousSocketChannel,? super A> handler); | |
| 280 | + | |
| 281 | + /** | |
| 282 | + * Accepts a connection. | |
| 283 | + * | |
| 284 | + * <p> This method initiates an asynchronous operation to accept a | |
| 285 | + * connection made to this channel's socket. The method behaves in exactly | |
| 286 | + * the same manner as the {@link #accept(Object, CompletionHandler)} method | |
| 287 | + * except that instead of specifying a completion handler, this method | |
| 288 | + * returns a {@code Future} representing the pending result. The {@code | |
| 289 | + * Future}'s {@link Future#get() get} method returns the {@link | |
| 290 | + * AsynchronousSocketChannel} to the new connection on successful completion. | |
| 291 | + * | |
| 292 | + * @return a {@code Future} object representing the pending result | |
| 293 | + * | |
| 294 | + * @throws AcceptPendingException | |
| 295 | + * If an accept operation is already in progress on this channel | |
| 296 | + * @throws NotYetBoundException | |
| 297 | + * If this channel's socket has not yet been bound | |
| 298 | + */ | |
| 299 | + public abstract Future<AsynchronousSocketChannel> accept(); | |
| 300 | +} | |