Package com.sun.nio.sctp

Examples of com.sun.nio.sctp.SctpServerChannel


  private void acceptSctp(SelectionKey key) throws IOException {

    // For an accept to be pending the channel must be a server socket
    // channel.
    SctpServerChannel serverSocketChannel = (SctpServerChannel) key.channel();

    // Accept the connection and make it non-blocking
    SctpChannel socketChannel = serverSocketChannel.accept();

    Set<SocketAddress> peerAddresses = socketChannel.getRemoteAddresses();

    this.doAccept(serverSocketChannel, socketChannel, peerAddresses);
  }
View Full Code Here


            ExecutorService executor = Executors.newCachedThreadPool();
            boolean success;
            long startTime;
            int interestOps;

            SctpServerChannel ch = null;
            SelectorLoop loop = null;

            try {
                // Open a channel.
                ch = com.sun.nio.sctp.SctpServerChannel.open();

                // Configure the channel
                try {
                    ch.bind(new InetSocketAddress(0));
                    ch.configureBlocking(false);
                } catch (Throwable e) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Failed to configure a temporary socket.", e);
                    }
                    return -1;
                }

                // Prepare the selector loop.
                try {
                    loop = new SelectorLoop();
                } catch (Throwable e) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Failed to open a temporary selector.", e);
                    }
                    return -1;
                }

                // Register the channel
                try {
                    ch.register(loop.selector, 0);
                } catch (Throwable e) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Failed to register a temporary selector.", e);
                    }
                    return -1;
                }

                SelectionKey key = ch.keyFor(loop.selector);

                // Start the selector loop.
                executor.execute(loop);

                // Level 0
                success = true;
                for (int i = 0; i < 10; i ++) {

                    // Increase the probability of calling interestOps
                    // while select() is running.
                    do {
                        while (!loop.selecting) {
                            Thread.yield();
                        }

                        // Wait a little bit more.
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            // Ignore
                        }
                    } while (!loop.selecting);

                    startTime = System.nanoTime();
                    key.interestOps(key.interestOps() | SelectionKey.OP_ACCEPT);
                    key.interestOps(key.interestOps() & ~SelectionKey.OP_ACCEPT);

                    if (System.nanoTime() - startTime >= 500000000L) {
                        success = false;
                        break;
                    }
                }

                if (success) {
                    constraintLevel = 0;
                } else {
                    // Level 1
                    success = true;
                    for (int i = 0; i < 10; i ++) {

                        // Increase the probability of calling interestOps
                        // while select() is running.
                        do {
                            while (!loop.selecting) {
                                Thread.yield();
                            }

                            // Wait a little bit more.
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                // Ignore
                            }
                        } while (!loop.selecting);

                        startTime = System.nanoTime();
                        interestOps = key.interestOps();
                        synchronized (loop) {
                            loop.selector.wakeup();
                            key.interestOps(interestOps | SelectionKey.OP_ACCEPT);
                            key.interestOps(interestOps & ~SelectionKey.OP_ACCEPT);
                        }

                        if (System.nanoTime() - startTime >= 500000000L) {
                            success = false;
                            break;
                        }
                    }
                    if (success) {
                        constraintLevel = 1;
                    } else {
                        constraintLevel = 2;
                    }
                }
            } catch (Throwable e) {
                return -1;
            } finally {
                if (ch != null) {
                    try {
                        ch.close();
                    } catch (Throwable e) {
                        if (logger.isWarnEnabled()) {
                            logger.warn("Failed to close a temporary socket.", e);
                        }
                    }
View Full Code Here

TOP

Related Classes of com.sun.nio.sctp.SctpServerChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.