Search in sources :

Example 6 with NoConnectionPendingException

use of java.nio.channels.NoConnectionPendingException in project XobotOS by xamarin.

the class SocketChannelImpl method finishConnect.

@Override
public boolean finishConnect() throws IOException {
    synchronized (this) {
        if (!isOpen()) {
            throw new ClosedChannelException();
        }
        if (status == SOCKET_STATUS_CONNECTED) {
            return true;
        }
        if (status != SOCKET_STATUS_PENDING) {
            throw new NoConnectionPendingException();
        }
    }
    boolean finished = false;
    try {
        begin();
        InetAddress inetAddress = connectAddress.getAddress();
        int port = connectAddress.getPort();
        // Return immediately.
        finished = IoBridge.isConnected(fd, inetAddress, port, 0, 0);
        isBound = finished;
    } catch (ConnectException e) {
        if (isOpen()) {
            close();
            finished = true;
        }
        throw e;
    } finally {
        end(finished);
    }
    synchronized (this) {
        status = (finished ? SOCKET_STATUS_CONNECTED : status);
        isBound = finished;
    }
    return finished;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) NoConnectionPendingException(java.nio.channels.NoConnectionPendingException) InetAddress(java.net.InetAddress) ConnectException(java.net.ConnectException)

Example 7 with NoConnectionPendingException

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

the class Connect method doTest.

