Example usage for java.nio.channels.spi AbstractSelectableChannel keyFor

List of usage examples for java.nio.channels.spi AbstractSelectableChannel keyFor

Introduction

In this page you can find the example usage for java.nio.channels.spi AbstractSelectableChannel keyFor.

Prototype

public final SelectionKey keyFor(Selector sel) 

Source Link

Usage

From source file:net.timewalker.ffmq4.transport.tcp.nio.NIOTcpMultiplexer.java

private void closeSocketChannel(AbstractSelectableChannel channel, Selector selector) {
    try {/*ww w .  j  a v a2 s . c o m*/
        SelectionKey sk = channel.keyFor(selector);
        if (sk != null && sk.isValid())
            sk.cancel();
        if (channel.isOpen())
            channel.close();
    } catch (Exception e) {
        log.error("Could not close channel : " + e.toString());
    }
}

From source file:net.timewalker.ffmq4.transport.tcp.nio.NIOTcpMultiplexer.java

protected void addInterest(AbstractSelectableChannel channel, int interest, Object attachment,
        Selector selector) {/*from   w ww.  j a  v a  2  s . com*/
    try {
        SelectionKey sk = channel.keyFor(selector);
        if (sk != null) {
            if (!sk.isValid())
                return;

            int actualInterests = sk.interestOps();
            if ((actualInterests & interest) != interest)
                sk.interestOps(actualInterests | interest);
            if (attachment != null)
                sk.attach(attachment);
        } else
            channel.register(selector, interest, attachment);
    } catch (ClosedChannelException e) {
        log.warn("Cannot add interest to selector channel : channel is closed");
    }
}

From source file:net.timewalker.ffmq4.transport.tcp.nio.NIOTcpMultiplexer.java

private void removeInterest(AbstractSelectableChannel channel, int interest, Selector selector) {
    SelectionKey sk = channel.keyFor(selector);
    if (sk != null && sk.isValid()) {
        int actualInterests = sk.interestOps();
        if ((actualInterests & interest) != 0)
            sk.interestOps(sk.interestOps() & ~interest);
    }//  w ww  .j  a  v a2  s  . c  om
}