jdk/test/com/sun/nio/sctp/SctpChannel/Util.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
import java.net.NetworkInterface;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    25
import java.net.InetSocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    26
import java.net.SocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    27
import java.net.InetAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    28
import java.net.Inet4Address;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    29
import java.net.SocketException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    30
import java.io.IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    31
import java.io.PrintStream;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    32
import java.io.UnsupportedEncodingException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    33
import java.util.List;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    34
import java.util.ArrayList;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
import java.util.Set;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
import java.util.Collections;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import java.util.Enumeration;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import java.util.Iterator;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
import java.nio.ByteBuffer;
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 static java.lang.System.out;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
public class Util {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
    static final int SMALL_BUFFER = 128;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
    static final String SMALL_MESSAGE =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
      "Under the bridge and over the dam, looking for berries, berries for jam";
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
    static final int LARGE_BUFFER = 32768;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
    static final String LARGE_MESSAGE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
    static {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
        StringBuffer sb = new StringBuffer(LARGE_BUFFER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
        for (int i=0; i<460; i++)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
          sb.append(SMALL_MESSAGE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
        LARGE_MESSAGE = sb.toString();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    58
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
    static boolean isSCTPSupported() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
            SctpChannel c = SctpChannel.open();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
            c.close();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
            return true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
        } catch (IOException ioe) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
            ioe.printStackTrace();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
        } catch (UnsupportedOperationException e) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
            out.println(e);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
        return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
     * Returns a list of all the addresses on the system.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
     * @param  inclLoopback
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
     *         if {@code true}, include the loopback addresses
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
     * @param  ipv4Only
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
     *         it {@code true}, only IPv4 addresses will be included
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
    static List<InetAddress> getAddresses(boolean inclLoopback,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
                                          boolean ipv4Only)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
        throws SocketException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
        ArrayList<InetAddress> list = new ArrayList<InetAddress>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
        Enumeration<NetworkInterface> nets =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
                 NetworkInterface.getNetworkInterfaces();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
        for (NetworkInterface netInf : Collections.list(nets)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
            Enumeration<InetAddress> addrs = netInf.getInetAddresses();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
            for (InetAddress addr : Collections.list(addrs)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
                if (!list.contains(addr) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
                        (inclLoopback ? true : !addr.isLoopbackAddress()) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
                        (ipv4Only ? (addr instanceof Inet4Address) : true)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
                    list.add(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
                }
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
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
        return list;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
    static void dumpAddresses(SctpChannel channel,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
                              PrintStream printStream)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
        throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
        Set<SocketAddress> addrs = channel.getAllLocalAddresses();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
        printStream.println("Local Addresses: ");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
        for (Iterator<SocketAddress> it = addrs.iterator(); it.hasNext(); ) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
            InetSocketAddress addr = (InetSocketAddress)it.next();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
            printStream.println("\t" + addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
     * Compare the contents of the given ByteBuffer with the contens of the
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
     * given byte array. true if, and only if, the contents are the same.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
    static boolean compare(ByteBuffer bb, byte[] message) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
        if (message.length != bb.remaining()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
            out.println("Compare failed, byte array length != to buffer remaining");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
            return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
        for (int i=0; i<message.length; i++) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
            byte b = bb.get();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
            if (message[i] != b) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
                out.println("Position " + i + ": " + message[i] + " != " + b);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
                return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
        return true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
    static boolean compare(ByteBuffer bb, String str) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
        try{
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
            return Util.compare(bb, str.getBytes("ISO-8859-1"));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
        } catch (UnsupportedEncodingException unsupported) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
            throw new AssertionError(unsupported);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
}