Search in sources :

Example 1 with SctpMultiChannel

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

the class Branch method doTest.

void doTest(SocketAddress peerAddress) {
    SctpMultiChannel channel = null;
    ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
    MessageInfo info = MessageInfo.createOutgoing(null, 0);
    try {
        channel = SctpMultiChannel.open();
        /* setup an association implicitly by sending a small message */
        int streamNumber = 0;
        debug("sending to " + peerAddress + " on stream number: " + streamNumber);
        info = MessageInfo.createOutgoing(peerAddress, streamNumber);
        buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
        buffer.flip();
        int position = buffer.position();
        int remaining = buffer.remaining();
        debug("sending small message: " + buffer);
        int sent = channel.send(buffer, info);
        check(sent == remaining, "sent should be equal to remaining");
        check(buffer.position() == (position + sent), "buffers position should have been incremented by sent");
        /* Receive the COMM_UP */
        buffer.clear();
        BranchNotificationHandler handler = new BranchNotificationHandler();
        info = channel.receive(buffer, null, handler);
        check(handler.receivedCommUp(), "COMM_UP no received");
        Set<Association> associations = channel.associations();
        check(!associations.isEmpty(), "There should be some associations");
        Association bassoc = associations.iterator().next();
        /* TEST 1: branch */
        SctpChannel bchannel = channel.branch(bassoc);
        check(!bchannel.getAllLocalAddresses().isEmpty(), "branched channel should be bound");
        check(!bchannel.getRemoteAddresses().isEmpty(), "branched channel should be connected");
        check(channel.associations().isEmpty(), "there should be no associations since the only one was branched off");
        buffer.clear();
        info = bchannel.receive(buffer, null, null);
        buffer.flip();
        check(info != null, "info is null");
        check(info.streamNumber() == streamNumber, "message not sent on the correct stream");
        check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").length, "bytes received not equal to message length");
        check(info.bytes() == buffer.remaining(), "bytes != remaining");
        check(Util.compare(buffer, Util.SMALL_MESSAGE), "received message not the same as sent message");
    } catch (IOException ioe) {
        unexpected(ioe);
    } finally {
        clientFinishedLatch.countDown();
        try {
            serverFinishedLatch.await(10L, TimeUnit.SECONDS);
        } catch (InterruptedException ie) {
            unexpected(ie);
        }
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                unexpected(e);
            }
        }
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) Association(com.sun.nio.sctp.Association) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SctpMultiChannel(com.sun.nio.sctp.SctpMultiChannel) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 2 with SctpMultiChannel

use of com.sun.nio.sctp.SctpMultiChannel 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 SctpMultiChannel

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

the class Send method doTest.

void doTest(SocketAddress peerAddress) {
    SctpMultiChannel channel = null;
    ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
    MessageInfo info = MessageInfo.createOutgoing(null, 0);
    try {
        channel = SctpMultiChannel.open();
        /* TEST 1: send small message */
        int streamNumber = 0;
        debug("sending to " + peerAddress + " on stream number: " + streamNumber);
        info = MessageInfo.createOutgoing(peerAddress, streamNumber);
        buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
        buffer.flip();
        int position = buffer.position();
        int remaining = buffer.remaining();
        debug("sending small message: " + buffer);
        int sent = channel.send(buffer, info);
        check(sent == remaining, "sent should be equal to remaining");
        check(buffer.position() == (position + sent), "buffers position should have been incremented by sent");
        /* TEST 2: receive the echoed message */
        buffer.clear();
        info = channel.receive(buffer, null, null);
        buffer.flip();
        check(info != null, "info is null");
        check(info.streamNumber() == streamNumber, "message not sent on the correct stream");
        check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").length, "bytes received not equal to message length");
        check(info.bytes() == buffer.remaining(), "bytes != remaining");
        check(Util.compare(buffer, Util.SMALL_MESSAGE), "received message not the same as sent message");
        /* TEST 3: send large message */
        Set<Association> assocs = channel.associations();
        check(assocs.size() == 1, "there should be only one association");
        Iterator<Association> it = assocs.iterator();
        check(it.hasNext());
        Association assoc = it.next();
        streamNumber = assoc.maxOutboundStreams() - 1;
        debug("sending on stream number: " + streamNumber);
        info = MessageInfo.createOutgoing(assoc, null, streamNumber);
        buffer.clear();
        buffer.put(Util.LARGE_MESSAGE.getBytes("ISO-8859-1"));
        buffer.flip();
        position = buffer.position();
        remaining = buffer.remaining();
        debug("sending large message: " + buffer);
        sent = channel.send(buffer, info);
        check(sent == remaining, "sent should be equal to remaining");
        check(buffer.position() == (position + sent), "buffers position should have been incremented by sent");
        /* TEST 4: receive the echoed message */
        buffer.clear();
        info = channel.receive(buffer, null, null);
        buffer.flip();
        check(info != null, "info is null");
        check(info.streamNumber() == streamNumber, "message not sent on the correct stream");
        check(info.bytes() == Util.LARGE_MESSAGE.getBytes("ISO-8859-1").length, "bytes received not equal to message length");
        check(info.bytes() == buffer.remaining(), "bytes != remaining");
        check(Util.compare(buffer, Util.LARGE_MESSAGE), "received message not the same as sent message");
        /* TEST 5: InvalidStreamExcepton */
        streamNumber = assoc.maxOutboundStreams() + 1;
        info = MessageInfo.createOutgoing(assoc, null, streamNumber);
        buffer.clear();
        buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
        buffer.flip();
        position = buffer.position();
        remaining = buffer.remaining();
        debug("sending on stream number: " + streamNumber);
        debug("sending small message: " + buffer);
        try {
            sent = channel.send(buffer, info);
            fail("should have thrown InvalidStreamExcepton");
        } catch (InvalidStreamException ise) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        check(buffer.remaining() == remaining, "remaining should not be changed");
        check(buffer.position() == position, "buffers position should not be changed");
        /* TEST 5: getRemoteAddresses(Association) */
        channel.getRemoteAddresses(assoc);
        /* TEST 6: Send from heap buffer to force implementation to
             * substitute with a native buffer, then check that its position
             * is updated correctly */
        info = MessageInfo.createOutgoing(assoc, null, 0);
        buffer.clear();
        buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
        buffer.flip();
        final int offset = 1;
        buffer.position(offset);
        remaining = buffer.remaining();
        try {
            sent = channel.send(buffer, info);
            check(sent == remaining, "sent should be equal to remaining");
            check(buffer.position() == (offset + sent), "buffers position should have been incremented by sent");
        } catch (IllegalArgumentException iae) {
            fail(iae + ", Error updating buffers position");
        }
    } catch (IOException ioe) {
        unexpected(ioe);
    } finally {
        clientFinishedLatch.countDown();
        try {
            serverFinishedLatch.await(10L, TimeUnit.SECONDS);
        } catch (InterruptedException ie) {
            unexpected(ie);
        }
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                unexpected(e);
            }
        }
    }
}
Also used : Association(com.sun.nio.sctp.Association) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) InvalidStreamException(com.sun.nio.sctp.InvalidStreamException) SctpMultiChannel(com.sun.nio.sctp.SctpMultiChannel) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 4 with SctpMultiChannel

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

