Search in sources :

Example 1 with Selector

use of java.nio.channels.Selector in project jetty.project by eclipse.

the class ManagedSelector method dump.

@Override
public void dump(Appendable out, String indent) throws IOException {
    out.append(String.valueOf(this)).append(" id=").append(String.valueOf(_id)).append(System.lineSeparator());
    Selector selector = _selector;
    if (selector != null && selector.isOpen()) {
        final ArrayList<Object> dump = new ArrayList<>(selector.keys().size() * 2);
        DumpKeys dumpKeys = new DumpKeys(dump);
        submit(dumpKeys);
        dumpKeys.await(5, TimeUnit.SECONDS);
        ContainerLifeCycle.dump(out, indent, dump);
    }
}
Also used : ArrayList(java.util.ArrayList) Selector(java.nio.channels.Selector)

Example 2 with Selector

use of java.nio.channels.Selector in project jetty.project by eclipse.

the class ManagedSelector method submit.

public void submit(Runnable change) {
    if (LOG.isDebugEnabled())
        LOG.debug("Queued change {} on {}", change, this);
    Selector selector = null;
    try (Locker.Lock lock = _locker.lock()) {
        _actions.offer(change);
        if (_selecting) {
            selector = _selector;
            // To avoid the extra select wakeup.
            _selecting = false;
        }
    }
    if (selector != null)
        selector.wakeup();
}
Also used : Locker(org.eclipse.jetty.util.thread.Locker) Selector(java.nio.channels.Selector)

Example 3 with Selector

use of java.nio.channels.Selector in project tomcat by apache.

the class NioSelectorPool method close.

public void close() throws IOException {
    enabled = false;
    Selector s;
    while ((s = selectors.poll()) != null) s.close();
    spare.set(0);
    active.set(0);
    if (blockingSelector != null) {
        blockingSelector.close();
    }
    if (SHARED && getSharedSelector() != null) {
        getSharedSelector().close();
        SHARED_SELECTOR = null;
    }
}
Also used : Selector(java.nio.channels.Selector)

Example 4 with Selector

use of java.nio.channels.Selector in project cryptomator by cryptomator.

the class SingleInstanceManager method tryFill.

/**
	 * tries to fill the given buffer for the given time
	 * 
	 * @param channel
	 * @param buf
	 * @param timeout
	 * @throws ClosedChannelException
	 * @throws IOException
	 */
public static <T extends SelectableChannel & ReadableByteChannel> void tryFill(T channel, final ByteBuffer buf, int timeout) throws IOException {
    if (channel.isBlocking()) {
        throw new IllegalStateException("Channel is in blocking mode.");
    }
    try (Selector selector = Selector.open()) {
        channel.register(selector, SelectionKey.OP_READ);
        TimeoutTask.attempt(remainingTime -> {
            if (!buf.hasRemaining()) {
                return true;
            }
            if (selector.select(remainingTime) > 0) {
                if (channel.read(buf) < 0) {
                    return true;
                }
            }
            return !buf.hasRemaining();
        }, timeout, 1);
    }
}
Also used : Selector(java.nio.channels.Selector)

Example 5 with Selector

use of java.nio.channels.Selector in project cryptomator by cryptomator.

the class SingleInstanceManager method startLocalInstance.

/**
	 * Creates a server socket on a free port and saves the port in
	 * {@link Preferences#userNodeForPackage(Class)} for {@link Cryptomator} under the
	 * given applicationKey.
	 * 
	 * @param applicationKey
	 *            key used to save the port and identify upon connection.
	 * @param exec
	 *            the task which is submitted is interruptable.
	 * @return
	 * @throws IOException
	 */
public static LocalInstance startLocalInstance(String applicationKey, ExecutorService exec) throws IOException {
    final ServerSocketChannel channel = ServerSocketChannel.open();
    boolean success = false;
    try {
        channel.configureBlocking(false);
        channel.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
        final int port = ((InetSocketAddress) channel.getLocalAddress()).getPort();
        Preferences.userNodeForPackage(Cryptomator.class).putInt(applicationKey, port);
        LOG.debug("InstanceManager bound to port {}", port);
        Selector selector = Selector.open();
        channel.register(selector, SelectionKey.OP_ACCEPT);
        LocalInstance instance = new LocalInstance(applicationKey, channel, selector, port);
        exec.submit(instance::selectionLoop);
        success = true;
        return instance;
    } finally {
        if (!success) {
            channel.close();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Cryptomator(org.cryptomator.ui.Cryptomator) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Aggregations

Selector (java.nio.channels.Selector)186 SelectionKey (java.nio.channels.SelectionKey)84 IOException (java.io.IOException)70 SocketChannel (java.nio.channels.SocketChannel)51 InetSocketAddress (java.net.InetSocketAddress)38 ByteBuffer (java.nio.ByteBuffer)28 ServerSocketChannel (java.nio.channels.ServerSocketChannel)27 Test (org.junit.Test)14 DatagramChannel (java.nio.channels.DatagramChannel)13 ClosedChannelException (java.nio.channels.ClosedChannelException)12 CancelledKeyException (java.nio.channels.CancelledKeyException)11 NioEndpoint (org.apache.tomcat.util.net.NioEndpoint)10 ClosedSelectorException (java.nio.channels.ClosedSelectorException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 EOFException (java.io.EOFException)7 ServerSocket (java.net.ServerSocket)7 SelectableChannel (java.nio.channels.SelectableChannel)7 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)5 Iterator (java.util.Iterator)5 MemberImpl (org.apache.catalina.tribes.membership.MemberImpl)5