Example usage for java.nio.channels SelectableChannel register

List of usage examples for java.nio.channels SelectableChannel register

Introduction

In this page you can find the example usage for java.nio.channels SelectableChannel register.

Prototype

public abstract SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException;

Source Link

Document

Registers this channel with the given selector, returning a selection key.

Usage

From source file:edu.tsinghua.lumaqq.qq.net.Porter.java

/**
 * portporter//from   w w  w .  j  av a  2s . co m
 * 
 * @param port
 *       IPort
 * @throws ClosedChannelException
 *       
 */
public void register(IPort port) throws ClosedChannelException {
    SelectableChannel channel = port.channel();
    if (channel instanceof SocketChannel)
        channel.register(selector, SelectionKey.OP_CONNECT, port.getNIOHandler());
    else if (channel instanceof DatagramChannel)
        channel.register(selector, SelectionKey.OP_READ, port.getNIOHandler());
    if (!ports.contains(port))
        ports.add(port);
}

From source file:edu.tsinghua.lumaqq.qq.net.Porter.java

/**
 * ?channel//  ww  w . j av  a 2 s.  c om
 * 
 * @param port
 * @param ops
 * @throws ClosedChannelException
 */
public void register(IPort port, int ops) throws ClosedChannelException {
    SelectableChannel channel = port.channel();
    if (channel instanceof SocketChannel)
        channel.register(selector, ops, port.getNIOHandler());
    else if (channel instanceof DatagramChannel)
        channel.register(selector, ops, port.getNIOHandler());
    if (!ports.contains(port))
        ports.add(port);
}

From source file:edu.tsinghua.lumaqq.qq.net.Porter.java

/**
 * ???/*from   w w w. ja va2  s  .co  m*/
 * 
 * @param proxy
 *       IProxy
 * @throws ClosedChannelException
 *       
 */
public void register(IProxy proxy) throws ClosedChannelException {
    SelectableChannel channel = proxy.channel();
    if (channel instanceof SocketChannel)
        channel.register(selector, SelectionKey.OP_CONNECT, proxy.getNIOHandler());
    else if (channel instanceof DatagramChannel)
        channel.register(selector, SelectionKey.OP_READ, proxy.getNIOHandler());
    if (!proxies.contains(proxy))
        proxies.add(proxy);
}

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

private void registerChannelNow(final SelectableChannel channel, final int ops, final Object attachment,
        final Selector selector) {
    this.gate.lock();
    try {//www .  j av a  2 s  .c om
        if (channel.isOpen()) {
            channel.register(selector, ops, attachment);
        }
    } catch (final ClosedChannelException e) {
        log.error("Register channel error", e);
        this.controller.notifyException(e);
    } finally {
        this.gate.unlock();
    }
}

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  w  ww  .ja  v  a 2 s.  c om
     * 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: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();
    /**/*from  www. jav  a2s  . co  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.byteatebit.nbserver.simple.SelectorTask.java

@Override
public SelectionKey register(SelectableChannel channel, int ops, IOTask nioTask, IOTimeoutTask timeoutTask,
        long timeoutMs) {
    Preconditions.checkNotNull(channel, "channel cannot be null");
    Preconditions.checkNotNull(nioTask, "nioTask cannot be null");
    IOTimeoutTask ioTimeoutTask = timeoutTask == null ? defaultIoTimeoutTask : timeoutTask;
    if (LOG.isDebugEnabled())
        LOG.debug("registering channel " + channel + "for ops " + ops + " and task " + nioTask);
    SelectionKey key = null;//from ww  w .j a  v  a2s .c  o m
    NioSelectionKeyEntry selectionKeyEntry = null;
    synchronized (this) {
        try {
            key = channel.keyFor(selector);
            if (key != null) {
                key.interestOps(key.interestOps() | ops);
                selectionKeyEntry = (NioSelectionKeyEntry) key.attachment();
            } else {
                selectionKeyEntry = new NioSelectionKeyEntry();
                key = channel.register(selector, ops, selectionKeyEntry);
            }
        } catch (ClosedChannelException e) {
            throw new SelectorException(e);
        }
        selectionKeyEntry.setTask(nioTask, ioTimeoutTask, ops, timeoutMs);
    }
    if (LOG.isDebugEnabled())
        LOG.debug("Total number of selection keys: " + selector.keys().size());
    return key;
}

From source file:net.ymate.platform.serv.nio.support.NioEventProcessor.java

public void registerEvent(SelectableChannel channel, int ops, INioSession session) throws IOException {
    if (Thread.currentThread() == this) {
        SelectionKey key = channel.register(__selector, ops, session);
        if (session != null) {
            session.selectionKey(key);/*from  ww  w.  j  a  v  a 2 s  .  c om*/
            session.status(ISession.Status.CONNECTED);
            //
            __eventGroup.listener().onSessionRegisted(session);
        }
    } else {
        __eventQueues.offer(new Object[] { channel, ops, session });
        __selector.wakeup();
    }
}

From source file:net.ymate.platform.serv.nio.support.NioEventProcessor.java

protected void __doRegisterEvent() {
    Object[] _event;//  w  w  w. j  a va 2 s.  co  m
    while ((_event = __eventQueues.poll()) != null) {
        try {
            SelectableChannel _channel = (SelectableChannel) _event[0];
            if (!_channel.isOpen()) {
                continue;
            }
            INioSession _session = (INioSession) _event[2];
            SelectionKey _key = _channel.register(__selector, (Integer) _event[1], _session);
            if (_session != null) {
                _session.selectionKey(_key);
                _session.status(ISession.Status.CONNECTED);
                //
                __eventGroup.listener().onSessionRegisted(_session);
            }
        } catch (Exception e) {
            _LOG.error(e.getMessage(), RuntimeUtils.unwrapThrow(e));
        }
    }
}

From source file:org.apache.catalina.cluster.tcp.ReplicationListener.java

/**
 * Register the given channel with the given selector for
 * the given operations of interest//w  w w.  j  av a  2 s  .  c om
 */
protected void registerChannel(Selector selector, SelectableChannel channel, int ops, Object attach)
        throws Exception {
    if (channel == null)
        return; // could happen
    // set the new channel non-blocking
    channel.configureBlocking(false);
    // register it with the selector
    channel.register(selector, ops, attach);
}