Example usage for java.nio.channels Selector isOpen

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

Introduction

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

Prototype

public abstract boolean isOpen();

Source Link

Document

Tells whether or not this selector is open.

Usage

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  va2  s. c o  m

            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.openhab.binding.lifx.internal.LifxLightDiscovery.java

public void receiveAndHandlePackets() {
    Selector localSelector = selector;

    try {//from  www.  ja  va 2 s  .c  o  m
        if (localSelector == null || !localSelector.isOpen()) {
            logger.debug("Unable to receive and handle packets with null or closed selector");
            return;
        }

        discoveredLights.clear();
        logger.trace("Entering read loop");
        long startStamp = System.currentTimeMillis();

        while (System.currentTimeMillis() - startStamp < SELECTOR_TIMEOUT) {
            int lightCount = discoveredLights.size();
            long selectStamp = System.currentTimeMillis();

            LifxSelectorUtil.receiveAndHandlePackets(localSelector, LOG_ID,
                    (packet, address) -> handlePacket(packet, address));
            requestAdditionalLightData();

            boolean discoveredNewLights = lightCount < discoveredLights.size();
            if (!discoveredNewLights) {
                boolean preventBusyWaiting = System.currentTimeMillis() - selectStamp < PACKET_INTERVAL;
                if (preventBusyWaiting) {
                    Thread.sleep(PACKET_INTERVAL);
                }
            }
        }
        logger.trace("Exited read loop");
    } catch (Exception e) {
        logger.debug("{} while receiving and handling discovery packets: {}", e.getClass().getSimpleName(),
                e.getMessage(), e);
    } finally {
        LifxSelectorUtil.closeSelector(localSelector, LOG_ID);
        selector = null;
        isScanning = false;
    }
}