jdk/test/com/sun/nio/sctp/SctpChannel/Shutdown.java
author chegar
Thu, 16 Apr 2009 17:42:00 +0100
changeset 2542 d859108aea12
child 3072 a801b122142f
permissions -rw-r--r--
4927640: Implementation of the sctp protocol Summary: An implementation-specific API for the Stream Control Transmission Protocol Reviewed-by: alanb, michaelm, jccollet
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     1
/*
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     4
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     8
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    13
 * accompanied this code).
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    14
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    18
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    21
 * have any questions.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    22
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    23
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    24
/* @test
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    25
 * @bug 4927640
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    26
 * @summary Tests the SCTP protocol implementation
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    27
 * @author chegar
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    28
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    29
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    30
import java.net.InetSocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    31
import java.net.SocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    32
import java.io.IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    33
import java.util.concurrent.CountDownLatch;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    34
import java.nio.ByteBuffer;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
import java.nio.channels.ClosedChannelException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
import java.nio.channels.NotYetConnectedException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import com.sun.nio.sctp.AbstractNotificationHandler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import com.sun.nio.sctp.HandlerResult;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
import com.sun.nio.sctp.MessageInfo;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
import com.sun.nio.sctp.SctpServerChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
import com.sun.nio.sctp.ShutdownNotification;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
import static java.lang.System.out;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
import static java.lang.System.err;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
public class Shutdown {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
    static CountDownLatch finishedLatch = new CountDownLatch(1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
    static CountDownLatch sentLatch = new CountDownLatch(1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
    void test(String[] args) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
        SocketAddress address = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
        ShutdownServer server = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
        if (!Util.isSCTPSupported()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
            out.println("SCTP protocol is not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
            out.println("Test cannot be run");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
            return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    58
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
        if (args.length == 2) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
            /* requested to connecct to a specific address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
                int port = Integer.valueOf(args[1]);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
                address = new InetSocketAddress(args[0], port);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
            } catch (NumberFormatException nfe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
                err.println(nfe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
        } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
            /* start server on local machine, default */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
                server = new ShutdownServer();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
                server.start();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
                address = server.address();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
                debug("Server started and listening on " + address);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
                ioe.printStackTrace();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
        doTest(address);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
    void doTest(SocketAddress peerAddress) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
        SctpChannel channel = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
        ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
        MessageInfo info;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
            channel = SctpChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
            /* TEST 1: Verify NotYetConnectedException thrown */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
            debug("Test 1: NotYetConnectedException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
                channel.shutdown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
                fail("shutdown not throwing expected NotYetConnectedException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
            } catch (NotYetConnectedException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
            }  catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
            channel.connect(peerAddress);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
            sentLatch.await();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
            channel.shutdown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
            /* TEST 2: receive data sent before shutdown */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
            do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
                debug("Test 2: invoking receive");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
                info = channel.receive(buffer, null, null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
                if (info == null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
                    fail("unexpected null from receive");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
                    return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
            } while (!info.isComplete());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
            buffer.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
            check(info != null, "info is null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
            check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
                  length, "bytes received not equal to message length");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
            check(info.bytes() == buffer.remaining(), "bytes != remaining");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
            check(Util.compare(buffer, Util.SMALL_MESSAGE),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
                  "received message not the same as sent message");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
            buffer.clear();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
            /* TEST 3: receive notifications on the SCTP stack */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
            debug("Test 3: receive notifications");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
            while ((info = channel.receive(buffer, null, null )) != null &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
                    info.bytes() != -1 );
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
            /* TEST 4: If the channel is already shutdown then invoking this
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
             * method has no effect. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
            debug("Test 4: no-op");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
                channel.shutdown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
            /* TEST 5: Further sends will throw ClosedChannelException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
            debug("Test 5: ClosedChannelException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   145
            info = MessageInfo.createOutgoing(null, 1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
                channel.send(buffer, info);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
                fail("shutdown not throwing expected ClosedChannelException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
            } catch (ClosedChannelException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   153
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
        } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
            unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
        } catch (InterruptedException ie) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   157
            unexpected(ie);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   158
        }finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   159
            finishedLatch.countDown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   160
            try { if (channel != null) channel.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   161
            catch (IOException e) { unexpected(e);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   162
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   163
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   164
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   165
    class ShutdownServer implements Runnable
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   166
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   167
        final InetSocketAddress serverAddr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
        private SctpServerChannel ssc;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   170
        public ShutdownServer() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   171
            ssc = SctpServerChannel.open().bind(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   172
            //serverAddr = (InetSocketAddress)(ssc.getAllLocalAddresses().iterator().next());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
            java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
            if (addrs.isEmpty())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   176
                debug("addrs should not be empty");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   177
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   178
            serverAddr = (InetSocketAddress) addrs.iterator().next();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   180
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   181
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   182
        public void start() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   183
            (new Thread(this, "ShutdownServer-"  + serverAddr.getPort())).start();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   185
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   186
        public InetSocketAddress address() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
            return serverAddr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
        public void run() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   192
            SctpChannel sc = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   193
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   194
                sc = ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   195
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   196
                /* send a message */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   197
                MessageInfo info = MessageInfo.createOutgoing(null, 1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
                ByteBuffer buf = ByteBuffer.allocateDirect(Util.SMALL_BUFFER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   199
                buf.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   200
                buf.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   201
                sc.send(buf, info);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   202
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
                /* notify client that the data has been sent */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   204
                sentLatch.countDown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   205
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   206
                /* wait until after the client has finished its tests */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   207
                finishedLatch.await();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   208
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   209
                buf.clear();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
                ShutdownNotificationHandler handler =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
                        new ShutdownNotificationHandler();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   212
                BooleanWrapper bool = new BooleanWrapper();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   213
                sc.configureBlocking(false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   214
                sc.receive(buf, bool, handler);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
                check(bool.booleanValue(), "SHUTDOWN not received on Server");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   216
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   217
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
                ioe.printStackTrace();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
            } catch (InterruptedException ie) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
                ie.printStackTrace();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
                try { if (ssc != null) ssc.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
                catch (IOException  ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
                try { if (sc != null) sc.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
                catch (IOException  ioe) { unexpected(ioe); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
    class BooleanWrapper {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
        boolean bool;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   232
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   233
        boolean booleanValue() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   234
            return bool;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
        void booleanValue(boolean value) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
            bool = value;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
    class ShutdownNotificationHandler extends AbstractNotificationHandler<BooleanWrapper>
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
        public HandlerResult handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
                ShutdownNotification sn, BooleanWrapper bool)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
        {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
            bool.booleanValue(true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
            debug(sn.toString());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
            return HandlerResult.RETURN;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   253
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   254
        //--------------------- Infrastructure ---------------------------
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   255
    boolean debug = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
    volatile int passed = 0, failed = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
    void pass() {passed++;}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   258
    void fail() {failed++; Thread.dumpStack();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   259
    void fail(String msg) {System.err.println(msg); fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   260
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   261
    void check(boolean cond) {if (cond) pass(); else fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   262
    void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   263
    void debug(String message) {if(debug) { System.out.println(message); }  }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
    public static void main(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
        Class<?> k = new Object(){}.getClass().getEnclosingClass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
        try {k.getMethod("instanceMain",String[].class)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
                .invoke( k.newInstance(), (Object) args);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   268
        catch (Throwable e) {throw e.getCause();}}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   269
    public void instanceMain(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   270
        try {test(args);} catch (Throwable t) {unexpected(t);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   272
        if (failed > 0) throw new AssertionError("Some tests failed");}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   273
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   274
}