FileDocCategorySizeDatePackage
ServerSocketChannelImpl.javaAPI DocAndroid 1.5 API10645Wed May 06 22:41:04 BST 2009org.apache.harmony.nio.internal

ServerSocketChannelImpl

public class ServerSocketChannelImpl extends ServerSocketChannel implements org.apache.harmony.luni.platform.FileDescriptorHandler

Fields Summary
private static final int
SERVER_STATUS_UNINIT
private static final int
SERVER_STATUS_OPEN
private static final int
SERVER_STATUS_CLOSED
private final FileDescriptor
fd
private final ServerSocket
socket
private final SocketImpl
impl
int
status
boolean
isBound
private final Object
acceptLock
Constructors Summary
public ServerSocketChannelImpl(SelectorProvider sp)


    // ----------------------------------------------------
    // Constructor
    // ----------------------------------------------------

    /*
     * Constructor
     */
         
        super(sp);
        status = SERVER_STATUS_OPEN;
        fd = new FileDescriptor();
        Platform.getNetworkSystem().createServerStreamSocket(fd,
                NetUtil.preferIPv4Stack());
        impl = SocketImplProvider.getServerSocketImpl(fd);
        socket = new ServerSocketAdapter(impl, this);
    
private ServerSocketChannelImpl()

        super(SelectorProvider.provider());
        status = SERVER_STATUS_OPEN;
        fd = new FileDescriptor();
        impl = SocketImplProvider.getServerSocketImpl(fd);        
        socket = new ServerSocketAdapter(impl, this);
        isBound = false;
    
Methods Summary
public java.nio.channels.SocketChannelaccept()

        if (!isOpen()) {
            throw new ClosedChannelException();
        }
        if (!isBound) {
            throw new NotYetBoundException();
        }

        SocketChannel sockChannel = SocketChannel.open();
        Socket socketGot = sockChannel.socket();

        try {
            begin();

            synchronized (acceptLock) {
                synchronized (blockingLock()) {
                    boolean isBlocking = isBlocking();
                    if (!isBlocking) {
                        // for non blocking mode, use select to see whether
                        // there are any pending connections.
                        int[] tryResult = Platform.getNetworkSystem().select(
                                new FileDescriptor[] { this.fd },
                                new FileDescriptor[0], 0);
                        if (0 == tryResult.length || 0 == tryResult[0]) {
                            // no pending connections, returns immediately.
                            return null;
                        }
                    }
                    // do accept.
                    do {
                        try {
                            ((ServerSocketAdapter) socket).accept(socketGot,
                                    (SocketChannelImpl) sockChannel);
                            // select successfully, break out immediately.
                            break;
                        } catch (SocketTimeoutException e) {
                            // continue to accept if the channel is in blocking
                            // mode.
                        }
                    } while (isBlocking);
                }
            }
        } finally {
            end(socketGot.isConnected());
        }
        return sockChannel;
    
public java.io.FileDescriptorgetFD()

        return fd;
    
protected synchronized voidimplCloseSelectableChannel()

        status = SERVER_STATUS_CLOSED;
        if (!socket.isClosed()) {
            socket.close();
        }
    
protected voidimplConfigureBlocking(boolean blockingMode)

        // Do nothing here. For real accept() operation in nonblocking mode,
        // it uses INetworkSystem.select. Whether a channel is blocking can be
        // decided by isBlocking() method.
    
public java.net.ServerSocketsocket()

        return socket;