Search in sources :

Example 6 with Association

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

Aggregations

Association (com.sun.nio.sctp.Association)6 MessageInfo (com.sun.nio.sctp.MessageInfo)3 SctpMultiChannel (com.sun.nio.sctp.SctpMultiChannel)3 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 SocketAddress (java.net.SocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 SctpChannel (com.sun.nio.sctp.SctpChannel)2 AbstractNotificationHandler (com.sun.nio.sctp.AbstractNotificationHandler)1 HandlerResult (com.sun.nio.sctp.HandlerResult)1 InvalidStreamException (com.sun.nio.sctp.InvalidStreamException)1 SctpServerChannel (com.sun.nio.sctp.SctpServerChannel)1 Entry (java.util.Map.Entry)1