Search in sources :

Example 21 with SelectableChannel

use of java.nio.channels.SelectableChannel in project robovm by robovm.

the class AbstractSelectableChannelTest method test_configureBlocking_Z_IllegalBlockingMode.

/**
     * @tests AbstractSelectableChannel#configureBlocking(boolean)
     */
public void test_configureBlocking_Z_IllegalBlockingMode() throws Exception {
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    SelectionKey acceptKey = sc.register(acceptSelector, SelectionKey.OP_READ, null);
    assertEquals(sc.keyFor(acceptSelector), acceptKey);
    SelectableChannel getChannel = sc.configureBlocking(false);
    assertEquals(getChannel, sc);
    try {
        sc.configureBlocking(true);
        fail("Should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) SelectableChannel(java.nio.channels.SelectableChannel) AbstractSelectableChannel(java.nio.channels.spi.AbstractSelectableChannel) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) Selector(java.nio.channels.Selector)

Example 22 with SelectableChannel

use of java.nio.channels.SelectableChannel in project heron by twitter.

the class HeronServerTest method testHandleError.

/**
   * Method: handleError(SelectableChannel channel)
   */
@Test
public void testHandleError() throws Exception {
    runBase();
    Map<SocketChannel, SocketChannelHelper> activeConnections = heronServer.getActiveConnections();
    SelectableChannel channel = activeConnections.keySet().iterator().next();
    heronServer.handleError(channel);
    Assert.assertEquals(0, activeConnections.size());
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectableChannel(java.nio.channels.SelectableChannel) Test(org.junit.Test)

Example 23 with SelectableChannel

use of java.nio.channels.SelectableChannel in project jeromq by zeromq.

the class ZPoller method add.

// add an item to this poller
protected boolean add(Object socketOrChannel, final ItemHolder holder) {
    if (socketOrChannel == null) {
        Socket socket = holder.socket();
        SelectableChannel ch = holder.item().getRawSocket();
        if (socket == null) {
            // not a socket
            assert (ch != null);
            socketOrChannel = ch;
        }
        if (ch == null) {
            // not a channel
            assert (socket != null);
            socketOrChannel = socket;
        }
    }
    assert (socketOrChannel != null);
    Set<ItemHolder> holders = items.get(socketOrChannel);
    if (holders == null) {
        holders = createContainer(1);
        items.put(socketOrChannel, holders);
    }
    final boolean rc = holders.add(holder);
    if (rc) {
        all.add(holder);
    }
    return rc;
}
Also used : SelectableChannel(java.nio.channels.SelectableChannel) Socket(org.zeromq.ZMQ.Socket)

Example 24 with SelectableChannel

use of java.nio.channels.SelectableChannel in project jdk8u_jdk by JetBrains.

the class FileChannelImpl method transferToDirectly.

private long transferToDirectly(long position, int icount, WritableByteChannel target) throws IOException {
    if (!transferSupported)
        return IOStatus.UNSUPPORTED;
    FileDescriptor targetFD = null;
    if (target instanceof FileChannelImpl) {
        if (!fileSupported)
            return IOStatus.UNSUPPORTED_CASE;
        targetFD = ((FileChannelImpl) target).fd;
    } else if (target instanceof SelChImpl) {
        // Direct transfer to pipe causes EINVAL on some configurations
        if ((target instanceof SinkChannelImpl) && !pipeSupported)
            return IOStatus.UNSUPPORTED_CASE;
        // Platform-specific restrictions. Now there is only one:
        // Direct transfer to non-blocking channel could be forbidden
        SelectableChannel sc = (SelectableChannel) target;
        if (!nd.canTransferToDirectly(sc))
            return IOStatus.UNSUPPORTED_CASE;
        targetFD = ((SelChImpl) target).getFD();
    }
    if (targetFD == null)
        return IOStatus.UNSUPPORTED;
    int thisFDVal = IOUtil.fdVal(fd);
    int targetFDVal = IOUtil.fdVal(targetFD);
    if (// Not supported on some configurations
    thisFDVal == targetFDVal)
        return IOStatus.UNSUPPORTED;
    if (nd.transferToDirectlyNeedsPositionLock()) {
        synchronized (positionLock) {
            long pos = position();
            try {
                return transferToDirectlyInternal(position, icount, target, targetFD);
            } finally {
                position(pos);
            }
        }
    } else {
        return transferToDirectlyInternal(position, icount, target, targetFD);
    }
}
Also used : SelectableChannel(java.nio.channels.SelectableChannel) FileDescriptor(java.io.FileDescriptor)

Example 25 with SelectableChannel

use of java.nio.channels.SelectableChannel in project geode by apache.

the class ServerConnection method makeBlocking.

/**
   * Switch this guy to blocking mode so we can use oldIO to read and write msgs.
   */
public void makeBlocking() throws IOException {
    // logger.info("DEBUG: makeBlocking " + this);
    // if (this.sKey != null) {
    // this.sKey = null;
    // }
    SelectableChannel c = this.theSocket.getChannel();
    c.configureBlocking(true);
}
Also used : SelectableChannel(java.nio.channels.SelectableChannel)

Aggregations

SelectableChannel (java.nio.channels.SelectableChannel)25 SelectionKey (java.nio.channels.SelectionKey)13 IOException (java.io.IOException)5 Selector (java.nio.channels.Selector)4 SocketChannel (java.nio.channels.SocketChannel)4 Test (org.junit.Test)4 SocketTimeoutException (java.net.SocketTimeoutException)3 CancelledKeyException (java.nio.channels.CancelledKeyException)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 ServerSocketChannel (java.nio.channels.ServerSocketChannel)3 Socket (org.zeromq.ZMQ.Socket)3 ConnectException (java.net.ConnectException)2 InetSocketAddress (java.net.InetSocketAddress)2 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)2 ChannelException (io.netty.channel.ChannelException)1 EventLoopException (io.netty.channel.EventLoopException)1 FileDescriptor (java.io.FileDescriptor)1 Socket (java.net.Socket)1