Java Code Examples for java.nio.channels.SelectionKey#OP_ACCEPT

The following examples show how to use java.nio.channels.SelectionKey#OP_ACCEPT . 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: SelectorImpl.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public void update(Selectable selectable) {
    if (selectable.getChannel() != null) {
        int interestedOps = 0;
        if (selectable.getChannel() instanceof SocketChannel &&
                ((SocketChannel)selectable.getChannel()).isConnectionPending()) {
            interestedOps |= SelectionKey.OP_CONNECT;
        } else {
            if (selectable.isReading()) {
                if (selectable.getChannel() instanceof ServerSocketChannel) {
                    interestedOps |= SelectionKey.OP_ACCEPT;
                } else {
                    interestedOps |= SelectionKey.OP_READ;
                }
            }
            if (selectable.isWriting()) interestedOps |= SelectionKey.OP_WRITE;
        }
        SelectionKey key = selectable.getChannel().keyFor(selector);
        key.interestOps(interestedOps);
    }
}
 
Example 2
Source File: Server.java    From TarsJava with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void startNIOServer() throws Exception {
    SelectableChannel server = null;
    int interestKey;

    //1. Start reactor service
    selectorManager.start();

    //2. Start server on the specified port
    if (this.udpMode) {
        server = DatagramChannel.open();
        ((DatagramChannel) server).socket().bind(new InetSocketAddress(host, port));
        interestKey = SelectionKey.OP_READ;
    } else {
        server = ServerSocketChannel.open();
        ((ServerSocketChannel) server).socket().bind(new InetSocketAddress(host, port), 1024);
        interestKey = SelectionKey.OP_ACCEPT;

    }

    server.configureBlocking(false);

    //3. Choose one reactor to handle NIO event
    selectorManager.getReactor(0).registerChannel(server, interestKey);
    System.out.println("INFO: NAMI Server started on port " + String.valueOf(port) + "...");

}
 
Example 3
Source File: Proxy.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
String printSelectionOps(SelectionKey key) {
    StringBuffer sb=new StringBuffer();
    if ((key.readyOps() & SelectionKey.OP_ACCEPT) !=0)
        sb.append("OP_ACCEPT ");
    if ((key.readyOps() & SelectionKey.OP_CONNECT) !=0)
        sb.append("OP_CONNECT ");
    if ((key.readyOps() & SelectionKey.OP_READ) !=0)
        sb.append("OP_READ ");
    if ((key.readyOps() & SelectionKey.OP_WRITE) !=0)
        sb.append("OP_WRITE ");
    return sb.toString();
}
 
Example 4
Source File: Proxy.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
String printSelectionOps(SelectionKey key) {
    StringBuffer sb=new StringBuffer();
    if ((key.readyOps() & SelectionKey.OP_ACCEPT) !=0)
        sb.append("OP_ACCEPT ");
    if ((key.readyOps() & SelectionKey.OP_CONNECT) !=0)
        sb.append("OP_CONNECT ");
    if ((key.readyOps() & SelectionKey.OP_READ) !=0)
        sb.append("OP_READ ");
    if ((key.readyOps() & SelectionKey.OP_WRITE) !=0)
        sb.append("OP_WRITE ");
    return sb.toString();
}
 
Example 5
Source File: SelectionKeyTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @tests java.nio.channels.SelectionKey#attachment()
 */
public void test_attachment() {
    MockSelectionKey mockSelectionKey = new MockSelectionKey(SelectionKey.OP_ACCEPT);
    assertNull(mockSelectionKey.attachment());
    Object o = new Object();
    mockSelectionKey.attach(o);
    assertSame(o, mockSelectionKey.attachment());
}
 
Example 6
Source File: NioEventLoop.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private static void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
    final NioUnsafe unsafe = ch.unsafe();
    if (!k.isValid()) {
        // close the channel if the key is not valid anymore
        unsafe.close(unsafe.voidPromise());
        return;
    }

    try {
        int readyOps = k.readyOps();
        // Also check for readOps of 0 to workaround possible JDK bug which may otherwise lead
        // to a spin loop
        if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
            unsafe.read();
            if (!ch.isOpen()) {
                // Connection already closed - no need to handle write.
                return;
            }
        }
        if ((readyOps & SelectionKey.OP_WRITE) != 0) {
            // Call forceFlush which will also take care of clear the OP_WRITE once there is nothing left to write
            ch.unsafe().forceFlush();
        }
        if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
            // remove OP_CONNECT as otherwise Selector.select(..) will always return without blocking
            // See https://github.com/netty/netty/issues/924
            int ops = k.interestOps();
            ops &= ~SelectionKey.OP_CONNECT;
            k.interestOps(ops);

            unsafe.finishConnect();
        }
    } catch (CancelledKeyException ignored) {
        unsafe.close(unsafe.voidPromise());
    }
}
 
