Search in sources :

Example 1 with MessageInfo

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

the class Receive method doTest.

void doTest(SocketAddress peerAddress) {
    SctpChannel channel = null;
    ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
    MessageInfo info;
    try {
        channel = SctpChannel.open();
        ReceiveNotificationHandler handler = new ReceiveNotificationHandler(channel);
        /* TEST 1: Verify NotYetConnectedException thrown */
        try {
            channel.receive(buffer, null, handler);
            fail("should have thrown NotYetConnectedException");
        } catch (NotYetConnectedException unused) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        channel.connect(peerAddress);
        /* TEST 2: receive small message */
        do {
            debug("Test 2: invoking receive");
            info = channel.receive(buffer, null, handler);
            if (info == null) {
                fail("unexpected null from receive");
                return;
            }
        } while (!info.isComplete());
        buffer.flip();
        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.payloadProtocolID() == PPID, "PPID incorrect");
        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");
        buffer.clear();
        /* TEST 3: receive large message */
        do {
            debug("Test 3: invoking receive");
            info = channel.receive(buffer, null, handler);
            if (info == null) {
                fail("unexpected null from receive");
                return;
            }
        } while (!info.isComplete());
        buffer.flip();
        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() == 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");
        buffer.clear();
        /* TEST 4: EOF */
        // buffer position 0
        buffer.clear();
        info = channel.receive(buffer, null, handler);
        check(info != null, "info is null");
        check(info.bytes() == -1, "should have received EOF");
        check(buffer.position() == 0, "buffer position should be unchanged");
        /* TEST 5: ClosedChannelException */
        channel.close();
        try {
            channel.receive(buffer, null, null);
            fail("should have thrown ClosedChannelException");
        } catch (ClosedChannelException cce) {
            pass();
        } catch (IOException ioe) {
            unexpected(ioe);
        }
        handler = null;
        /* TEST 6: handler returns RETURN after handling a notification */
        ReceiveNotificationHandler handler2 = new ReceiveNotificationHandler(null);
        /* HandlerResult.RETURN */
        channel = SctpChannel.open(peerAddress, 0, 0);
        info = channel.receive(buffer, null, handler2);
        check(info == null, "channel should return null");
        check(handler2.receivedCommUp(), "SCTP_COMM_UP not received");
        check(buffer.position() == 0, "buffer position should be unchanged");
        /* TEST 7: Non blocking channel return null if no data */
        channel.configureBlocking(false);
        info = channel.receive(buffer, null, null);
        check(info == null, "non-blocking channel should return null");
        check(buffer.position() == 0, "buffer position should be unchanged");
    } 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) ClosedChannelException(java.nio.channels.ClosedChannelException) NotYetConnectedException(java.nio.channels.NotYetConnectedException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 2 with MessageInfo

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

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

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

the class MessageInfoTests method test.

void test(String[] args) {
    /* TEST 1 : createOutGoing(SocketAddress,int) */
    MessageInfo info = MessageInfo.createOutgoing(addr, DEFAULT_STREAM_NUMBER);
    checkDefaults(info);
    checkGetterSetters(info);
    /* TEST 2 : createOutGoing(Association,SocketAddress,int) */
    info = MessageInfo.createOutgoing(assoc, addr, DEFAULT_STREAM_NUMBER);
    checkDefaults(info);
    check(info.association().equals(assoc), "incorrect association");
    checkGetterSetters(info);
    /* TEST 3: null values */
    info = MessageInfo.createOutgoing(null, 0);
    check(info.address() == null, "address should be null");
    check(info.association() == null, "association should be null");
    info = MessageInfo.createOutgoing(assoc, null, 0);
    check(info.address() == null, "address should be null");
    /* Test 4: IllegalArgumentException */
    testIAE(new Runnable() {

        public void run() {
            MessageInfo.createOutgoing(addr, -1);
        }
    });
    testIAE(new Runnable() {

        public void run() {
            MessageInfo.createOutgoing(addr, 65537);
        }
    });
    testIAE(new Runnable() {

        public void run() {
            MessageInfo.createOutgoing(null, addr, 0);
        }
    });
    testIAE(new Runnable() {

        public void run() {
            MessageInfo.createOutgoing(assoc, addr, -1);
        }
    });
    testIAE(new Runnable() {

        public void run() {
            MessageInfo.createOutgoing(assoc, addr, 65537);
        }
    });
    final MessageInfo iaeInfo = MessageInfo.createOutgoing(assoc, addr, 0);
    testIAE(new Runnable() {

        public void run() {
            iaeInfo.streamNumber(-1);
        }
    });
    testIAE(new Runnable() {

        public void run() {
            iaeInfo.streamNumber(65537);
        }
    });
}
Also used : MessageInfo(com.sun.nio.sctp.MessageInfo)

Example 5 with MessageInfo

use of com.sun.nio.sctp.MessageInfo in project netty by netty.

the class OioSctpChannel method doWrite.

@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
    if (!writeSelector.isOpen()) {
        return;
    }
    final int size = in.size();
    final int selectedKeys = writeSelector.select(SO_TIMEOUT);
    if (selectedKeys > 0) {
        final Set<SelectionKey> writableKeys = writeSelector.selectedKeys();
        if (writableKeys.isEmpty()) {
            return;
        }
        Iterator<SelectionKey> writableKeysIt = writableKeys.iterator();
        int written = 0;
        for (; ; ) {
            if (written == size) {
                // all written
                return;
            }
            writableKeysIt.next();
            writableKeysIt.remove();
            SctpMessage packet = (SctpMessage) in.current();
            if (packet == null) {
                return;
            }
            ByteBuf data = packet.content();
            int dataLen = data.readableBytes();
            ByteBuffer nioData;
            if (data.nioBufferCount() != -1) {
                nioData = data.nioBuffer();
            } else {
                nioData = ByteBuffer.allocate(dataLen);
                data.getBytes(data.readerIndex(), nioData);
                nioData.flip();
            }
            final MessageInfo mi = MessageInfo.createOutgoing(association(), null, packet.streamIdentifier());
            mi.payloadProtocolID(packet.protocolIdentifier());
            mi.streamNumber(packet.streamIdentifier());
            mi.unordered(packet.isUnordered());
            ch.send(nioData, mi);
            written++;
            in.remove();
            if (!writableKeysIt.hasNext()) {
                return;
            }
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ByteBuf(io.netty.buffer.ByteBuf) SctpMessage(io.netty.channel.sctp.SctpMessage) 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