jdk/test/com/sun/nio/sctp/SctpChannel/Receive.java
author chegar
Thu, 16 Apr 2009 17:42:00 +0100
changeset 2542 d859108aea12
child 5506 202f599c92aa
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.util.concurrent.TimeUnit;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
import java.nio.ByteBuffer;
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 java.nio.channels.ClosedChannelException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import com.sun.nio.sctp.AbstractNotificationHandler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
import com.sun.nio.sctp.AssociationChangeNotification;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
import com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
import com.sun.nio.sctp.HandlerResult;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
import com.sun.nio.sctp.IllegalReceiveException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
import com.sun.nio.sctp.MessageInfo;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
import com.sun.nio.sctp.Notification;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
import com.sun.nio.sctp.SctpServerChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
import com.sun.nio.sctp.ShutdownNotification;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
import static java.lang.System.out;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
import static java.lang.System.err;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
public class Receive {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
    /* Latches used to synchronize between the client and server so that
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
     * connections without any IO may not be closed without being accepted */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
    final CountDownLatch clientFinishedLatch = new CountDownLatch(1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
    final CountDownLatch serverFinishedLatch = new CountDownLatch(1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
    /* Used to verify that the ppid is being sent and received correctly */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    58
    static final int PPID = 5;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
    void test(String[] args) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
        SocketAddress address = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
        Server server;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
        if (!Util.isSCTPSupported()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
            out.println("SCTP protocol is not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
            out.println("Test cannot be run");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
            return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
        if (args.length == 2) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
            /* requested to connecct to a specific address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
                int port = Integer.valueOf(args[1]);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
                address = new InetSocketAddress(args[0], port);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
            } catch (NumberFormatException nfe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
                err.println(nfe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
        } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
            /* start server on local machine, default */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
                server = new Server();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
                server.start();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
                address = server.address();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
                debug("Server started and listening on " + address);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
                ioe.printStackTrace();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
        doTest(address);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
    void doTest(SocketAddress peerAddress) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
        SctpChannel channel = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
        ByteBuffer buffer = ByteBuffer.allocate(Util.LARGE_BUFFER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
        MessageInfo info;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
            channel = SctpChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
            ReceiveNotificationHandler handler =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
                new ReceiveNotificationHandler(channel);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
            /* TEST 1: Verify NotYetConnectedException thrown */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
                channel.receive(buffer, null, handler);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
                fail("should have thrown NotYetConnectedException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
            } catch (NotYetConnectedException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
            }  catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
            channel.connect(peerAddress);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
            /* TEST 2: receive small message */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
            do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
                debug("Test 2: invoking receive");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
                info = channel.receive(buffer, null, handler);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
                if (info == null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
                    fail("unexpected null from receive");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
                    return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
            } while (!info.isComplete());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
            buffer.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
            check(handler.receivedCommUp(), "SCTP_COMM_UP not received");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
            check(info != null, "info is null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
            check(info.address() != null, "address is null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
            check(info.association() != null, "association is null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
            check(info.isComplete(), "message is not complete");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
            check(info.isUnordered() != true,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
                  "message should not be unordered");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
            check(info.streamNumber() >= 0, "invalid stream number");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
            check(info.payloadProtocolID() == PPID, "PPID incorrect");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
            check(info.bytes() == Util.SMALL_MESSAGE.getBytes("ISO-8859-1").
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
                  length, "bytes received not equal to message length");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
            check(info.bytes() == buffer.remaining(), "bytes != remaining");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
            check(Util.compare(buffer, Util.SMALL_MESSAGE),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
                  "received message not the same as sent message");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
            buffer.clear();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   145
             /* TEST 3: receive large message */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
            do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
                debug("Test 3: invoking receive");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
                info = channel.receive(buffer, null, handler);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
                if (info == null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
                    fail("unexpected null from receive");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
                    return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   153
            } while (!info.isComplete());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
            buffer.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
            check(info != null, "info is null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   157
            check(info.address() != null, "address is null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   158
            check(info.association() != null, "association is null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   159
            check(info.isComplete(), "message is not complete");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   160
            check(info.isUnordered() != true,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   161
                  "message should not be unordered");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   162
            check(info.streamNumber() >= 0, "invalid stream number");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   163
            check(info.bytes() == Util.LARGE_MESSAGE.getBytes("ISO-8859-1").
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   164
                  length, "bytes received not equal to message length");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   165
            check(info.bytes() == buffer.remaining(), "bytes != remaining");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   166
            check(Util.compare(buffer, Util.LARGE_MESSAGE),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   167
                  "received message not the same as sent message");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
            buffer.clear();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   170
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   171
            /* TEST 4: EOF */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   172
            buffer.clear();  // buffer position 0
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
            info = channel.receive(buffer,null, handler);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
            check(info != null, "info is null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
            check(info.bytes() == -1, "should have received EOF");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   176
            check(buffer.position() == 0, "buffer position should be unchanged");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   177
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   178
            /* TEST 5: ClosedChannelException */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
            channel.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   180
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   181
                channel.receive(buffer, null, null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   182
                fail("should have thrown ClosedChannelException");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   183
            } catch (ClosedChannelException cce) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
               pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   185
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   186
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
            handler = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
            /* TEST 6: handler returns RETURN after handling a notification */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
            ReceiveNotificationHandler handler2 =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   192
                new ReceiveNotificationHandler(null); /* HandlerResult.RETURN */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   193
            channel = SctpChannel.open(peerAddress, 0, 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   194
            info = channel.receive(buffer, null, handler2);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   195
            check(info == null, "channel should return null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   196
            check(handler2.receivedCommUp(), "SCTP_COMM_UP not received");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   197
            check(buffer.position() == 0, "buffer position should be unchanged");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   199
            /* TEST 7: Non blocking channel return null if no data */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   200
            channel.configureBlocking(false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   201
            info = channel.receive(buffer, null, null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   202
            check(info == null, "non-blocking channel should return null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
            check(buffer.position() == 0, "buffer position should be unchanged");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   204
        } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   205
            unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   206
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   207
            clientFinishedLatch.countDown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   208
            try { serverFinishedLatch.await(10L, TimeUnit.SECONDS); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   209
            catch (InterruptedException ie) { unexpected(ie); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
            if (channel != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
                try { channel.close(); }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   212
                catch (IOException e) { unexpected (e);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   213
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   214
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   216
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   217
    class Server implements Runnable
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
        final InetSocketAddress serverAddr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
        private SctpServerChannel ssc;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
        public Server() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
            ssc = SctpServerChannel.open().bind(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
            java.util.Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
            if (addrs.isEmpty())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
                debug("addrs should not be empty");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
            serverAddr = (InetSocketAddress) addrs.iterator().next();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
        public void start() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   232
            (new Thread(this, "Server-"  + serverAddr.getPort())).start();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   233
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   234
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
        public InetSocketAddress address() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
            return serverAddr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
        public void run() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
                SctpChannel sc = ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
                /* send a small message */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
                MessageInfo info = MessageInfo.createOutgoing(null, 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
                        .payloadProtocolID(PPID);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
                ByteBuffer buf = ByteBuffer.allocateDirect(Util.SMALL_BUFFER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
                buf.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
                buf.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
                debug("sending small message: " + buf);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
                sc.send(buf, info);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   253
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   254
                /* send a large message */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   255
                buf = ByteBuffer.allocateDirect(Util.LARGE_BUFFER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
                buf.put(Util.LARGE_MESSAGE.getBytes("ISO-8859-1"));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
                buf.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   258
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   259
                debug("sending large message: " + buf);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   260
                sc.send(buf, info);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   261
                sc.shutdown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   262
                debug("shutdown");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   263
                ReceiveNotificationHandler handler =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
                    new ReceiveNotificationHandler(sc);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
                sc.receive(buf, null, handler);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
                sc.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   268
                /* accept another socket for the TEST 6 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   269
                sc = ssc.accept();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   270
                ssc.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   272
                clientFinishedLatch.await(10L, TimeUnit.SECONDS);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   273
                serverFinishedLatch.countDown();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   274
                sc.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   275
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   276
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   277
            } catch (InterruptedException ie) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   278
                unexpected(ie);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   279
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   280
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   281
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   282
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   283
    class ReceiveNotificationHandler extends AbstractNotificationHandler<Object>
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   284
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   285
        SctpChannel channel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   286
        boolean receivedCommUp;  // false
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   287
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   288
        public ReceiveNotificationHandler(SctpChannel channel) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   289
            this.channel = channel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   290
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   291
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   292
        public boolean receivedCommUp() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   293
            return receivedCommUp;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   294
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   295
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   296
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   297
        public HandlerResult handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   298
                Notification notification, Object attachment) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   299
            fail("Unknown notification type");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   300
            return HandlerResult.CONTINUE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   301
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   302
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   303
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   304
        public HandlerResult handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   305
                AssociationChangeNotification notification, Object attachment) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   306
            AssocChangeEvent event = notification.event();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   307
            debug("AssociationChangeNotification");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   308
            debug("  Association: " + notification.association());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   309
            debug("  Event: " + event);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   310
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   311
            if (event.equals(AssocChangeEvent.COMM_UP))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   312
                receivedCommUp = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   313
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   314
            if (channel == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   315
                return HandlerResult.RETURN;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   316
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   317
            /* TEST 4: IllegalReceiveException - If the given handler invokes
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   318
             * the receive method of this channel*/
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   319
            ByteBuffer buffer = ByteBuffer.allocate(10);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   320
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   321
                channel.receive(buffer, null, this);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   322
                fail("IllegalReceiveException expected");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   323
            } catch (IllegalReceiveException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   324
                pass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   325
            } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   326
                unexpected(ioe);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   327
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   328
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   329
            return HandlerResult.CONTINUE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   330
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   331
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   332
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   333
        public HandlerResult handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   334
                ShutdownNotification notification, Object attachment) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   335
            debug("ShutdownNotification");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   336
            debug("  Association: " + notification.association());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   337
            return HandlerResult.CONTINUE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   338
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   339
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   340
        //--------------------- Infrastructure ---------------------------
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   341
    boolean debug = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   342
    volatile int passed = 0, failed = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   343
    void pass() {passed++;}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   344
    void fail() {failed++; Thread.dumpStack();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   345
    void fail(String msg) {System.err.println(msg); fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   346
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   347
    void check(boolean cond) {if (cond) pass(); else fail();}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   348
    void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   349
    void debug(String message) {if(debug) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   350
          System.out.println(Thread.currentThread() + " " + message); } }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   351
    public static void main(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   352
        Class<?> k = new Object(){}.getClass().getEnclosingClass();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   353
        try {k.getMethod("instanceMain",String[].class)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   354
                .invoke( k.newInstance(), (Object) args);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   355
        catch (Throwable e) {throw e.getCause();}}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   356
    public void instanceMain(String[] args) throws Throwable {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   357
        try {test(args);} catch (Throwable t) {unexpected(t);}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   358
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   359
        if (failed > 0) throw new AssertionError("Some tests failed");}
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   360
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   361
}