void doTest() {
    SctpChannel channel = null;
    SctpServerChannel ssc = null;
    try {
        /* Create a server channel to connect to */
        ssc = SctpServerChannel.open().bind(null);
        Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
        if (addrs.isEmpty())
            debug("addrs should not be empty");
        final SocketAddress peerAddress = (InetSocketAddress) addrs.iterator().next();
        channel = SctpChannel.open();
        /* TEST 0.5 Verify default values for new/unconnected channel */
        check(channel.getRemoteAddresses().isEmpty(), "non empty set for unconnected channel");
        check(channel.association() == null, "non-null association for unconnected channel");
        check(!channel.isConnectionPending(), "should not have a connection pending");
        /* TEST 1: non-blocking connect */
        channel.configureBlocking(false);
        if (channel.connect(peerAddress) != true) {
            debug("non-blocking connect did not immediately succeed");
            check(channel.isConnectionPending(), "should return true for isConnectionPending");
            try {
                channel.connect(peerAddress);
                fail("should have thrown ConnectionPendingException");
            } catch (ConnectionPendingException cpe) {
                pass();
            } catch (IOException ioe) {
                unexpected(ioe);
            }
            channel.configureBlocking(true);
            check(channel.finishConnect(), "finishConnect should have returned true");
        }
        ssc.accept();
        ssc.close();
        /* TEST 1.5 Verify after connect */
        check(!channel.getRemoteAddresses().isEmpty(), "empty set for connected channel");
        check(channel.association() != null, "null association for connected channel");
        check(!channel.isConnectionPending(), "pending connection for connected channel");
        /* TEST 2: Verify AlreadyConnectedException thrown */
        try {
            channel.connect(peerAddress);
            fail("should have thrown AlreadyConnectedException");
        } catch (AlreadyConnectedException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 2.5: Verify AlreadyConnectedException thrown */
        try {
            channel.connect(peerAddress, 5, 5);
            fail("should have thrown AlreadyConnectedException");
        } catch (AlreadyConnectedException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 3: UnresolvedAddressException */
        channel.close();
        channel = SctpChannel.open();
        InetSocketAddress unresolved = InetSocketAddress.createUnresolved("xxyyzzabc", 4567);
        try {
            channel.connect(unresolved);
            fail("should have thrown UnresolvedAddressException");
        } catch (UnresolvedAddressException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 4: UnsupportedAddressTypeException */
        SocketAddress unsupported = new UnsupportedSocketAddress();
        try {
            channel.connect(unsupported);
            fail("should have thrown UnsupportedAddressTypeException");
        } catch (UnsupportedAddressTypeException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 5: ClosedChannelException */
        channel.close();
        final SctpChannel closedChannel = channel;
        testCCE(new Callable<Void>() {

            public Void call() throws IOException {
                closedChannel.connect(peerAddress);
                return null;
            }
        });
        /* TEST 5.5 getRemoteAddresses */
        testCCE(new Callable<Void>() {

            public Void call() throws IOException {
                closedChannel.getRemoteAddresses();
                return null;
            }
        });
        testCCE(new Callable<Void>() {

            public Void call() throws IOException {
                closedChannel.association();
                return null;
            }
        });
        check(!channel.isConnectionPending(), "pending connection for closed channel");
        /* Run some more finishConnect tests */
        /* TEST 6: NoConnectionPendingException */
        channel = SctpChannel.open();
        try {
            channel.finishConnect();
            fail("should have thrown NoConnectionPendingException");
        } catch (NoConnectionPendingException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        /* TEST 7: ClosedChannelException */
        channel.close();
        final SctpChannel cceChannel = channel;
        testCCE(new Callable<Void>() {

            public Void call() throws IOException {
                cceChannel.finishConnect();
                return null;
            }
        });
        /* TEST 8: IOException: Connection refused. Exercises handleSocketError.
             *         Assumption: no sctp socket listening on 3456 */
        SocketAddress addr = new InetSocketAddress("localhost", 3456);
        channel = SctpChannel.open();
        try {
            channel.connect(addr);
            fail("should have thrown ConnectException: Connection refused");
        } catch (IOException ioe) {
            pass();
        }
    } catch (IOException ioe) {
        unexpected(ioe);
    } finally {
        try {
            if (channel != null)
                channel.close();
        } catch (IOException unused) {
        }
        try {
            if (ssc != null)
                ssc.close();
        } catch (IOException unused) {
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) NoConnectionPendingException(java.nio.channels.NoConnectionPendingException) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) InetSocketAddress(java.net.InetSocketAddress) NoConnectionPendingException(java.nio.channels.NoConnectionPendingException) ConnectionPendingException(java.nio.channels.ConnectionPendingException) SctpServerChannel(com.sun.nio.sctp.SctpServerChannel) IOException(java.io.IOException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 8 with NoConnectionPendingException

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

the class NoConnectionPendingExceptionTest method test_Constructor.

/**
     * @tests {@link java.nio.channels.NoConnectionPendingException#NoConnectionPendingException()}
     */
public void test_Constructor() {
    NoConnectionPendingException e = new NoConnectionPendingException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
Also used : NoConnectionPendingException(java.nio.channels.NoConnectionPendingException)

Example 9 with NoConnectionPendingException

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

the class SctpChannelImpl method finishConnect.

@Override
public boolean finishConnect() throws IOException {
    synchronized (receiveLock) {
        synchronized (sendLock) {
            synchronized (stateLock) {
                if (!isOpen())
                    throw new ClosedChannelException();
                if (isConnected())
                    return true;
                if (state != ChannelState.PENDING)
                    throw new NoConnectionPendingException();
            }
            int n = 0;
            try {
                try {
                    begin();
                    synchronized (blockingLock()) {
                        synchronized (stateLock) {
                            if (!isOpen()) {
                                return false;
                            }
                            receiverThread = NativeThread.current();
                        }
                        if (!isBlocking()) {
                            for (; ; ) {
                                n = checkConnect(fd, false, readyToConnect);
                                if ((n == IOStatus.INTERRUPTED) && isOpen())
                                    continue;
                                break;
                            }
                        } else {
                            for (; ; ) {
                                n = checkConnect(fd, true, readyToConnect);
                                if (n == 0) {
                                    // spurious notifications
                                    continue;
                                }
                                if ((n == IOStatus.INTERRUPTED) && isOpen())
                                    continue;
                                break;
                            }
                        }
                    }
                } finally {
                    synchronized (stateLock) {
                        receiverThread = 0;
                        if (state == ChannelState.KILLPENDING) {
                            kill();
                            /* poll()/getsockopt() does not report
                                 * error (throws exception, with n = 0)
                                 * on Linux platform after dup2 and
                                 * signal-wakeup. Force n to 0 so the
                                 * end() can throw appropriate exception */
                            n = 0;
                        }
                    }
                    end((n > 0) || (n == IOStatus.UNAVAILABLE));
                    assert IOStatus.check(n);
                }
            } catch (IOException x) {
                /* If an exception was thrown, close the channel after
                     * invoking end() so as to avoid bogus
                     * AsynchronousCloseExceptions */
                close();
                throw x;
            }
            if (n > 0) {
                synchronized (stateLock) {
                    state = ChannelState.CONNECTED;
                    if (!isBound()) {
                        InetSocketAddress boundIsa = Net.localAddress(fd);
                        port = boundIsa.getPort();
                    }
                    /* Receive COMM_UP */
                    ByteBuffer buf = Util.getTemporaryDirectBuffer(50);
                    try {
                        receive(buf, null, null, true);
                    } finally {
                        Util.releaseTemporaryDirectBuffer(buf);
                    }
                    /* cache remote addresses */
                    try {
                        remoteAddresses = getRemoteAddresses();
                    } catch (IOException unused) {
                    /* swallow exception */
                    }
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) NoConnectionPendingException(java.nio.channels.NoConnectionPendingException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

NoConnectionPendingException (java.nio.channels.NoConnectionPendingException)9 ClosedChannelException (java.nio.channels.ClosedChannelException)5 ByteBuffer (java.nio.ByteBuffer)4 IOException (java.io.IOException)3 ConnectException (java.net.ConnectException)3 InetAddress (java.net.InetAddress)3 InetSocketAddress (java.net.InetSocketAddress)3 AlreadyConnectedException (java.nio.channels.AlreadyConnectedException)3 SctpChannel (com.sun.nio.sctp.SctpChannel)1 SctpServerChannel (com.sun.nio.sctp.SctpServerChannel)1 SocketAddress (java.net.SocketAddress)1 ConnectionPendingException (java.nio.channels.ConnectionPendingException)1 NotYetConnectedException (java.nio.channels.NotYetConnectedException)1 SelectionKey (java.nio.channels.SelectionKey)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)1 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)1 ArrayList (java.util.ArrayList)1 Command (org.openhab.core.types.Command)1