893N/A/*
2362N/A * Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage java.nio.channels;
893N/A
893N/Aimport java.nio.channels.spi.*;
893N/Aimport java.net.SocketOption;
893N/Aimport java.net.SocketAddress;
893N/Aimport java.util.concurrent.Future;
893N/Aimport java.io.IOException;
893N/A
893N/A/**
893N/A * An asynchronous channel for stream-oriented listening sockets.
893N/A *
893N/A * <p> An asynchronous server-socket channel is created by invoking the
893N/A * {@link #open open} method of this class.
893N/A * A newly-created asynchronous server-socket channel is open but not yet bound.
893N/A * It can be bound to a local address and configured to listen for connections
893N/A * by invoking the {@link #bind(SocketAddress,int) bind} method. Once bound,
893N/A * the {@link #accept(Object,CompletionHandler) accept} method
893N/A * is used to initiate the accepting of connections to the channel's socket.
893N/A * An attempt to invoke the <tt>accept</tt> method on an unbound channel will
893N/A * cause a {@link NotYetBoundException} to be thrown.
893N/A *
893N/A * <p> Channels of this type are safe for use by multiple concurrent threads
893N/A * though at most one accept operation can be outstanding at any time.
893N/A * If a thread initiates an accept operation before a previous accept operation
893N/A * has completed then an {@link AcceptPendingException} will be thrown.
893N/A *
893N/A * <p> Socket options are configured using the {@link #setOption(SocketOption,Object)
893N/A * setOption} method. Channels of this type support the following options:
893N/A * <blockquote>
893N/A * <table border>
893N/A * <tr>
893N/A * <th>Option Name</th>
893N/A * <th>Description</th>
893N/A * </tr>
893N/A * <tr>
4216N/A * <td> {@link java.net.StandardSocketOptions#SO_RCVBUF SO_RCVBUF} </td>
893N/A * <td> The size of the socket receive buffer </td>
893N/A * </tr>
893N/A * <tr>
4216N/A * <td> {@link java.net.StandardSocketOptions#SO_REUSEADDR SO_REUSEADDR} </td>
893N/A * <td> Re-use address </td>
893N/A * </tr>
893N/A * </table>
893N/A * </blockquote>
893N/A * Additional (implementation specific) options may also be supported.
893N/A *
893N/A * <p> <b>Usage Example:</b>
893N/A * <pre>
893N/A * final AsynchronousServerSocketChannel listener =
893N/A * AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(5000));
893N/A *
893N/A * listener.accept(null, new CompletionHandler&lt;AsynchronousSocketChannel,Void&gt;() {
893N/A * public void completed(AsynchronousSocketChannel ch, Void att) {
893N/A * // accept the next connection
893N/A * listener.accept(null, this);
893N/A *
893N/A * // handle this connection
893N/A * handle(ch);
893N/A * }
893N/A * public void failed(Throwable exc, Void att) {
893N/A * ...
893N/A * }
893N/A * });
893N/A * </pre>
893N/A *
893N/A * @since 1.7
893N/A */
893N/A
893N/Apublic abstract class AsynchronousServerSocketChannel
893N/A implements AsynchronousChannel, NetworkChannel
893N/A{
893N/A private final AsynchronousChannelProvider provider;
893N/A
893N/A /**
893N/A * Initializes a new instance of this class.
893N/A */
893N/A protected AsynchronousServerSocketChannel(AsynchronousChannelProvider provider) {
893N/A this.provider = provider;
893N/A }
893N/A
893N/A /**
893N/A * Returns the provider that created this channel.
893N/A */
893N/A public final AsynchronousChannelProvider provider() {
893N/A return provider;
893N/A }
893N/A
893N/A /**
893N/A * Opens an asynchronous server-socket channel.
893N/A *
893N/A * <p> The new channel is created by invoking the {@link
893N/A * java.nio.channels.spi.AsynchronousChannelProvider#openAsynchronousServerSocketChannel
893N/A * openAsynchronousServerSocketChannel} method on the {@link
893N/A * java.nio.channels.spi.AsynchronousChannelProvider} object that created
893N/A * the given group. If the group parameter is <tt>null</tt> then the
893N/A * resulting channel is created by the system-wide default provider, and
893N/A * bound to the <em>default group</em>.
893N/A *
893N/A * @param group
893N/A * The group to which the newly constructed channel should be bound,
893N/A * or <tt>null</tt> for the default group
893N/A *
893N/A * @return A new asynchronous server socket channel
893N/A *
893N/A * @throws ShutdownChannelGroupException
893N/A * If the channel group is shutdown
893N/A * @throws IOException
893N/A * If an I/O error occurs
893N/A */
893N/A public static AsynchronousServerSocketChannel open(AsynchronousChannelGroup group)
893N/A throws IOException
893N/A {
893N/A AsynchronousChannelProvider provider = (group == null) ?
893N/A AsynchronousChannelProvider.provider() : group.provider();
893N/A return provider.openAsynchronousServerSocketChannel(group);
893N/A }
893N/A
893N/A /**
893N/A * Opens an asynchronous server-socket channel.
893N/A *
893N/A * <p> This method returns an asynchronous server socket channel that is
893N/A * bound to the <em>default group</em>. This method is equivalent to evaluating
893N/A * the expression:
893N/A * <blockquote><pre>
893N/A * open((AsynchronousChannelGroup)null);
893N/A * </pre></blockquote>
893N/A *
893N/A * @return A new asynchronous server socket channel
893N/A *
893N/A * @throws IOException
893N/A * If an I/O error occurs
893N/A */
893N/A public static AsynchronousServerSocketChannel open()
893N/A throws IOException
893N/A {
893N/A return open(null);
893N/A }
893N/A
893N/A /**
893N/A * Binds the channel's socket to a local address and configures the socket to
893N/A * listen for connections.
893N/A *
893N/A * <p> An invocation of this method is equivalent to the following:
893N/A * <blockquote><pre>
893N/A * bind(local, 0);
893N/A * </pre></blockquote>
893N/A *
893N/A * @param local
893N/A * The local address to bind the socket, or <tt>null</tt> to bind
893N/A * to an automatically assigned socket address
893N/A *
893N/A * @return This channel
893N/A *
893N/A * @throws AlreadyBoundException {@inheritDoc}
893N/A * @throws UnsupportedAddressTypeException {@inheritDoc}
893N/A * @throws SecurityException {@inheritDoc}
893N/A * @throws ClosedChannelException {@inheritDoc}
893N/A * @throws IOException {@inheritDoc}
893N/A */
893N/A public final AsynchronousServerSocketChannel bind(SocketAddress local)
893N/A throws IOException
893N/A {
893N/A return bind(local, 0);
893N/A }
893N/A
893N/A /**
893N/A * Binds the channel's socket to a local address and configures the socket to
893N/A * listen for connections.
893N/A *
893N/A * <p> This method is used to establish an association between the socket and
893N/A * a local address. Once an association is established then the socket remains
893N/A * bound until the associated channel is closed.
893N/A *
893N/A * <p> The {@code backlog} parameter is the maximum number of pending
893N/A * connections on the socket. Its exact semantics are implementation specific.
893N/A * In particular, an implementation may impose a maximum length or may choose
893N/A * to ignore the parameter altogther. If the {@code backlog} parameter has
893N/A * the value {@code 0}, or a negative value, then an implementation specific
893N/A * default is used.
893N/A *
893N/A * @param local
893N/A * The local address to bind the socket, or {@code null} to bind
893N/A * to an automatically assigned socket address
893N/A * @param backlog
893N/A * The maximum number of pending connections
893N/A *
893N/A * @return This channel
893N/A *
893N/A * @throws AlreadyBoundException
893N/A * If the socket is already bound
893N/A * @throws UnsupportedAddressTypeException
893N/A * If the type of the given address is not supported
893N/A * @throws SecurityException
893N/A * If a security manager has been installed and its {@link
893N/A * SecurityManager#checkListen checkListen} method denies the operation
893N/A * @throws ClosedChannelException
893N/A * If the channel is closed
893N/A * @throws IOException
893N/A * If some other I/O error occurs
893N/A */
893N/A public abstract AsynchronousServerSocketChannel bind(SocketAddress local, int backlog)
893N/A throws IOException;
893N/A
893N/A /**
893N/A * @throws IllegalArgumentException {@inheritDoc}
893N/A * @throws ClosedChannelException {@inheritDoc}
893N/A * @throws IOException {@inheritDoc}
893N/A */
893N/A public abstract <T> AsynchronousServerSocketChannel setOption(SocketOption<T> name, T value)
893N/A throws IOException;
893N/A
893N/A /**
893N/A * Accepts a connection.
893N/A *
1580N/A * <p> This method initiates an asynchronous operation to accept a
1580N/A * connection made to this channel's socket. The {@code handler} parameter is
1580N/A * a completion handler that is invoked when a connection is accepted (or
1580N/A * the operation fails). The result passed to the completion handler is
1580N/A * the {@link AsynchronousSocketChannel} to the new connection.
893N/A *
893N/A * <p> When a new connection is accepted then the resulting {@code
893N/A * AsynchronousSocketChannel} will be bound to the same {@link
893N/A * AsynchronousChannelGroup} as this channel. If the group is {@link
893N/A * AsynchronousChannelGroup#isShutdown shutdown} and a connection is accepted,
893N/A * then the connection is closed, and the operation completes with an {@code
893N/A * IOException} and cause {@link ShutdownChannelGroupException}.
893N/A *
893N/A * <p> To allow for concurrent handling of new connections, the completion
893N/A * handler is not invoked directly by the initiating thread when a new
893N/A * connection is accepted immediately (see <a
893N/A * href="AsynchronousChannelGroup.html#threading">Threading<a>).
893N/A *
893N/A * <p> If a security manager has been installed then it verifies that the
893N/A * address and port number of the connection's remote endpoint are permitted
893N/A * by the security manager's {@link SecurityManager#checkAccept checkAccept}
893N/A * method. The permission check is performed with privileges that are restricted
893N/A * by the calling context of this method. If the permission check fails then
893N/A * the connection is closed and the operation completes with a {@link
893N/A * SecurityException}.
893N/A *
893N/A * @param attachment
893N/A * The object to attach to the I/O operation; can be {@code null}
893N/A * @param handler
1580N/A * The handler for consuming the result
893N/A *
893N/A * @throws AcceptPendingException
893N/A * If an accept operation is already in progress on this channel
893N/A * @throws NotYetBoundException
893N/A * If this channel's socket has not yet been bound
893N/A * @throws ShutdownChannelGroupException
1580N/A * If the channel group has terminated
893N/A */
1580N/A public abstract <A> void accept(A attachment,
1580N/A CompletionHandler<AsynchronousSocketChannel,? super A> handler);
893N/A
893N/A /**
893N/A * Accepts a connection.
893N/A *
1580N/A * <p> This method initiates an asynchronous operation to accept a
1580N/A * connection made to this channel's socket. The method behaves in exactly
1580N/A * the same manner as the {@link #accept(Object, CompletionHandler)} method
1580N/A * except that instead of specifying a completion handler, this method
1580N/A * returns a {@code Future} representing the pending result. The {@code
1580N/A * Future}'s {@link Future#get() get} method returns the {@link
1580N/A * AsynchronousSocketChannel} to the new connection on successful completion.
893N/A *
1580N/A * @return a {@code Future} object representing the pending result
893N/A *
893N/A * @throws AcceptPendingException
893N/A * If an accept operation is already in progress on this channel
893N/A * @throws NotYetBoundException
893N/A * If this channel's socket has not yet been bound
893N/A */
1580N/A public abstract Future<AsynchronousSocketChannel> accept();
893N/A}