Example 7
Source File: SelectorImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void registerForEvent(EventHandler eventHandler)
{
    if (orb.transportDebugFlag) {
        dprint(".registerForEvent: " + eventHandler);
    }

    if (isClosed()) {
        if (orb.transportDebugFlag) {
            dprint(".registerForEvent: closed: " + eventHandler);
        }
        return;
    }

    if (eventHandler.shouldUseSelectThreadToWait()) {
        synchronized (deferredRegistrations) {
            deferredRegistrations.add(eventHandler);
        }
        if (! selectorStarted) {
            startSelector();
        }
        selector.wakeup();
        return;
    }

    switch (eventHandler.getInterestOps()) {
    case SelectionKey.OP_ACCEPT :
        createListenerThread(eventHandler);
        break;
    case SelectionKey.OP_READ :
        createReaderThread(eventHandler);
        break;
    default:
        if (orb.transportDebugFlag) {
            dprint(".registerForEvent: default: " + eventHandler);
        }
        throw new RuntimeException(
            "SelectorImpl.registerForEvent: unknown interest ops");
    }
}
 
Example 8
Source File: TestMessageIO.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static boolean awaitOp(Selector selector, SelectionKey key, int op) throws IOException {
    long end = System.currentTimeMillis() + SELECTION_TIMEOUT_MS;
    boolean selected = false;
    while (!selected) {
        // Check timeout
        long remaining = end - System.currentTimeMillis();
        if (remaining < 0) {
            break;
        }

        // Select up to remaining millis
        selector.select(remaining);

        // Handle op if possible
        switch (op) {
        case SelectionKey.OP_READ:
            selected |= key.isReadable();
            break;
        case SelectionKey.OP_ACCEPT:
            selected |= key.isAcceptable();
            break;
        case SelectionKey.OP_WRITE:
            selected |= key.isWritable();
            break;
        case SelectionKey.OP_CONNECT:
            selected |= key.isConnectable();
            break;
        }
    }
    return selected;
}
 
Example 9
Source File: SctpServerChannelImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
    int newOps = 0;

    /* Translate ops */
    if ((ops & SelectionKey.OP_ACCEPT) != 0)
        newOps |= Net.POLLIN;
    /* Place ops into pollfd array */
    sk.selector.putEventOps(sk, newOps);

}
 
Example 10
Source File: SctpServerChannelImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates native poll revent ops into a ready operation ops
 */
private boolean translateReadyOps(int ops, int initialOps,
                                 SelectionKeyImpl sk) {
    int intOps = sk.nioInterestOps();
    int oldOps = sk.nioReadyOps();
    int newOps = initialOps;

    if ((ops & Net.POLLNVAL) != 0) {
        /* This should only happen if this channel is pre-closed while a
         * selection operation is in progress
         * ## Throw an error if this channel has not been pre-closed */
        return false;
    }

    if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
        newOps = intOps;
        sk.nioReadyOps(newOps);
        return (newOps & ~oldOps) != 0;
    }

    if (((ops & Net.POLLIN) != 0) &&
        ((intOps & SelectionKey.OP_ACCEPT) != 0))
            newOps |= SelectionKey.OP_ACCEPT;

    sk.nioReadyOps(newOps);
    return (newOps & ~oldOps) != 0;
}
 
Example 11
Source File: SctpServerChannelImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates native poll revent ops into a ready operation ops
 */
private boolean translateReadyOps(int ops, int initialOps,
                                 SelectionKeyImpl sk) {
    int intOps = sk.nioInterestOps();
    int oldOps = sk.nioReadyOps();
    int newOps = initialOps;

    if ((ops & Net.POLLNVAL) != 0) {
        /* This should only happen if this channel is pre-closed while a
         * selection operation is in progress
         * ## Throw an error if this channel has not been pre-closed */
        return false;
    }

    if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
        newOps = intOps;
        sk.nioReadyOps(newOps);
        return (newOps & ~oldOps) != 0;
    }

    if (((ops & Net.POLLIN) != 0) &&
        ((intOps & SelectionKey.OP_ACCEPT) != 0))
            newOps |= SelectionKey.OP_ACCEPT;

    sk.nioReadyOps(newOps);
    return (newOps & ~oldOps) != 0;
}
 
