if (!Util.isSCTPSupported()) {
out.println("SCTP protocol is not supported");
out.println("Test cannot be run"); return;
}
if (args.length == 2) { /* requested to connecct to a specific address */ try { int port = Integer.valueOf(args[1]);
address = new InetSocketAddress(args[0], port);
} catch (NumberFormatException nfe) {
err.println(nfe);
}
} else { /* start server on local machine, default */ try {
server = new CommUpServer();
server.start();
address = server.address();
debug("Server started and listening on " + address);
} catch (IOException ioe) {
ioe.printStackTrace(); return;
}
}
/* store the main thread so that the server can interrupt it, if necessary */
clientThread = Thread.currentThread();
doClient(address);
}
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);
/* 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");
} /* OP_READ */ elseif (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 *wecheckthattheserver'sacceptedchannelalso
* 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
class CommUpServer implements Runnable
{ final InetSocketAddress serverAddr; private SctpServerChannel ssc; privateThread serverThread;
public CommUpServer() throws IOException {
ssc = SctpServerChannel.open().bind(null);
java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses(); if (addrs.isEmpty())
debug("addrs should not be empty");
selector = Selector.open();
sc.configureBlocking(false);
check(sc.isBlocking() == false, "Should be in non-blocking mode");
readKey = sc.register(selector, SelectionKey.OP_READ);
debug("select"); 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();
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");
/* block until we check if the client has received its COMM_UP*/
clientHandler.waitForCommUp();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.