the class SocketOptionTests method test.

void test(String[] args) {
    if (!Util.isSCTPSupported()) {
        out.println("SCTP protocol is not supported");
        out.println("Test cannot be run");
        return;
    }
    try {
        SctpMultiChannel smc = SctpMultiChannel.open();
        /* check supported options */
        Set<SctpSocketOption<?>> options = smc.supportedOptions();
        List<? extends SctpSocketOption<?>> expected = Arrays.<SctpSocketOption<?>>asList(SCTP_DISABLE_FRAGMENTS, SCTP_EXPLICIT_COMPLETE, SCTP_FRAGMENT_INTERLEAVE, SCTP_INIT_MAXSTREAMS, SCTP_NODELAY, SCTP_PRIMARY_ADDR, SCTP_SET_PEER_PRIMARY_ADDR, SO_SNDBUF, SO_RCVBUF, SO_LINGER);
        for (SctpSocketOption opt : expected) {
            if (!options.contains(opt))
                fail(opt.name() + " should be supported");
        }
        InitMaxStreams streams = InitMaxStreams.create(1024, 1024);
        smc.setOption(SCTP_INIT_MAXSTREAMS, streams, null);
        checkOption(smc, SCTP_INIT_MAXSTREAMS, streams);
        streams = smc.getOption(SCTP_INIT_MAXSTREAMS, null);
        check(streams.maxInStreams() == 1024, "Max in streams: value: " + streams.maxInStreams() + ", expected 1024 ");
        check(streams.maxOutStreams() == 1024, "Max out streams: value: " + streams.maxOutStreams() + ", expected 1024 ");
        optionalSupport(smc, SCTP_DISABLE_FRAGMENTS, true);
        optionalSupport(smc, SCTP_EXPLICIT_COMPLETE, true);
        optionalSupport(smc, SCTP_FRAGMENT_INTERLEAVE, 1);
        smc.setOption(SCTP_NODELAY, true, null);
        checkOption(smc, SCTP_NODELAY, true);
        smc.setOption(SO_SNDBUF, 16 * 1024, null);
        smc.setOption(SO_RCVBUF, 16 * 1024, null);
        checkOption(smc, SO_LINGER, -1);
        /* Setting SO_LINGER not support for one-to-many on Solaris */
        if (!"SunOS".equals(osName)) {
            smc.setOption(SO_LINGER, 2000, null);
            checkOption(smc, SO_LINGER, 2000);
        }
        /* SCTP_PRIMARY_ADDR */
        sctpPrimaryAddr();
        /* NullPointerException */
        try {
            smc.setOption(null, "value", null);
            fail("NullPointerException not thrown for setOption");
        } catch (NullPointerException unused) {
            pass();
        }
        try {
            smc.getOption(null, null);
            fail("NullPointerException not thrown for getOption");
        } catch (NullPointerException unused) {
            pass();
        }
        /* ClosedChannelException */
        smc.close();
        try {
            smc.setOption(SCTP_INIT_MAXSTREAMS, streams, null);
            fail("ClosedChannelException not thrown");
        } catch (ClosedChannelException unused) {
            pass();
        }
    } catch (IOException ioe) {
        unexpected(ioe);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) SctpSocketOption(com.sun.nio.sctp.SctpSocketOption) IOException(java.io.IOException) SctpMultiChannel(com.sun.nio.sctp.SctpMultiChannel)

Aggregations

SctpMultiChannel (com.sun.nio.sctp.SctpMultiChannel)4 Association (com.sun.nio.sctp.Association)3 MessageInfo (com.sun.nio.sctp.MessageInfo)3 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 SctpChannel (com.sun.nio.sctp.SctpChannel)2 InvalidStreamException (com.sun.nio.sctp.InvalidStreamException)1 SctpServerChannel (com.sun.nio.sctp.SctpServerChannel)1 SctpSocketOption (com.sun.nio.sctp.SctpSocketOption)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1