jdk/test/com/sun/nio/sctp/SctpMultiChannel/SocketOptionTests.java
author alanb
Mon, 09 May 2011 18:45:33 +0100
changeset 9679 d98ae8bc45fc
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7042979: Rename StandardSocketOption and StandardWatchEventKind Reviewed-by: forax, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3320
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     4
 *
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     8
 *
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    13
 * accompanied this code).
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    14
 *
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3320
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3320
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3320
diff changeset
    21
 * questions.
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    22
 */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    23
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    24
/* @test
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    25
 * @bug 4927640
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    26
 * @summary Tests the SCTP protocol implementation
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    27
 * @author chegar
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    28
 */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    29
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    30
import java.io.IOException;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    31
import java.net.InetSocketAddress;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    32
import java.net.SocketAddress;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    33
import java.util.Iterator;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    34
import java.util.Set;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    35
import java.util.List;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    36
import java.util.Arrays;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    37
import java.nio.ByteBuffer;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    38
import java.nio.channels.ClosedChannelException;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    39
import com.sun.nio.sctp.AbstractNotificationHandler;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    40
import com.sun.nio.sctp.Association;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    41
import com.sun.nio.sctp.AssociationChangeNotification;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    42
import com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    43
import com.sun.nio.sctp.HandlerResult;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    44
import com.sun.nio.sctp.MessageInfo;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    45
import com.sun.nio.sctp.SctpChannel;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    46
import com.sun.nio.sctp.SctpMultiChannel;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    47
import com.sun.nio.sctp.SctpServerChannel;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    48
import com.sun.nio.sctp.SctpSocketOption;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    49
import java.security.AccessController;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    50
import sun.security.action.GetPropertyAction;
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 5506
diff changeset
    51
