Search in sources :

Example 1 with SctpServerChannel

use of com.sun.nio.sctp.SctpServerChannel 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 2 with SctpServerChannel

use of com.sun.nio.sctp.SctpServerChannel in project jdk8u_jdk by JetBrains.

the class SocketOptionTests method sctpPrimaryAddr.

/* SCTP_PRIMARY_ADDR */
void sctpPrimaryAddr() throws IOException {
    SocketAddress addrToSet = null;
    ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);
    System.out.println("TESTING SCTP_PRIMARY_ADDR");
    /* create listening channel */
    SctpServerChannel ssc = SctpServerChannel.open().bind(null);
    Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
    if (addrs.isEmpty())
        debug("addrs should not be empty");
    InetSocketAddress serverAddr = (InetSocketAddress) addrs.iterator().next();
    /* setup an association implicitly by sending a small message */
    int streamNumber = 0;
    debug("sending to " + serverAddr + " on stream number: " + streamNumber);
    MessageInfo info = MessageInfo.createOutgoing(serverAddr, streamNumber);
    buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
    buffer.flip();
    debug("sending small message: " + buffer);
    SctpMultiChannel smc = SctpMultiChannel.open();
    int sent = smc.send(buffer, info);
    /* Receive the COMM_UP */
    buffer.clear();
    SOTNotificationHandler handler = new SOTNotificationHandler();
    info = smc.receive(buffer, null, handler);
    check(handler.receivedCommUp(), "COMM_UP no received");
    Set<Association> associations = smc.associations();
    check(!associations.isEmpty(), "There should be some associations");
    Association assoc = associations.iterator().next();
    SctpChannel peerChannel = ssc.accept();
    ssc.close();
    Set<SocketAddress> peerAddrs = peerChannel.getAllLocalAddresses();
    debug("Peer local Addresses: ");
    for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {
        InetSocketAddress addr = (InetSocketAddress) it.next();
        debug("\t" + addr);
        // any of the peer addresses will do!
        addrToSet = addr;
    }
    /* retrieval of SCTP_PRIMARY_ADDR is not supported on Solaris */
    if ("SunOS".equals(osName)) {
        //smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);
        return;
    } else {
        /* Linux */
        SocketAddress primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);
        System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);
        /* Verify that this is one of the peer addresses */
        boolean found = false;
        // may not have more than one addr
        addrToSet = primaryAddr;
        for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {
            InetSocketAddress addr = (InetSocketAddress) it.next();
            if (addr.equals(primaryAddr)) {
                found = true;
            }
            addrToSet = addr;
        }
        check(found, "SCTP_PRIMARY_ADDR returned bogus address!");
        System.out.println("Try SCTP_PRIMARY_ADDR set to: " + addrToSet);
        smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);
        System.out.println("SCTP_PRIMARY_ADDR set to: " + addrToSet);
        primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);
        System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);
        check(addrToSet.equals(primaryAddr), "SCTP_PRIMARY_ADDR not set correctly");
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) InetSocketAddress(java.net.InetSocketAddress) SctpServerChannel(com.sun.nio.sctp.SctpServerChannel) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo) Association(com.sun.nio.sctp.Association) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SctpMultiChannel(com.sun.nio.sctp.SctpMultiChannel)

Example 3 with SctpServerChannel

use of com.sun.nio.sctp.SctpServerChannel in project jdk8u_jdk by JetBrains.

the class Bind method connectChannel.

SctpChannel connectChannel(SctpChannel channel) throws IOException {
    debug("connecting channel...");
    try {
        SctpServerChannel ssc = SctpServerChannel.open();
        ssc.bind(null);
        Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
        Iterator<SocketAddress> iterator = addrs.iterator();
        SocketAddress addr = iterator.next();
        debug("using " + addr + "...");
        channel.connect(addr);
        SctpChannel peerChannel = ssc.accept();
        ssc.close();
        debug("connected");
        return peerChannel;
    } catch (IOException ioe) {
        debug("Cannot connect channel");
        unexpected(ioe);
        throw ioe;
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) SctpServerChannel(com.sun.nio.sctp.SctpServerChannel)

Aggregations

SctpChannel (com.sun.nio.sctp.SctpChannel)3 SctpServerChannel (com.sun.nio.sctp.SctpServerChannel)3 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 Association (com.sun.nio.sctp.Association)1 MessageInfo (com.sun.nio.sctp.MessageInfo)1 SctpMultiChannel (com.sun.nio.sctp.SctpMultiChannel)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 AlreadyConnectedException (java.nio.channels.AlreadyConnectedException)1 ConnectionPendingException (java.nio.channels.ConnectionPendingException)1 NoConnectionPendingException (java.nio.channels.NoConnectionPendingException)1 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)1 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)1