1427N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1427N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1427N/A *
1427N/A * This code is free software; you can redistribute it and/or modify it
1427N/A * under the terms of the GNU General Public License version 2 only, as
1427N/A * published by the Free Software Foundation.
1427N/A *
1427N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1427N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1427N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1427N/A * version 2 for more details (a copy is included in the LICENSE file that
1427N/A * accompanied this code).
1427N/A *
1427N/A * You should have received a copy of the GNU General Public License version
1427N/A * 2 along with this work; if not, write to the Free Software Foundation,
1427N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1427N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1427N/A */
1427N/A
1427N/A/* @test
1427N/A * @bug 6863110
1427N/A * @summary Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
1427N/A * @author chegar
1427N/A */
1427N/A
1427N/Aimport java.net.InetSocketAddress;
1427N/Aimport java.net.SocketAddress;
1427N/Aimport java.io.IOException;
1427N/Aimport java.util.Iterator;
1427N/Aimport java.util.Set;
1427N/Aimport java.util.concurrent.CountDownLatch;
1427N/Aimport java.nio.ByteBuffer;
1427N/Aimport java.nio.channels.Selector;
1427N/Aimport java.nio.channels.SelectionKey;
1427N/Aimport com.sun.nio.sctp.AbstractNotificationHandler;
1427N/Aimport com.sun.nio.sctp.AssociationChangeNotification;
1427N/Aimport com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent;
1427N/Aimport com.sun.nio.sctp.HandlerResult;
1427N/Aimport com.sun.nio.sctp.Notification;
1427N/Aimport com.sun.nio.sctp.SctpChannel;
1427N/Aimport com.sun.nio.sctp.SctpServerChannel;
1427N/Aimport com.sun.nio.sctp.ShutdownNotification;
1427N/Aimport static java.lang.System.out;
1427N/Aimport static java.lang.System.err;
1427N/Aimport static java.nio.channels.SelectionKey.OP_CONNECT;
1427N/Aimport static java.nio.channels.SelectionKey.OP_READ;
1427N/A
1427N/Apublic class CommUp {
1427N/A static CountDownLatch acceptLatch = new CountDownLatch(1);
1427N/A static final int TIMEOUT = 10000;
1427N/A
1427N/A CommUpNotificationHandler clientHandler = new CommUpNotificationHandler();
1427N/A CommUpNotificationHandler serverHandler = new CommUpNotificationHandler();
1427N/A CommUpServer server;
1427N/A Thread clientThread;
1427N/A
1427N/A void test(String[] args) {
1427N/A SocketAddress address = null;
1427N/A
1427N/A if (!Util.isSCTPSupported()) {
1427N/A out.println("SCTP protocol is not supported");
1427N/A out.println("Test cannot be run");
1427N/A return;
1427N/A }
1427N/A
1427N/A if (args.length == 2) {
1427N/A /* requested to connecct to a specific address */
1427N/A try {
1427N/A int port = Integer.valueOf(args[1]);
1427N/A address = new InetSocketAddress(args[0], port);
1427N/A } catch (NumberFormatException nfe) {
1427N/A err.println(nfe);
1427N/A }
1427N/A } else {
1427N/A /* start server on local machine, default */
1427N/A try {
1427N/A server = new CommUpServer();
1427N/A server.start();
1427N/A address = server.address();
1427N/A debug("Server started and listening on " + address);
1427N/A } catch (IOException ioe) {
1427N/A ioe.printStackTrace();
1427N/A return;
1427N/A }
1427N/A }
1427N/A
1427N/A /* store the main thread so that the server can interrupt it, if necessary */
1427N/A clientThread = Thread.currentThread();
1427N/A
1427N/A doClient(address);
1427N/A }
1427N/A
1427N/A void doClient(SocketAddress peerAddress) {
1427N/A SctpChannel sc = null;
1427N/A try {
1427N/A debug("connecting to " + peerAddress);
1427N/A sc = SctpChannel.open();
1427N/A sc.configureBlocking(false);
1427N/A check(sc.isBlocking() == false, "Should be in non-blocking mode");
1427N/A sc.connect(peerAddress);
1427N/A
1427N/A Selector selector = Selector.open();
1427N/A SelectionKey selectiontKey = sc.register(selector, OP_CONNECT);
1427N/A
1427N/A /* Expect two interest Ops */
1427N/A boolean opConnectReceived = false;
1427N/A boolean opReadReceived = false;
1427N/A for (int z=0; z<2; z++) {
1427N/A debug("select " + z);
1427N/A int keysAdded = selector.select(TIMEOUT);
1427N/A debug("returned " + keysAdded + " keys");
1427N/A if (keysAdded > 0) {
1427N/A Set<SelectionKey> keys = selector.selectedKeys();
1427N/A Iterator<SelectionKey> i = keys.iterator();
1427N/A while(i.hasNext()) {
1427N/A SelectionKey sk = i.next();
1427N/A i.remove();
1427N/A SctpChannel readyChannel =
1427N/A (SctpChannel)sk.channel();
1427N/A
1427N/A /* OP_CONNECT */
1427N/A if (sk.isConnectable()) {
1427N/A /* some trivial checks */
1427N/A check(opConnectReceived == false,
1427N/A "should only received one OP_CONNECT");
1427N/A check(opReadReceived == false,
1427N/A "should not receive OP_READ before OP_CONNECT");
1427N/A check(readyChannel.equals(sc),
1427N/A "channels should be equal");
1427N/A check(!sk.isAcceptable(),
1427N/A "key should not be acceptable");
1427N/A check(!sk.isReadable(),
1427N/A "key should not be readable");
1427N/A check(!sk.isWritable(),
1427N/A "key should not be writable");
1427N/A
1427N/A /* now process the OP_CONNECT */
1427N/A opConnectReceived = true;
1427N/A check((sk.interestOps() & OP_CONNECT) == OP_CONNECT,
1427N/A "selection key interest ops should contain OP_CONNECT");
1427N/A sk.interestOps(OP_READ);
1427N/A check((sk.interestOps() & OP_CONNECT) != OP_CONNECT,
1427N/A "selection key interest ops should not contain OP_CONNECT");
1427N/A check(sc.finishConnect(),
1427N/A "finishConnect should return true");
1427N/A } /* OP_READ */
1427N/A else if (sk.isReadable()) {
1427N/A /* some trivial checks */
1427N/A check(opConnectReceived == true,
1427N/A "should receive one OP_CONNECT before OP_READ");
1427N/A check(opReadReceived == false,
1427N/A "should not receive OP_READ before OP_CONNECT");
1427N/A check(readyChannel.equals(sc),
1427N/A "channels should be equal");
1427N/A check(!sk.isAcceptable(),
1427N/A "key should not be acceptable");
1427N/A check(sk.isReadable(),
1427N/A "key should be readable");
1427N/A check(!sk.isWritable(),
1427N/A "key should not be writable");
1427N/A check(!sk.isConnectable(),
1427N/A "key should not be connectable");
1427N/A
1427N/A /* now process the OP_READ */
1427N/A opReadReceived = true;
1427N/A selectiontKey.cancel();
1427N/A
1427N/A /* try with small buffer to see if native
1427N/A * implementation can handle this */
1427N/A ByteBuffer buffer = ByteBuffer.allocateDirect(1);
1427N/A readyChannel.receive(buffer, null, clientHandler);
1427N/A check(clientHandler.receivedCommUp(),
1427N/A "Client should have received COMM_UP");
1427N/A
1427N/A /* dont close (or put anything on) the channel until
1427N/A * we check that the server's accepted channel also
1427N/A * received COMM_UP */
1427N/A serverHandler.waitForCommUp();
1427N/A } else {
1427N/A fail("Unexpected selection key");
1427N/A }
1427N/A }
1427N/A } else {
1427N/A fail("Client selector returned 0 ready keys");
1427N/A /* stop the server */
1427N/A server.thread().interrupt();
1427N/A }
1427N/A } //for
1427N/A
1427N/A } catch (IOException ioe) {
1427N/A unexpected(ioe);
1427N/A } catch (InterruptedException ie) {
1427N/A unexpected(ie);
1427N/A }
1427N/A }
1427N/A
1427N/A class CommUpServer implements Runnable
1427N/A {
1427N/A final InetSocketAddress serverAddr;
1427N/A private SctpServerChannel ssc;
1427N/A private Thread serverThread;
1427N/A
1427N/A public CommUpServer() throws IOException {
1427N/A ssc = SctpServerChannel.open().bind(null);
1427N/A java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
1427N/A if (addrs.isEmpty())
1427N/A debug("addrs should not be empty");
1427N/A
1427N/A serverAddr = (InetSocketAddress) addrs.iterator().next();
1427N/A }
1427N/A
1427N/A void start() {
1427N/A serverThread = new Thread(this, "CommUpServer-" +
1427N/A serverAddr.getPort());
1427N/A serverThread.start();
1427N/A }
1427N/A
1427N/A InetSocketAddress address () {
1427N/A return serverAddr;
1427N/A }
1427N/A
1427N/A Thread thread() {
1427N/A return serverThread;
1427N/A }
1427N/A
1427N/A @Override
1427N/A public void run() {
1427N/A Selector selector = null;
1427N/A SctpChannel sc = null;
1427N/A SelectionKey readKey = null;
1427N/A try {
1427N/A sc = ssc.accept();
1427N/A debug("accepted " + sc);
1427N/A
1427N/A selector = Selector.open();
1427N/A sc.configureBlocking(false);
1427N/A check(sc.isBlocking() == false, "Should be in non-blocking mode");
1427N/A readKey = sc.register(selector, SelectionKey.OP_READ);
1427N/A
1427N/A debug("select");
1427N/A int keysAdded = selector.select(TIMEOUT);
1427N/A debug("returned " + keysAdded + " keys");
1427N/A if (keysAdded > 0) {
1427N/A Set<SelectionKey> keys = selector.selectedKeys();
1427N/A Iterator<SelectionKey> i = keys.iterator();
1427N/A while(i.hasNext()) {
1427N/A SelectionKey sk = i.next();
1427N/A i.remove();
1427N/A SctpChannel readyChannel =
1427N/A (SctpChannel)sk.channel();
1427N/A check(readyChannel.equals(sc),
1427N/A "channels should be equal");
1427N/A check(!sk.isAcceptable(),
1427N/A "key should not be acceptable");
1427N/A check(sk.isReadable(),
1427N/A "key should be readable");
1427N/A check(!sk.isWritable(),
1427N/A "key should not be writable");
1427N/A check(!sk.isConnectable(),
1427N/A "key should not be connectable");
1427N/A
1427N/A /* block until we check if the client has received its COMM_UP*/
1427N/A clientHandler.waitForCommUp();
1427N/A
1427N/A ByteBuffer buffer = ByteBuffer.allocateDirect(1);
1427N/A sc.receive(buffer, null, serverHandler);
1427N/A check(serverHandler.receivedCommUp(),
1427N/A "Accepted channel should have received COMM_UP");
1427N/A }
1427N/A } else {
1427N/A fail("Server selector returned 0 ready keys");
1427N/A /* stop the client */
1427N/A clientThread.interrupt();
1427N/A }
1427N/A } catch (IOException ioe) {
1427N/A ioe.printStackTrace();
1427N/A } catch (InterruptedException unused) {
1427N/A } finally {
1427N/A if (readKey != null) readKey.cancel();
1427N/A try { if (selector != null) selector.close(); }
1427N/A catch (IOException ioe) { unexpected(ioe); }
1427N/A try { if (ssc != null) ssc.close(); }
1427N/A catch (IOException ioe) { unexpected(ioe); }
1427N/A try { if (sc != null) sc.close(); }
1427N/A catch (IOException ioe) { unexpected(ioe); }
1427N/A }
1427N/A }
1427N/A }
1427N/A
1427N/A class CommUpNotificationHandler extends AbstractNotificationHandler<Object>
1427N/A {
1427N/A private boolean receivedCommUp; // false
1427N/A
1427N/A public synchronized boolean receivedCommUp() {
1427N/A return receivedCommUp;
1427N/A }
1427N/A
1427N/A public synchronized boolean waitForCommUp() throws InterruptedException {
1427N/A while (receivedCommUp == false) {
1427N/A wait();
1427N/A }
1427N/A
1427N/A return false;
1427N/A }
1427N/A
1427N/A @Override
1427N/A public HandlerResult handleNotification(
1427N/A Notification notification, Object attachment) {
1427N/A fail("Unknown notification type");
1427N/A return HandlerResult.CONTINUE;
1427N/A }
1427N/A
1427N/A @Override
1427N/A public synchronized HandlerResult handleNotification(
1427N/A AssociationChangeNotification notification, Object attachment) {
1427N/A AssocChangeEvent event = notification.event();
1427N/A debug("AssociationChangeNotification");
1427N/A debug(" Association: " + notification.association());
1427N/A debug(" Event: " + event);
1427N/A
1427N/A if (event.equals(AssocChangeEvent.COMM_UP)) {
1427N/A receivedCommUp = true;
1427N/A notifyAll();
1427N/A }
1427N/A
1427N/A return HandlerResult.RETURN;
1427N/A }
1427N/A
1427N/A @Override
1427N/A public HandlerResult handleNotification(
1427N/A ShutdownNotification notification, Object attachment) {
1427N/A debug("ShutdownNotification");
1427N/A debug(" Association: " + notification.association());
1427N/A return HandlerResult.RETURN;
1427N/A }
1427N/A }
1427N/A
1427N/A //--------------------- Infrastructure ---------------------------
1427N/A boolean debug = true;
1427N/A volatile int passed = 0, failed = 0;
1427N/A void pass() {passed++;}
1427N/A void fail() {failed++; Thread.dumpStack();}
1427N/A void fail(String msg) {err.println(msg); fail();}
1427N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
1427N/A void check(boolean cond) {if (cond) pass(); else fail();}
1427N/A void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
1427N/A void debug(String message) {if(debug) { out.println(Thread.currentThread().getName() + ": " + message); } }
1427N/A void sleep(long millis) { try { Thread.currentThread().sleep(millis); }
1427N/A catch(InterruptedException ie) { unexpected(ie); }}
1427N/A public static void main(String[] args) throws Throwable {
1427N/A Class<?> k = new Object(){}.getClass().getEnclosingClass();
1427N/A try {k.getMethod("instanceMain",String[].class)
1427N/A .invoke( k.newInstance(), (Object) args);}
1427N/A catch (Throwable e) {throw e.getCause();}}
1427N/A public void instanceMain(String[] args) throws Throwable {
1427N/A try {test(args);} catch (Throwable t) {unexpected(t);}
1427N/A out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1427N/A if (failed > 0) throw new AssertionError("Some tests failed");}
1427N/A
1427N/A}