Example 12
Source File: NioServerSocketChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new instance using the given {@link ServerSocketChannel}.
 */
public NioServerSocketChannel(ServerSocketChannel channel) {
    super(null, channel, SelectionKey.OP_ACCEPT);
    config = new NioServerSocketChannelConfig(this, javaChannel().socket());
}
 
Example 13
Source File: SelectorImpl.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void unregisterForEvent(EventHandler eventHandler)
{
    if (orb.transportDebugFlag) {
        dprint(".unregisterForEvent: " + eventHandler);
    }

    if (isClosed()) {
        if (orb.transportDebugFlag) {
            dprint(".unregisterForEvent: closed: " + eventHandler);
        }
        return;
    }

    if (eventHandler.shouldUseSelectThreadToWait()) {
        SelectionKey selectionKey ;
        synchronized(deferredRegistrations) {
            selectionKey = eventHandler.getSelectionKey();
        }
        if (selectionKey != null) {
            selectionKey.cancel();
        }
        if (selector != null) {
            selector.wakeup();
        }
        return;
    }

    switch (eventHandler.getInterestOps()) {
    case SelectionKey.OP_ACCEPT :
        destroyListenerThread(eventHandler);
        break;
    case SelectionKey.OP_READ :
        destroyReaderThread(eventHandler);
        break;
    default:
        if (orb.transportDebugFlag) {
            dprint(".unregisterForEvent: default: " + eventHandler);
        }
        throw new RuntimeException(
            "SelectorImpl.uregisterForEvent: unknown interest ops");
    }
}
 
Example 14
Source File: ConnectionAcceptor.java    From mldht with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public int calcInterestOps() {
	return SelectionKey.OP_ACCEPT;
}
 
Example 15
Source File: SocketOrChannelAcceptorImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public int getInterestOps()
{
    return SelectionKey.OP_ACCEPT;
}
 
Example 16
Source File: SelectorImpl.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
public void unregisterForEvent(EventHandler eventHandler)
{
    if (orb.transportDebugFlag) {
        dprint(".unregisterForEvent: " + eventHandler);
    }

    if (isClosed()) {
        if (orb.transportDebugFlag) {
            dprint(".unregisterForEvent: closed: " + eventHandler);
        }
        return;
    }

    if (eventHandler.shouldUseSelectThreadToWait()) {
        SelectionKey selectionKey ;
        synchronized(deferredRegistrations) {
            selectionKey = eventHandler.getSelectionKey();
        }
        if (selectionKey != null) {
            selectionKey.cancel();
        }
        if (selector != null) {
            selector.wakeup();
        }
        return;
    }

    switch (eventHandler.getInterestOps()) {
    case SelectionKey.OP_ACCEPT :
        destroyListenerThread(eventHandler);
        break;
    case SelectionKey.OP_READ :
        destroyReaderThread(eventHandler);
        break;
    default:
        if (orb.transportDebugFlag) {
            dprint(".unregisterForEvent: default: " + eventHandler);
        }
        throw new RuntimeException(
            "SelectorImpl.uregisterForEvent: unknown interest ops");
    }
}
 
Example 17
Source File: SocketOrChannelAcceptorImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public int getInterestOps()
{
    return SelectionKey.OP_ACCEPT;
}
 
Example 18
Source File: SctpServerChannel.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns an operation set identifying this channel's supported
 * operations.
 *
 * <P> SCTP server channels only support the accepting of new
 * associations, so this method returns
 * {@link java.nio.channels.SelectionKey#OP_ACCEPT}.
 *
 * @return  The valid-operation set
 */
@Override
public final int validOps() {
    return SelectionKey.OP_ACCEPT;
}
 
Example 19
Source File: SctpServerChannel.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns an operation set identifying this channel's supported
 * operations.
 *
 * <P> SCTP server channels only support the accepting of new
 * associations, so this method returns
 * {@link java.nio.channels.SelectionKey#OP_ACCEPT}.
 *
 * @return  The valid-operation set
 */
@Override
public final int validOps() {
    return SelectionKey.OP_ACCEPT;
}
 
Example 20
Source File: SctpServerChannel.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns an operation set identifying this channel's supported
 * operations.
 *
 * <P> SCTP server channels only support the accepting of new
 * associations, so this method returns
 * {@link java.nio.channels.SelectionKey#OP_ACCEPT}.
 *
 * @return  The valid-operation set
 */
@Override
public final int validOps() {
    return SelectionKey.OP_ACCEPT;
}