0N/A/*
3261N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/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
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/Apackage sun.nio.ch;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.nio.channels.*;
0N/A
0N/A
0N/A// Make a server-socket channel look like a server socket.
0N/A//
0N/A// The methods in this class are defined in exactly the same order as in
0N/A// java.net.ServerSocket so as to simplify tracking future changes to that
0N/A// class.
0N/A//
0N/A
0N/Apublic class ServerSocketAdaptor // package-private
0N/A extends ServerSocket
0N/A{
0N/A
0N/A // The channel being adapted
0N/A private final ServerSocketChannelImpl ssc;
0N/A
0N/A // Timeout "option" value for accepts
0N/A private volatile int timeout = 0;
0N/A
0N/A public static ServerSocket create(ServerSocketChannelImpl ssc) {
0N/A try {
0N/A return new ServerSocketAdaptor(ssc);
0N/A } catch (IOException x) {
0N/A throw new Error(x);
0N/A }
0N/A }
0N/A
0N/A // ## super will create a useless impl
0N/A private ServerSocketAdaptor(ServerSocketChannelImpl ssc)
0N/A throws IOException
0N/A {
0N/A this.ssc = ssc;
0N/A }
0N/A
0N/A
0N/A public void bind(SocketAddress local) throws IOException {
0N/A bind(local, 50);
0N/A }
0N/A
0N/A public void bind(SocketAddress local, int backlog) throws IOException {
0N/A if (local == null)
0N/A local = new InetSocketAddress(0);
0N/A try {
0N/A ssc.bind(local, backlog);
0N/A } catch (Exception x) {
0N/A Net.translateException(x);
0N/A }
0N/A }
0N/A
0N/A public InetAddress getInetAddress() {
0N/A if (!ssc.isBound())
0N/A return null;
6319N/A return Net.getRevealedLocalAddress(ssc.localAddress()).getAddress();
6319N/A
0N/A }
0N/A
0N/A public int getLocalPort() {
0N/A if (!ssc.isBound())
0N/A return -1;
0N/A return Net.asInetSocketAddress(ssc.localAddress()).getPort();
0N/A }
0N/A
0N/A
0N/A public Socket accept() throws IOException {
0N/A synchronized (ssc.blockingLock()) {
0N/A if (!ssc.isBound())
0N/A throw new IllegalBlockingModeException();
0N/A try {
0N/A if (timeout == 0) {
0N/A SocketChannel sc = ssc.accept();
0N/A if (sc == null && !ssc.isBlocking())
0N/A throw new IllegalBlockingModeException();
0N/A return sc.socket();
0N/A }
0N/A
0N/A // Implement timeout with a selector
0N/A SelectionKey sk = null;
0N/A Selector sel = null;
0N/A ssc.configureBlocking(false);
0N/A try {
0N/A SocketChannel sc;
0N/A if ((sc = ssc.accept()) != null)
0N/A return sc.socket();
0N/A sel = Util.getTemporarySelector(ssc);
0N/A sk = ssc.register(sel, SelectionKey.OP_ACCEPT);
0N/A long to = timeout;
0N/A for (;;) {
0N/A if (!ssc.isOpen())
0N/A throw new ClosedChannelException();
0N/A long st = System.currentTimeMillis();
0N/A int ns = sel.select(to);
0N/A if (ns > 0 &&
0N/A sk.isAcceptable() && ((sc = ssc.accept()) != null))
0N/A return sc.socket();
0N/A sel.selectedKeys().remove(sk);
0N/A to -= System.currentTimeMillis() - st;
0N/A if (to <= 0)
0N/A throw new SocketTimeoutException();
0N/A }
0N/A } finally {
0N/A if (sk != null)
0N/A sk.cancel();
0N/A if (ssc.isOpen())
0N/A ssc.configureBlocking(true);
0N/A if (sel != null)
0N/A Util.releaseTemporarySelector(sel);
0N/A }
0N/A
0N/A } catch (Exception x) {
0N/A Net.translateException(x);
0N/A assert false;
0N/A return null; // Never happens
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void close() throws IOException {
2471N/A ssc.close();
0N/A }
0N/A
0N/A public ServerSocketChannel getChannel() {
0N/A return ssc;
0N/A }
0N/A
0N/A public boolean isBound() {
0N/A return ssc.isBound();
0N/A }
0N/A
0N/A public boolean isClosed() {
0N/A return !ssc.isOpen();
0N/A }
0N/A
0N/A public void setSoTimeout(int timeout) throws SocketException {
0N/A this.timeout = timeout;
0N/A }
0N/A
0N/A public int getSoTimeout() throws SocketException {
0N/A return timeout;
0N/A }
0N/A
0N/A public void setReuseAddress(boolean on) throws SocketException {
524N/A try {
4216N/A ssc.setOption(StandardSocketOptions.SO_REUSEADDR, on);
524N/A } catch (IOException x) {
524N/A Net.translateToSocketException(x);
524N/A }
0N/A }
0N/A
0N/A public boolean getReuseAddress() throws SocketException {
524N/A try {
4216N/A return ssc.getOption(StandardSocketOptions.SO_REUSEADDR).booleanValue();
524N/A } catch (IOException x) {
524N/A Net.translateToSocketException(x);
524N/A return false; // Never happens
524N/A }
0N/A }
0N/A
0N/A public String toString() {
0N/A if (!isBound())
0N/A return "ServerSocket[unbound]";
0N/A return "ServerSocket[addr=" + getInetAddress() +
0N/A // ",port=" + getPort() +
0N/A ",localport=" + getLocalPort() + "]";
0N/A }
0N/A
0N/A public void setReceiveBufferSize(int size) throws SocketException {
524N/A // size 0 valid for ServerSocketChannel, invalid for ServerSocket
524N/A if (size <= 0)
524N/A throw new IllegalArgumentException("size cannot be 0 or negative");
524N/A try {
4216N/A ssc.setOption(StandardSocketOptions.SO_RCVBUF, size);
524N/A } catch (IOException x) {
524N/A Net.translateToSocketException(x);
524N/A }
0N/A }
0N/A
0N/A public int getReceiveBufferSize() throws SocketException {
524N/A try {
4216N/A return ssc.getOption(StandardSocketOptions.SO_RCVBUF).intValue();
524N/A } catch (IOException x) {
524N/A Net.translateToSocketException(x);
524N/A return -1; // Never happens
524N/A }
0N/A }
0N/A
0N/A}