Search in sources :

Example 1 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project robovm by robovm.

the class AbstractSelectableChannelTest method test_provider.

/**
     * @tests AbstractSelectableChannel#provider()
     */
public void test_provider() {
    SelectorProvider provider = testChannel.provider();
    assertSame(SelectorProvider.provider(), provider);
    testChannel = new MockSelectableChannel(null);
    provider = testChannel.provider();
    assertNull(provider);
}
Also used : SelectorProvider(java.nio.channels.spi.SelectorProvider)

Example 2 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project baseio by generallycloud.

the class SocketSelectorEventLoop method openSelector.

@SuppressWarnings("rawtypes")
private SocketSelector openSelector(SelectableChannel channel) throws IOException {
    SelectorProvider provider = SelectorProvider.provider();
    Object res = AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            try {
                return Class.forName("sun.nio.ch.SelectorImpl");
            } catch (Throwable cause) {
                return cause;
            }
        }
    });
    final Selector selector = provider.openSelector();
    if (res instanceof Throwable) {
        return new NioSocketSelector(this, channel, selector);
    }
    final Class selectorImplClass = (Class) res;
    final SelectionKeySet keySet = new SelectionKeySet();
    res = AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            try {
                Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
                Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");
                Throwable cause = ClassUtil.trySetAccessible(selectedKeysField);
                if (cause != null) {
                    return cause;
                }
                cause = ClassUtil.trySetAccessible(publicSelectedKeysField);
                if (cause != null) {
                    return cause;
                }
                selectedKeysField.set(selector, keySet);
                publicSelectedKeysField.set(selector, keySet);
                return null;
            } catch (Exception e) {
                return e;
            }
        }
    });
    if (res instanceof Throwable) {
        return new NioSocketSelector(this, channel, selector);
    }
    selectionKeySet = keySet;
    return new SelectionKeyNioSocketSelector(this, channel, selector, keySet);
}
Also used : SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) Field(java.lang.reflect.Field) SelectorProvider(java.nio.channels.spi.SelectorProvider) PrivilegedAction(java.security.PrivilegedAction) Selector(java.nio.channels.Selector)

Example 3 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project webpieces by deanhiller.

the class TestXEvaluateNIOBehavior method testNormalNio.

/**
 * This test is interesting.  If a Socket is registered with a Selector, and
 * you close the socket, the socket cannot be closed until the select statement.
 *
 * NOTE: Appears to be fixed in 1.5.0_06 on windows, so we only run this on
 * linux now!!!!!!!!!
 *
 * @throws Exception
 */
public void testNormalNio() throws Exception {
    SocketChannel channel1 = SocketChannel.open();
    channel1.configureBlocking(false);
    channel1.socket().setReuseAddress(true);
    InetAddress loopBack = InetAddress.getByName("127.0.0.1");
    InetSocketAddress addr1 = new InetSocketAddress(loopBack, 0);
    channel1.socket().bind(addr1);
    channel1.connect(svrAddr);
    channel1.finishConnect();
    mockServer.expect(MockNIOServer.CONNECTED);
    SelectorProvider prov = SelectorProvider.provider();
    AbstractSelector sel = prov.openSelector();
    log.info("about to register client");
    channel1.register(sel, SelectionKey.OP_READ);
    log.info("registered channel");
    log.info("about to close------------------");
    channel1.close();
    log.info("channel is now closed-----------");
    mockServer.expect(MockNIOServer.FAR_END_CLOSED);
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectorProvider(java.nio.channels.spi.SelectorProvider) AbstractSelector(java.nio.channels.spi.AbstractSelector) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 4 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project netty by netty.

the class NioEventLoopGroup method newChild.

@Override
protected EventLoop newChild(Executor executor, Object... args) throws Exception {
    SelectorProvider selectorProvider = (SelectorProvider) args[0];
    SelectStrategyFactory selectStrategyFactory = (SelectStrategyFactory) args[1];
    RejectedExecutionHandler rejectedExecutionHandler = (RejectedExecutionHandler) args[2];
    EventLoopTaskQueueFactory taskQueueFactory = null;
    EventLoopTaskQueueFactory tailTaskQueueFactory = null;
    int argsLength = args.length;
    if (argsLength > 3) {
        taskQueueFactory = (EventLoopTaskQueueFactory) args[3];
    }
    if (argsLength > 4) {
        tailTaskQueueFactory = (EventLoopTaskQueueFactory) args[4];
    }
    return new NioEventLoop(this, executor, selectorProvider, selectStrategyFactory.newSelectStrategy(), rejectedExecutionHandler, taskQueueFactory, tailTaskQueueFactory);
}
Also used : EventLoopTaskQueueFactory(io.netty.channel.EventLoopTaskQueueFactory) DefaultSelectStrategyFactory(io.netty.channel.DefaultSelectStrategyFactory) SelectStrategyFactory(io.netty.channel.SelectStrategyFactory) SelectorProvider(java.nio.channels.spi.SelectorProvider) RejectedExecutionHandler(io.netty.util.concurrent.RejectedExecutionHandler)

Example 5 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project j2objc by google.

the class AbstractSelectableChannelTest method test_provider.

/**
 * @tests AbstractSelectableChannel#provider()
 */
public void test_provider() {
    SelectorProvider provider = testChannel.provider();
    assertSame(SelectorProvider.provider(), provider);
    testChannel = new MockSelectableChannel(null);
    provider = testChannel.provider();
    assertNull(provider);
}
Also used : SelectorProvider(java.nio.channels.spi.SelectorProvider)

Aggregations

SelectorProvider (java.nio.channels.spi.SelectorProvider)14 InetAddress (java.net.InetAddress)3 InetSocketAddress (java.net.InetSocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 Channel (java.nio.channels.Channel)3 SocketChannel (java.nio.channels.SocketChannel)3 FileDescriptor (java.io.FileDescriptor)2 Field (java.lang.reflect.Field)2 DatagramChannel (java.nio.channels.DatagramChannel)2 Selector (java.nio.channels.Selector)2 ServerSocketChannel (java.nio.channels.ServerSocketChannel)2 PrivilegedAction (java.security.PrivilegedAction)2 DefaultSelectStrategyFactory (io.netty.channel.DefaultSelectStrategyFactory)1 EventLoopTaskQueueFactory (io.netty.channel.EventLoopTaskQueueFactory)1 SelectStrategyFactory (io.netty.channel.SelectStrategyFactory)1 RejectedExecutionHandler (io.netty.util.concurrent.RejectedExecutionHandler)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 AbstractSelector (java.nio.channels.spi.AbstractSelector)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1