Example usage for java.nio.channels SelectionKey interestOps

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

Introduction

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

Prototype

public abstract int interestOps();

Source Link

Document

Retrieves this key's interest set.

Usage

From source file:com.l2jfree.network.mmocore.ReadWriteThread.java

@Override
protected void handle(SelectionKey key) {
    System.out.println("ReadWriteThread.handle() " + describeInterestOps(key.interestOps()) + ", ready: "
            + describeInterestOps(key.readyOps()));
    switch (key.readyOps()) {
    case SelectionKey.OP_CONNECT:
        finishConnection(key);// www.  j a v a2 s. co m
        break;
    case SelectionKey.OP_READ:
        readPacket(key);
        break;
    case SelectionKey.OP_WRITE:
        writePacket(key);
        break;
    case SelectionKey.OP_READ | SelectionKey.OP_WRITE:
        writePacket(key);
        // key might have been invalidated on writePacket
        if (key.isValid())
            readPacket(key);
        break;
    default:
        System.err.println("Unknown readyOps: " + key.readyOps() + " for " + key.attachment());
        break;
    }
}

From source file:com.alibaba.napoli.gecko.core.nio.impl.Reactor.java

private boolean lookJVMBug(final long before, final int selected, final long wait) throws IOException {
    boolean seeing = false;
    final long now = System.currentTimeMillis();
    /**//ww  w  . j  ava 2s .c  o m
     * Bug?,(1)select0 (2)select?? (3)? (4)?wakenup
     */
    if (JVMBUG_THRESHHOLD > 0 && selected == 0 && wait > JVMBUG_THRESHHOLD && now - before < wait / 4
            && !this.wakenUp.get() /* waken up */
            && !Thread.currentThread().isInterrupted()/* Interrupted */) {
        this.jvmBug.incrementAndGet();
        // ?1?selector
        if (this.jvmBug.get() >= JVMBUG_THRESHHOLD2) {
            this.gate.lock();
            try {
                this.lastJVMBug = now;
                log.warn("JVM bug occured at " + new Date(this.lastJVMBug)
                        + ",http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6403933,reactIndex="
                        + this.reactorIndex);
                if (this.jvmBug1) {
                    log.debug("seeing JVM BUG(s) - recreating selector,reactIndex=" + this.reactorIndex);
                } else {
                    this.jvmBug1 = true;
                    log.info("seeing JVM BUG(s) - recreating selector,reactIndex=" + this.reactorIndex);
                }
                seeing = true;
                // selector
                final Selector new_selector = SystemUtils.openSelector();

                for (final SelectionKey k : this.selector.keys()) {
                    if (!k.isValid() || k.interestOps() == 0) {
                        continue;
                    }

                    final SelectableChannel channel = k.channel();
                    final Object attachment = k.attachment();
                    // ?interestOps>0channel
                    channel.register(new_selector, k.interestOps(), attachment);
                }

                this.selector.close();
                this.selector = new_selector;

            } finally {
                this.gate.unlock();
            }
            this.jvmBug.set(0);

        } else if (this.jvmBug.get() == JVMBUG_THRESHHOLD || this.jvmBug.get() == JVMBUG_THRESHHOLD1) {
            // BUG?0?interestedOps==0key
            if (this.jvmBug0) {
                log.debug("seeing JVM BUG(s) - cancelling interestOps==0,reactIndex=" + this.reactorIndex);
            } else {
                this.jvmBug0 = true;
                log.info("seeing JVM BUG(s) - cancelling interestOps==0,reactIndex=" + this.reactorIndex);
            }
            this.gate.lock();
            seeing = true;
            try {
                for (final SelectionKey k : this.selector.keys()) {
                    if (k.isValid() && k.interestOps() == 0) {
                        k.cancel();
                    }
                }
            } finally {
                this.gate.unlock();
            }
        }
    } else {
        this.jvmBug.set(0);
    }
    return seeing;
}

From source file:com.taobao.gecko.core.nio.impl.Reactor.java

private boolean lookJVMBug(final long before, final int selected, final long wait) throws IOException {
    boolean seeing = false;
    final long now = System.currentTimeMillis();
    /**/*from   www. j  a v  a  2s.  c  o m*/
     * Bug,(1)select0 (2)select (3) (4)wakenup
     */
    if (JVMBUG_THRESHHOLD > 0 && selected == 0 && wait > JVMBUG_THRESHHOLD && now - before < wait / 4
            && !this.wakenUp.get() /* waken up */
            && !Thread.currentThread().isInterrupted()/* Interrupted */) {
        this.jvmBug.incrementAndGet();
        // 1selector
        if (this.jvmBug.get() >= JVMBUG_THRESHHOLD2) {
            this.gate.lock();
            try {
                this.lastJVMBug = now;
                log.warn("JVM bug occured at " + new Date(this.lastJVMBug)
                        + ",http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6403933,reactIndex="
                        + this.reactorIndex);
                if (this.jvmBug1) {
                    log.debug("seeing JVM BUG(s) - recreating selector,reactIndex=" + this.reactorIndex);
                } else {
                    this.jvmBug1 = true;
                    log.info("seeing JVM BUG(s) - recreating selector,reactIndex=" + this.reactorIndex);
                }
                seeing = true;
                // selector
                final Selector new_selector = SystemUtils.openSelector();

                for (final SelectionKey k : this.selector.keys()) {
                    if (!k.isValid() || k.interestOps() == 0) {
                        continue;
                    }

                    final SelectableChannel channel = k.channel();
                    final Object attachment = k.attachment();
                    // interestOps>0channel
                    channel.register(new_selector, k.interestOps(), attachment);
                }

                this.selector.close();
                this.selector = new_selector;

            } finally {
                this.gate.unlock();
            }
            this.jvmBug.set(0);

        } else if (this.jvmBug.get() == JVMBUG_THRESHHOLD || this.jvmBug.get() == JVMBUG_THRESHHOLD1) {
            // BUG0interestedOps==0key
            if (this.jvmBug0) {
                log.debug("seeing JVM BUG(s) - cancelling interestOps==0,reactIndex=" + this.reactorIndex);
            } else {
                this.jvmBug0 = true;
                log.info("seeing JVM BUG(s) - cancelling interestOps==0,reactIndex=" + this.reactorIndex);
            }
            this.gate.lock();
            seeing = true;
            try {
                for (final SelectionKey k : this.selector.keys()) {
                    if (k.isValid() && k.interestOps() == 0) {
                        k.cancel();
                    }
                }
            } finally {
                this.gate.unlock();
            }
        }
    } else {
        this.jvmBug.set(0);
    }
    return seeing;
}

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

protected void addInterest(AbstractSelectableChannel channel, int interest, Object attachment,
        Selector selector) {//from  ww  w. j  a  va 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.  ja  va  2s. c  o  m
}