import static com.sun.nio.sctp.SctpStandardSocketOptions.*;
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    52
import static java.lang.System.out;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    53
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    54
public class SocketOptionTests {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    55
    final String osName = AccessController.doPrivileged(
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    56
                    new GetPropertyAction("os.name"));
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    57
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    58
    <T> void checkOption(SctpMultiChannel smc, SctpSocketOption<T> name,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    59
            T expectedValue) throws IOException {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    60
        T value = smc.getOption(name, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    61
        check(value.equals(expectedValue), name + ": value (" + value +
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    62
                ") not as expected (" + expectedValue + ")");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    63
       }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    64
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    65
    <T> void optionalSupport(SctpMultiChannel smc, SctpSocketOption<T> name,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    66
            T value) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    67
        try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    68
            smc.setOption(name, value, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    69
            checkOption(smc, name, value);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    70
        } catch (IOException e) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    71
            /* Informational only, not all options have native support */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    72
            out.println(name + " not supported. " + e);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    73
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    74
    }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    75
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    76
    void test(String[] args) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    77
        if (!Util.isSCTPSupported()) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    78
            out.println("SCTP protocol is not supported");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    79
            out.println("Test cannot be run");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    80
            return;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    81
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    82
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    83
        try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    84
            SctpMultiChannel smc = SctpMultiChannel.open();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    85
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    86
            /* check supported options */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    87
            Set<SctpSocketOption<?>> options = smc.supportedOptions();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    88
            List<? extends SctpSocketOption<?>> expected = Arrays.<SctpSocketOption<?>>asList(
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    89
                    SCTP_DISABLE_FRAGMENTS, SCTP_EXPLICIT_COMPLETE,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    90
                    SCTP_FRAGMENT_INTERLEAVE, SCTP_INIT_MAXSTREAMS,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    91
                    SCTP_NODELAY, SCTP_PRIMARY_ADDR, SCTP_SET_PEER_PRIMARY_ADDR,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    92
                    SO_SNDBUF, SO_RCVBUF, SO_LINGER);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    93
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    94
            for (SctpSocketOption opt: expected) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    95
                if (!options.contains(opt))
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    96
                    fail(opt.name() + " should be supported");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    97
            }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    98
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
    99
            InitMaxStreams streams = InitMaxStreams.create(1024, 1024);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   100
            smc.setOption(SCTP_INIT_MAXSTREAMS, streams, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   101
            checkOption(smc, SCTP_INIT_MAXSTREAMS, streams);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   102
            streams = smc.getOption(SCTP_INIT_MAXSTREAMS, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   103
            check(streams.maxInStreams() == 1024, "Max in streams: value: "
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   104
                    + streams.maxInStreams() + ", expected 1024 ");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   105
            check(streams.maxOutStreams() == 1024, "Max out streams: value: "
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   106
                    + streams.maxOutStreams() + ", expected 1024 ");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   107
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   108
            optionalSupport(smc, SCTP_DISABLE_FRAGMENTS, true);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   109
            optionalSupport(smc, SCTP_EXPLICIT_COMPLETE, true);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   110
            optionalSupport(smc, SCTP_FRAGMENT_INTERLEAVE, 1);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   111
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   112
            smc.setOption(SCTP_NODELAY, true, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   113
            checkOption(smc, SCTP_NODELAY, true);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   114
            smc.setOption(SO_SNDBUF, 16*1024, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   115
            smc.setOption(SO_RCVBUF, 16*1024, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   116
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   117
            checkOption(smc, SO_LINGER, -1);  /* default should be negative */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   118
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   119
            /* Setting SO_LINGER not support for one-to-many on Solaris */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   120
            if (!"SunOS".equals(osName)) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   121
                smc.setOption(SO_LINGER, 2000, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   122
                checkOption(smc, SO_LINGER, 2000);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   123
            }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   124
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   125
            /* SCTP_PRIMARY_ADDR */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   126
            sctpPrimaryAddr();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   127
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   128
            /* NullPointerException */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   129
            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   130
                smc.setOption(null, "value", null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   131
                fail("NullPointerException not thrown for setOption");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   132
            } catch (NullPointerException unused) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   133
                pass();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   134
            }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   135
            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   136
               smc.getOption(null, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   137
               fail("NullPointerException not thrown for getOption");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   138
            } catch (NullPointerException unused) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   139
               pass();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   140
            }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   141
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   142
            /* ClosedChannelException */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   143
            smc.close();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   144
            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   145
               smc.setOption(SCTP_INIT_MAXSTREAMS, streams, null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   146
               fail("ClosedChannelException not thrown");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   147
            } catch (ClosedChannelException unused) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   148
                pass();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   149
            }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   150
        } catch (IOException ioe) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   151
            unexpected(ioe);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   152
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   153
    }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   154
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   155
    /* SCTP_PRIMARY_ADDR */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   156
    void sctpPrimaryAddr() throws IOException {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   157
        SocketAddress addrToSet = null;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   158
        ByteBuffer buffer = ByteBuffer.allocate(Util.SMALL_BUFFER);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   159
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   160
        System.out.println("TESTING SCTP_PRIMARY_ADDR");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   161
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   162
        /* create listening channel */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   163
        SctpServerChannel ssc = SctpServerChannel.open().bind(null);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   164
        Set<SocketAddress> addrs = ssc.getAllLocalAddresses();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   165
        if (addrs.isEmpty())
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   166
            debug("addrs should not be empty");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   167
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   168
        InetSocketAddress serverAddr = (InetSocketAddress) addrs.iterator().next();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   169
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   170
        /* setup an association implicitly by sending a small message */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   171
        int streamNumber = 0;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   172
        debug("sending to " + serverAddr + " on stream number: " + streamNumber);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   173
        MessageInfo info = MessageInfo.createOutgoing(serverAddr, streamNumber);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   174
        buffer.put(Util.SMALL_MESSAGE.getBytes("ISO-8859-1"));
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   175
        buffer.flip();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   176
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   177
        debug("sending small message: " + buffer);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   178
        SctpMultiChannel smc = SctpMultiChannel.open();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   179
        int sent = smc.send(buffer, info);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   180
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   181
        /* Receive the COMM_UP */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   182
        buffer.clear();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   183
        SOTNotificationHandler handler = new SOTNotificationHandler();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   184
        info = smc.receive(buffer, null, handler);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   185
        check(handler.receivedCommUp(), "COMM_UP no received");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   186
        Set<Association> associations = smc.associations();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   187
        check(!associations.isEmpty(),"There should be some associations");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   188
        Association assoc = associations.iterator().next();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   189
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   190
        SctpChannel peerChannel = ssc.accept();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   191
        ssc.close();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   192
        Set<SocketAddress> peerAddrs = peerChannel.getAllLocalAddresses();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   193
        debug("Peer local Addresses: ");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   194
        for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   195
            InetSocketAddress addr = (InetSocketAddress)it.next();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   196
            debug("\t" + addr);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   197
            addrToSet = addr;   // any of the peer addresses will do!
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   198
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   199
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   200
        /* retrieval of SCTP_PRIMARY_ADDR is not supported on Solaris */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   201
        if ("SunOS".equals(osName)) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   202
            /* For now do not set this option. There is a bug on Solaris 10 pre Update 5
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   203
             * where setting this option returns Invalid argument */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   204
            //debug("Set SCTP_PRIMARY_ADDR with " + addrToSet);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   205
            //smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   206
            return;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   207
        } else { /* Linux */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   208
            SocketAddress primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   209
            System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   210
            /* Verify that this is one of the peer addresses */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   211
            boolean found = false;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   212
            addrToSet = primaryAddr; // may not have more than one addr
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   213
            for (Iterator<SocketAddress> it = peerAddrs.iterator(); it.hasNext(); ) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   214
                InetSocketAddress addr = (InetSocketAddress)it.next();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   215
                if (addr.equals(primaryAddr)) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   216
                    found = true;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   217
                }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   218
                addrToSet = addr;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   219
            }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   220
            check(found, "SCTP_PRIMARY_ADDR returned bogus address!");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   221
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   222
            System.out.println("Try SCTP_PRIMARY_ADDR set to: " + addrToSet);
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   223
            smc.setOption(SCTP_PRIMARY_ADDR, addrToSet, assoc);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   224
            System.out.println("SCTP_PRIMARY_ADDR set to: " + addrToSet);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   225
            primaryAddr = smc.getOption(SCTP_PRIMARY_ADDR, assoc);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   226
            System.out.println("SCTP_PRIMARY_ADDR returned: " + primaryAddr);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   227
            check(addrToSet.equals(primaryAddr),"SCTP_PRIMARY_ADDR not set correctly");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   228
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   229
    }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   230
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   231
    class SOTNotificationHandler extends AbstractNotificationHandler<Object>
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   232
    {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   233
        boolean receivedCommUp;  // false
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   234
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   235
        boolean receivedCommUp() {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   236
            return receivedCommUp;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   237
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   238
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   239
        @Override
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   240
        public HandlerResult handleNotification(
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   241
                AssociationChangeNotification notification, Object attachment) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   242
            AssocChangeEvent event = notification.event();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   243
            debug("AssociationChangeNotification");
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   244
            debug("  Association: " + notification.association());
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   245
            debug("  Event: " + event);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   246
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   247
            if (event.equals(AssocChangeEvent.COMM_UP))
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   248
                receivedCommUp = true;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   249
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   250
            return HandlerResult.RETURN;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   251
        }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   252
    }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   253
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   254
            //--------------------- Infrastructure ---------------------------
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   255
    boolean debug = true;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   256
    volatile int passed = 0, failed = 0;
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   257
    void pass() {passed++;}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   258
    void fail() {failed++; Thread.dumpStack();}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   259
    void fail(String msg) {System.err.println(msg); fail();}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   260
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   261
    void check(boolean cond) {if (cond) pass(); else fail();}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   262
    void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   263
    void debug(String message) {if(debug) { System.out.println(message); }  }
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   264
    public static void main(String[] args) throws Throwable {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   265
        Class<?> k = new Object(){}.getClass().getEnclosingClass();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   266
        try {k.getMethod("instanceMain",String[].class)
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   267
                .invoke( k.newInstance(), (Object) args);}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   268
        catch (Throwable e) {throw e.getCause();}}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   269
    public void instanceMain(String[] args) throws Throwable {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   270
        try {test(args);} catch (Throwable t) {unexpected(t);}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   271
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   272
        if (failed > 0) throw new AssertionError("Some tests failed");}
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents:
diff changeset
   273
}