Search in sources :

Example 11 with MessageInfo

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

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

the class ReceiveIntoDirect method doTest.

void doTest(SocketAddress peerAddress, int bufferSize, int bufferOffset) throws IOException {
    debug("\n\nTesting with bufferSize " + bufferSize + " and offset " + bufferOffset);
    assert bufferOffset + msgBytes.length <= bufferSize : "buffer offset + message length greater than buffer size ";
    ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
    MessageInfo info;
    try (SctpChannel channel = SctpChannel.open()) {
        channel.connect(peerAddress);
        ReceiveNotificationHandler handler = new ReceiveNotificationHandler();
        /* TEST 1: Assoc/peer change notif into direct buffer with offest */
        do {
            debug("Test 1: Assoc/peer change with offset " + bufferOffset);
            buffer.position(bufferOffset);
            info = channel.receive(buffer, null, handler);
            if (info == null) {
                fail("unexpected null from receive");
                return;
            }
        } while (!info.isComplete());
        buffer.flip().position(bufferOffset);
        check(handler.receivedCommUp(), "SCTP_COMM_UP not received");
        check(info != null, "info is null");
        check(info.address() != null, "address is null");
        check(info.association() != null, "association is null");
        check(info.isComplete(), "message is not complete");
        check(info.isUnordered() != true, "message should not be unordered");
        check(info.streamNumber() >= 0, "invalid stream number");
        check(info.bytes() == msgBytes.length, "bytes received not equal to message length");
        check(info.bytes() == buffer.remaining(), "bytes != remaining");
        check(Util.compare(buffer, msgBytes), "received message not the same as sent message");
        /* TEST 2: shutdown notification with offset */
        debug("Test 2: shutdown notif with offset " + bufferOffset);
        buffer.clear().position(bufferOffset);
        while ((info = channel.receive(buffer, null, handler)) != null && info.bytes() != -1) ;
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo)

Aggregations

MessageInfo (com.sun.nio.sctp.MessageInfo)12 ByteBuffer (java.nio.ByteBuffer)11 SctpChannel (com.sun.nio.sctp.SctpChannel)7 ByteBuf (io.netty.buffer.ByteBuf)4 SctpMessage (io.netty.channel.sctp.SctpMessage)4 IOException (java.io.IOException)4 Association (com.sun.nio.sctp.Association)3 SctpMultiChannel (com.sun.nio.sctp.SctpMultiChannel)3 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 NotYetConnectedException (java.nio.channels.NotYetConnectedException)2 IllegalUnbindException (com.sun.nio.sctp.IllegalUnbindException)1 InvalidStreamException (com.sun.nio.sctp.InvalidStreamException)1 SctpServerChannel (com.sun.nio.sctp.SctpServerChannel)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 AlreadyBoundException (java.nio.channels.AlreadyBoundException)1 SelectionKey (java.nio.channels.SelectionKey)1 Iterator (java.util.Iterator)1