Java Code Examples for java.nio.channels.Selector#isOpen()

The following examples show how to use java.nio.channels.Selector#isOpen() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LifxLightCommunicationHandler.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
public void receiveAndHandlePackets() {
    try {
        lock.lock();
        Selector localSelector = selector;
        if (localSelector == null || !localSelector.isOpen()) {
            logger.debug("{} : Unable to receive and handle packets with null or closed selector", logId);
        } else {
            LifxSelectorUtil.receiveAndHandlePackets(localSelector, logId,
                    (packet, address) -> handlePacket(packet, address));
        }
    } catch (Exception e) {
        logger.error("{} while receiving a packet from the light ({}): {}", e.getClass().getSimpleName(), logId,
                e.getMessage());
    } finally {
        lock.unlock();
    }
}
 
Example 2
Source File: AcceptorImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/** wake up the selector thread */
private void wakeupSelector() {
  Selector s = getSelector();
  if (s != null && s.isOpen()) {
    this.selector.wakeup();
  }
}
 
Example 3
Source File: AcceptorImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/** wake up the selector thread */
private void wakeupSelector() {
  Selector s = getSelector();
  if (s != null && s.isOpen()) {
    this.selector.wakeup();
  }
}
 
Example 4
Source File: LifxLightDiscovery.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public void receiveAndHandlePackets() {
    Selector localSelector = selector;

    try {
        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;
    }
}