Search in sources :

Example 1 with SctpChannel

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

the class OioSctpServerChannel method doReadMessages.

@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    if (!isActive()) {
        return -1;
    }
    SctpChannel s = null;
    int acceptedChannels = 0;
    try {
        final int selectedKeys = selector.select(SO_TIMEOUT);
        if (selectedKeys > 0) {
            final Iterator<SelectionKey> selectionKeys = selector.selectedKeys().iterator();
            for (; ; ) {
                SelectionKey key = selectionKeys.next();
                selectionKeys.remove();
                if (key.isAcceptable()) {
                    s = sch.accept();
                    if (s != null) {
                        buf.add(new OioSctpChannel(this, s));
                        acceptedChannels++;
                    }
                }
                if (!selectionKeys.hasNext()) {
                    return acceptedChannels;
                }
            }
        }
    } catch (Throwable t) {
        logger.warn("Failed to create a new channel from an accepted sctp channel.", t);
        if (s != null) {
            try {
                s.close();
            } catch (Throwable t2) {
                logger.warn("Failed to close a sctp channel.", t2);
            }
        }
    }
    return acceptedChannels;
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) SelectionKey(java.nio.channels.SelectionKey)

Example 2 with SctpChannel

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

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

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

the class CommUp method doClient.

void doClient(SocketAddress peerAddress) {
    SctpChannel sc = null;
    try {
        debug("connecting to " + peerAddress);
        sc = SctpChannel.open();
        sc.configureBlocking(false);
        check(sc.isBlocking() == false, "Should be in non-blocking mode");
        sc.connect(peerAddress);
        Selector selector = Selector.open();
        SelectionKey selectiontKey = sc.register(selector, OP_CONNECT);
        /* Expect two interest Ops */
        boolean opConnectReceived = false;
        boolean opReadReceived = false;
        for (int z = 0; z < 2; z++) {
            debug("select " + z);
            int keysAdded = selector.select(TIMEOUT);
            debug("returned " + keysAdded + " keys");
            if (keysAdded > 0) {
                Set<SelectionKey> keys = selector.selectedKeys();
                Iterator<SelectionKey> i = keys.iterator();
                while (i.hasNext()) {
                    SelectionKey sk = i.next();
                    i.remove();
                    SctpChannel readyChannel = (SctpChannel) sk.channel();
                    /* OP_CONNECT */
                    if (sk.isConnectable()) {
                        /* some trivial checks */
                        check(opConnectReceived == false, "should only received one OP_CONNECT");
                        check(opReadReceived == false, "should not receive OP_READ before OP_CONNECT");
                        check(readyChannel.equals(sc), "channels should be equal");
                        check(!sk.isAcceptable(), "key should not be acceptable");
                        check(!sk.isReadable(), "key should not be readable");
                        check(!sk.isWritable(), "key should not be writable");
                        /* now process the OP_CONNECT */
                        opConnectReceived = true;
                        check((sk.interestOps() & OP_CONNECT) == OP_CONNECT, "selection key interest ops should contain OP_CONNECT");
                        sk.interestOps(OP_READ);
                        check((sk.interestOps() & OP_CONNECT) != OP_CONNECT, "selection key interest ops should not contain OP_CONNECT");
                        check(sc.finishConnect(), "finishConnect should return true");
                    } else /* OP_READ */
                    if (sk.isReadable()) {
                        /* some trivial checks */
                        check(opConnectReceived == true, "should receive one OP_CONNECT before OP_READ");
                        check(opReadReceived == false, "should not receive OP_READ before OP_CONNECT");
                        check(readyChannel.equals(sc), "channels should be equal");
                        check(!sk.isAcceptable(), "key should not be acceptable");
                        check(sk.isReadable(), "key should be readable");
                        check(!sk.isWritable(), "key should not be writable");
                        check(!sk.isConnectable(), "key should not be connectable");
                        /* now process the OP_READ */
                        opReadReceived = true;
                        selectiontKey.cancel();
                        /* try with small buffer to see if native
                             * implementation can handle this */
                        ByteBuffer buffer = ByteBuffer.allocateDirect(1);
                        readyChannel.receive(buffer, null, clientHandler);
                        check(clientHandler.receivedCommUp(), "Client should have received COMM_UP");
                        /* dont close (or put anything on) the channel until
                             * we check that the server's accepted channel also
                             * received COMM_UP */
                        serverHandler.waitForCommUp();
                    } else {
                        fail("Unexpected selection key");
                    }
                }
            } else {
                fail("Client selector returned 0 ready keys");
                /* stop the server */
                server.thread().interrupt();
            }
        }
    //for
    } catch (IOException ioe) {
        unexpected(ioe);
    } catch (InterruptedException ie) {
        unexpected(ie);
    }
}
Also used : SctpChannel(com.sun.nio.sctp.SctpChannel) SelectionKey(java.nio.channels.SelectionKey) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Selector(java.nio.channels.Selector)

Example 5 with SctpChannel

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

Aggregations

SctpChannel (com.sun.nio.sctp.SctpChannel)17 IOException (java.io.IOException)8 ByteBuffer (java.nio.ByteBuffer)8 MessageInfo (com.sun.nio.sctp.MessageInfo)7 InetSocketAddress (java.net.InetSocketAddress)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 SctpServerChannel (com.sun.nio.sctp.SctpServerChannel)3 SocketAddress (java.net.SocketAddress)3 Association (com.sun.nio.sctp.Association)2 IllegalUnbindException (com.sun.nio.sctp.IllegalUnbindException)2 SctpMultiChannel (com.sun.nio.sctp.SctpMultiChannel)2 AlreadyBoundException (java.nio.channels.AlreadyBoundException)2 AlreadyConnectedException (java.nio.channels.AlreadyConnectedException)2 NotYetConnectedException (java.nio.channels.NotYetConnectedException)2 SelectionKey (java.nio.channels.SelectionKey)2 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)2 ByteBuf (io.netty.buffer.ByteBuf)1 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)1 SctpMessage (io.netty.channel.sctp.SctpMessage)1 FileDescriptor (java.io.FileDescriptor)1