Package java.nio.channels

Examples of java.nio.channels.Selector


    private static int readWithTemporarySelector(final SelectableChannel channel, final ByteBuffer byteBuffer, final long readTimeout, final ISelectorFactory selectorFactory)
            throws IOException {
        int count = 1;
        int byteRead = 0;
        int preReadInputBBPos = byteBuffer.position();
        Selector readSelector = null;
        SelectionKey tmpKey = null;

        try {
            ReadableByteChannel readableChannel = (ReadableByteChannel) channel;
            while(count > 0) {
                count = readableChannel.read(byteBuffer);
                if(count > -1) {
                    byteRead += count;
                } else {
                    byteRead = count;
                }
            }

            if(byteRead == 0 && byteBuffer.position() == preReadInputBBPos) {
                readSelector = selectorFactory.getSelector();

                if(readSelector == null) {
                    return 0;
                }
                count = 1;

                tmpKey = channel.register(readSelector, SelectionKey.OP_READ);
                tmpKey.interestOps(tmpKey.interestOps() | SelectionKey.OP_READ);
                int code = readSelector.select(readTimeout);
                tmpKey.interestOps(tmpKey.interestOps() & (~SelectionKey.OP_READ));

                if(code == 0) {
                    return 0; // Return on the main Selector and try again.
                }

                while(count > 0) {
                    count = readableChannel.read(byteBuffer);
                    if(count > -1) {
                        byteRead += count;
                    } else {
                        byteRead = count;
                    }
                }
            } else if(byteRead == 0 && byteBuffer.position() != preReadInputBBPos) {
                byteRead += (byteBuffer.position() - preReadInputBBPos);
            }
        } finally {
            if(tmpKey != null) {
                tmpKey.cancel();
            }
            if(readSelector != null) {// Bug 6403933
                try {
                    readSelector.selectNow();
                } catch (IOException e) {
                    ;
                }
                selectorFactory.returnSelector(readSelector);
            }


     * Get a exclusive <code>Selector</code>
     * @return <code>Selector</code>
     */
    public Selector getSelector() {
        synchronized(selectors) {
            Selector s = null;
            try {
                if(selectors.size() != 0) {
                    s = selectors.pop();
                }
            } catch (EmptyStackException ex) {

     * Decrease <code>Selector</code> pool size
     */
    private void reduce(int size) {
        for(int i = 0; i < maxSelectors - size; i++) {
            try {
                Selector selector = selectors.pop();
                selector.close();
            } catch (IOException e) {
                ;
            }
        }
    }

            }
        }
    }

    public void close() throws IOException {
        Selector selector;
        while(null != (selector = selectors.pop())) {
            selector.close();
        }
        selectors.clear();
    }

        this.mapping_file=mapping_file;
    }

    public void start() throws Exception {
        Map.Entry           entry;
        Selector            selector;
        ServerSocketChannel sock_channel;
        MyInetSocketAddress key, value;

        if (remote !=null && local !=null)
            mappings.put(new InetSocketAddress(local, local_port), new InetSocketAddress(remote, remote_port));

    }
   
    void _handleConnection(final SocketChannel in_channel, final SocketChannel out_channel) throws Exception {
        executor.execute(new Runnable() {
                public void run() {
                    Selector sel=null;
                    SocketChannel tmp;
                    Set ready_keys;
                    SelectionKey key;
                    ByteBuffer transfer_buf=ByteBuffer.allocate(BUFSIZE);

                    try {
                        sel=Selector.open();
                        in_channel.configureBlocking(false);
                        out_channel.configureBlocking(false);
                        in_channel.register(sel, SelectionKey.OP_READ);
                        out_channel.register(sel, SelectionKey.OP_READ);
                       
                        while (sel.select() > 0) {
                            ready_keys=sel.selectedKeys();
                            for (Iterator it=ready_keys.iterator(); it.hasNext();) {
                                key=(SelectionKey) it.next();
                                it.remove(); // remove current entry (why ?)
                                tmp=(SocketChannel) key.channel();
                                if (tmp == null) {

     */
    public static int readWithTemporarySelector(final SelectableChannel channel, final ByteBuffer byteBuffer, final long readTimeout, final NioSelectorPool selectorPool)
            throws IOException {
        final int preBufPos = byteBuffer.position();
        int byteRead = 0;
        Selector readSelector = null;
        SelectionKey tmpKey = null;

        try {
            ReadableByteChannel readableChannel = (ReadableByteChannel) channel;
            int count = 1;
            while(count > 0) {
                count = readableChannel.read(byteBuffer);
                if(count > -1) {
                    byteRead += count;
                } else {
                    byteRead = count;
                }
            }

            if(byteRead == 0 && byteBuffer.position() == preBufPos) {
                readSelector = selectorPool.getSelector();
                if(readSelector == null) {
                    return 0;
                }
                count = 1;

                tmpKey = channel.register(readSelector, SelectionKey.OP_READ);
                tmpKey.interestOps(tmpKey.interestOps() | SelectionKey.OP_READ);
                int code = readSelector.select(readTimeout);
                tmpKey.interestOps(tmpKey.interestOps() & (~SelectionKey.OP_READ));

                if(code == 0) {
                    return 0; // Return on the main Selector and try again.
                }

        this.maxAttempt = count;
    }

    @Nullable
    public Selector getSelector() throws IOException {
        Selector selector = pool.pop();
        if(selector == null) {
            if(AtomicUtils.tryIncrementIfLessThan(active, maxSelectors)) {
                selector = Selector.open();
            } else {
                waiter.incrementAndGet();

        returnSelector(s);
    }

    public void close() throws IOException {
        IOException firstException = null;
        Selector s;
        while((s = pool.pop()) != null) {
            try {
                s.close();
            } catch (IOException e) {
                if(firstException == null) {
                    firstException = e;
                }
            }

    }
  }

  private Selector initSelector() throws IOException {
    // Create a new selector
    Selector socketSelector = SelectorProvider.provider().openSelector();

    // Create a new non-blocking server socket channel
    this.serverChannel = ServerSocketChannel.open();
    serverChannel.configureBlocking(false);

TOP

Related Classes of java.nio.channels.Selector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.