Example usage for java.nio.channels Selector keys

List of usage examples for java.nio.channels Selector keys

Introduction

In this page you can find the example usage for java.nio.channels Selector keys.

Prototype

public abstract Set<SelectionKey> keys();

Source Link

Document

Returns this selector's key set.

Usage

From source file:ee.ria.xroad.proxy.clientproxy.FastestSocketSelector.java

private static void closeSelector(Selector selector, SocketChannel selectedChannel) throws IOException {
    for (SelectionKey key : selector.keys()) {
        if (selectedChannel == null || !selectedChannel.equals(key.channel())) {
            closeQuietly(key.channel());
        }/*  w w  w . j a  va 2 s  .c om*/
    }

    selector.close();
}

From source file:ee.ria.xroad.proxy.clientproxy.FastestSocketSelector.java

private SelectionKey selectFirstConnectedSocketChannel(Selector selector) throws IOException {
    log.trace("selectFirstConnectedSocketChannel()");

    while (!selector.keys().isEmpty()) {
        if (selector.select(connectTimeout) == 0) { // Block until something happens
            return null;
        }/*  w  w  w  .  j av a2s .  c o  m*/

        Iterator<SelectionKey> it = selector.selectedKeys().iterator();
        while (it.hasNext()) {
            SelectionKey key = it.next();
            if (key.isValid() && key.isConnectable()) {
                SocketChannel channel = (SocketChannel) key.channel();
                try {
                    if (channel.finishConnect()) {
                        return key;
                    }
                } catch (Exception e) {
                    key.cancel();
                    closeQuietly(channel);

                    log.trace("Error connecting socket channel: {}", e);
                }
            }

            it.remove();
        }
    }

    return null;
}

From source file:net.sf.cindy.impl.SimpleEventGenerator.java

protected void finishedSelect(Selector selector) {
    for (Iterator iter = selector.keys().iterator(); iter.hasNext();) {
        SelectionKey key = (SelectionKey) iter.next();
        Session session = (Session) key.attachment();
        session.close();//from  w  w w. ja va 2 s.c  om
    }
    try {
        selector.close();
    } catch (IOException e) {
    }
    selector = null;
    lastSelectTime = null;
    register.clear();
    close = false;
    thread = null;
}

From source file:gridool.communication.transport.nio.GridNioServer.java

public void accept(@Nonnull final Selector selector) throws IOException {
    while (!closed && selector.select() > 0) {
        for (Iterator<SelectionKey> iter = selector.selectedKeys().iterator(); iter.hasNext();) {
            SelectionKey key = iter.next();
            iter.remove();/*from  ww w  .j a  v  a 2 s.  com*/

            if (!key.isValid()) { // Is key closed?
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Non valid key was detected. Key is already closed?");
                }
                continue;
            }

            if (key.isAcceptable()) {
                ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
                handleAccept(serverChannel, selector);
            } else if (key.isReadable()) {
                SocketChannel channel = (SocketChannel) key.channel();
                handleRead(channel, key, sharedReadBuf, notifier, execPool);
            }
        }
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("GridNioServer is going to be closed");
    }
    this.closed = true;
    if (selector.isOpen()) {
        for (SelectionKey key : selector.keys()) {
            NIOUtils.close(key);
        }
        selector.close();
    }
}

From source file:org.apache.axis2.transport.udp.IODispatcher.java

/**
 * Remove an endpoint. This causes the corresponding UDP socket to be
 * closed./* w ww.ja  va2  s. co  m*/
 * 
 * @param endpoint the endpoint description
 * @throws IOException if an error occurred when closing the socket
 */
public void removeEndpoint(final Endpoint endpoint) throws IOException {
    execute(new SelectorOperation() {
        @Override
        public void doExecute(Selector selector) throws IOException {
            Iterator<SelectionKey> it = selector.keys().iterator();
            while (it.hasNext()) {
                SelectionKey key = it.next();
                Endpoint endpointForKey = (Endpoint) key.attachment();
                if (endpoint == endpointForKey) {
                    key.cancel();
                    key.channel().close();
                    break;
                }
            }
        }
    });
}

From source file:org.apache.axis2.transport.udp.IODispatcher.java

/**
 * Stop the dispatcher./*from   w w w  . j  a  va2s.  c om*/
 * This method closes all sockets and causes the execution of the
 * {@link #run()} method to stop.
 * 
 * @throws IOException
 */
public void stop() throws IOException {
    execute(new SelectorOperation() {
        @Override
        public void doExecute(Selector selector) throws IOException {
            IOException exception = null;
            for (SelectionKey key : selector.keys()) {
                try {
                    key.channel().close();
                } catch (IOException ex) {
                    if (exception == null) {
                        exception = ex;
                    }
                }
            }
            try {
                selector.close();
            } catch (IOException ex) {
                if (exception == null) {
                    exception = ex;
                }
            }
            if (exception != null) {
                throw exception;
            }
        }
    });
}

From source file:org.apache.synapse.transport.udp.IODispatcher.java

/**
 * Remove an endpoint. This causes the corresponding UDP socket to be
 * closed.//from  ww w  .j  a v a2 s  .co m
 * 
 * @param serviceName the name of the service corresponding to
 *                    the endpoint
 * @throws IOException if an error occurred when closing the socket
 */
public void removeEndpoint(final String serviceName) throws IOException {
    execute(new SelectorOperation() {
        @Override
        public void doExecute(Selector selector) throws IOException {
            Iterator<SelectionKey> it = selector.keys().iterator();
            while (it.hasNext()) {
                SelectionKey key = it.next();
                Endpoint endpoint = (Endpoint) key.attachment();
                if (serviceName.equals(endpoint.getService().getName())) {
                    key.cancel();
                    key.channel().close();
                    break;
                }
            }
        }
    });
}