Example usage for java.nio.channels SelectionKey equals

List of usage examples for java.nio.channels SelectionKey equals

Introduction

In this page you can find the example usage for java.nio.channels SelectionKey equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:oz.hadoop.yarn.api.net.ApplicationContainerServerImpl.java

@Override
protected boolean canClose(SelectionKey key) {
    return !key.equals(this.masterSelectionKey);
}

From source file:oz.hadoop.yarn.api.net.ApplicationContainerServerImpl.java

/**
 * Will be called ONLY when client initiates disconnect.
 * In the current implementation only Application Master initiates such disconnect
 * /*from w w  w  .j  a va2s .c om*/
 * @param selectionKey
 */
@Override
void onDisconnect(SelectionKey selectionKey) {
    if (selectionKey.equals(this.masterSelectionKey)) {

        preStop(true);

        this.closeChannel(this.masterSelectionKey.channel());

        this.closeChannel(this.rootChannel);
    } else {
        this.containerDelegates.remove(selectionKey);
    }
}

From source file:org.eclipse.smarthome.binding.lifx.internal.LifxLightDiscovery.java

private boolean sendPacket(Packet packet, InetSocketAddress address, SelectionKey selectedKey) {

    boolean result = false;

    try {/*from  w  w  w.  j a v  a2 s  . c o  m*/
        boolean sent = false;

        while (!sent) {
            try {
                selector.selectNow();
            } catch (IOException e) {
                logger.error("An exception occurred while selecting: {}", e.getMessage());
            }

            Set<SelectionKey> selectedKeys = selector.selectedKeys();

            Iterator<SelectionKey> keyIterator = selectedKeys.iterator();

            while (keyIterator.hasNext()) {
                SelectionKey key = keyIterator.next();

                if (key.isValid() && key.isWritable() && key.equals(selectedKey)) {
                    SelectableChannel channel = key.channel();
                    try {
                        if (channel instanceof DatagramChannel) {
                            logger.trace(
                                    "Discovery : Sending packet type '{}' from '{}' to '{}' for '{}' with sequence '{}' and source '{}'",
                                    new Object[] { packet.getClass().getSimpleName(),
                                            ((InetSocketAddress) ((DatagramChannel) channel).getLocalAddress())
                                                    .toString(),
                                            address.toString(), packet.getTarget().getHex(),
                                            packet.getSequence(), Long.toString(packet.getSource(), 16) });
                            ((DatagramChannel) channel).send(packet.bytes(), address);

                            sent = true;
                            result = true;
                        } else if (channel instanceof SocketChannel) {
                            ((SocketChannel) channel).write(packet.bytes());
                        }
                    } catch (Exception e) {
                        logger.error("An exception occurred while writing data : '{}'", e.getMessage());
                    }
                }
            }
        }

    } catch (Exception e) {
        logger.error("An exception occurred while communicating with the light : '{}'", e.getMessage());
    }

    return result;
}