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

SelectorImpl

public final class SelectorImpl extends AbstractSelector

Fields Summary
private static final int
MOCK_WRITEBUF_SIZE
private static final int
MOCK_READBUF_SIZE
private static final int
NA
private static final int
READABLE
private static final int
WRITEABLE
private static final int
SELECT_BLOCK
private static final int
SELECT_NOW
final Object
keysLock
private final Set
keys
private Set
unmodifiableKeys
private final Set
selectedKeys
private Set
unaddableSelectedKeys
private Pipe$SinkChannel
sink
private Pipe$SourceChannel
source
private FileDescriptor
sourcefd
private SelectionKey[]
readableChannels
private SelectionKey[]
writableChannels
private List
readableFDs
private List
writableFDs
private FileDescriptor[]
readable
private FileDescriptor[]
writable
Constructors Summary
public SelectorImpl(SelectorProvider selectorProvider)


       
        super(selectorProvider);
        try {
            Pipe mockSelector = selectorProvider.openPipe();
            sink = mockSelector.sink();
            source = mockSelector.source();
            sourcefd = ((FileDescriptorHandler)source).getFD();
            source.configureBlocking(false);
        } catch (IOException e) {
            // do nothing
        }
    
Methods Summary
private voidcloseCheck()

        if (!isOpen()) {
            throw new ClosedSelectorException();
        }
    
private voiddoCancel()

        Set<SelectionKey> cancelledKeys = cancelledKeys();
        synchronized (cancelledKeys) {
            if (cancelledKeys.size() > 0) {
                for (SelectionKey currentkey : cancelledKeys) {
                    deregister((AbstractSelectionKey) currentkey);
                    keys.remove(currentkey);
                    selectedKeys.remove(currentkey);
                }
            }
            cancelledKeys.clear();
        }
    
protected voidimplCloseSelector()

        doCancel();
        for (SelectionKey sk : keys) {
            deregister((AbstractSelectionKey) sk);
        }
        wakeup();
    
private booleanisConnected(SelectionKeyImpl key)

        SelectableChannel channel = key.channel();
        if (channel instanceof SocketChannel) {
            return ((SocketChannel) channel).isConnected();
        }
        return true;
    
public synchronized java.util.Setkeys()

        closeCheck();
        return unmodifiableKeys;
    
private voidprepareChannels()

        readableFDs.add(sourcefd);        
        List<SelectionKey> readChannelList = new ArrayList<SelectionKey>();
        readChannelList.add(source.keyFor(this));
        List<SelectionKey> writeChannelList = new ArrayList<SelectionKey>();
        synchronized (keysLock) {
            for (Iterator<SelectionKey> i = keys.iterator(); i.hasNext();) {
                SelectionKeyImpl key = (SelectionKeyImpl) i.next();
                key.oldInterestOps = key.interestOps();
                boolean isReadableChannel = ((SelectionKey.OP_ACCEPT | SelectionKey.OP_READ) & key.oldInterestOps) != 0;
                boolean isWritableChannel = ((SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE) & key.oldInterestOps) != 0;
                SelectableChannel channel = key.channel();                  
                if (isReadableChannel) {
                    readChannelList.add(channel.keyFor(this));
                    readableFDs.add(((FileDescriptorHandler)channel).getFD());
                }
                if (isWritableChannel) {
                    writeChannelList.add(channel.keyFor(this));
                    writableFDs.add(((FileDescriptorHandler)channel).getFD());
                }
            }
        }
        readableChannels = readChannelList.toArray(new SelectionKey[0]);
        writableChannels = writeChannelList.toArray(new SelectionKey[0]);
        readable = readableFDs.toArray(new FileDescriptor[0]);
        writable = writableFDs.toArray(new FileDescriptor[0]);
    
private intprocessSelectResult(int[] readyChannels)

        if (0 == readyChannels.length) {
            return 0;
        }
        // if the mock channel is selected, read the content.
        if (READABLE == readyChannels[0]) {
            ByteBuffer readbuf = ByteBuffer.allocate(MOCK_READBUF_SIZE);
            while (source.read(readbuf) > 0) {
                readbuf.flip();
            }
        }
        int selected = 0;
        for (int i = 1; i < readyChannels.length; i++) {            
            SelectionKeyImpl key = (SelectionKeyImpl) (i >= readable.length ? writableChannels[i
                    - readable.length]
                    : readableChannels[i]);
            if (null == key) {
                continue;
            }
            boolean isOldSelectedKey = selectedKeys.contains(key);
            int selectedOp = 0;
            // set ready ops
            switch (readyChannels[i]) {
            case NA:
                selectedOp = 0;
                break;
            case READABLE:
                selectedOp = (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)
                        & key.oldInterestOps;
                break;
            case WRITEABLE:
                if (isConnected(key)) {
                    selectedOp = SelectionKey.OP_WRITE & key.oldInterestOps;
                } else {
                    selectedOp = SelectionKey.OP_CONNECT & key.oldInterestOps;
                }
                break;
            }

            if (0 != selectedOp) {
                if (isOldSelectedKey && key.readyOps() != selectedOp) {
                    key.setReadyOps(key.readyOps() | selectedOp);
                    selected++;
                } else if (!isOldSelectedKey) {
                    key.setReadyOps(selectedOp);
                    selectedKeys.add(key);
                    selected++;
                }
            }
        }
        readableChannels = null;
        writableChannels = null;
        return selected;
    
protected java.nio.channels.SelectionKeyregister(java.nio.channels.spi.AbstractSelectableChannel channel, int operations, java.lang.Object attachment)

        if (!provider().equals(channel.provider())) {
            throw new IllegalSelectorException();
        }
        synchronized (this) {
            synchronized (keys) {
                SelectionKey sk = new SelectionKeyImpl(channel, operations,
                        attachment, this);
                keys.add(sk);
                return sk;
            }
        }
    
public intselect()

        return selectInternal(SELECT_BLOCK);
    
public intselect(long timeout)

        if (timeout < 0) {
            throw new IllegalArgumentException();
        }
        return selectInternal((0 == timeout) ? SELECT_BLOCK : timeout);
    
private intselectInternal(long timeout)

        closeCheck();
        synchronized (this) {
            synchronized (keys) {
                synchronized (selectedKeys) {
                    doCancel();
                    int[] readyChannels = null;
                    boolean isBlock = (SELECT_NOW != timeout);
                    // BEGIN android-removed
                    // copied from newer version of harmony
                    // if (keys.size() == 0) {
                    //     return 0;
                    // }
                    // END android-removed
                    prepareChannels();
                    try {
                        if (isBlock) {
                            begin();
                        }
                        readyChannels = Platform.getNetworkSystem().select(readable, writable, timeout);
                    } finally {
                        // clear results for next select
                        readableFDs.clear();
                        writableFDs.clear();                        
                        if (isBlock) {
                            end();
                        }
                    }
                    return processSelectResult(readyChannels);                    
                }
            }
        }
    
public intselectNow()

        return selectInternal(SELECT_NOW);
    
public synchronized java.util.SetselectedKeys()

        closeCheck();
        return unaddableSelectedKeys;
    
public java.nio.channels.Selectorwakeup()

        try {
            sink.write(ByteBuffer.allocate(MOCK_WRITEBUF_SIZE));
        } catch (IOException e) {
            // do nothing
        }
        return this;