jdk/test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5970 d4e98bbfb0be
child 14415 7a31b0e0cfaf
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5970
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
5807
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    25
 * @bug 4313882 4981129
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Unit test for datagram-socket-channel adaptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class AdaptDatagramSocket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static java.io.PrintStream out = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static Random rand = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static final int ECHO_PORT = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final int DISCARD_PORT = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final String REMOTE_HOST = TestUtil.HOST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static final InetSocketAddress echoAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        = new InetSocketAddress(REMOTE_HOST, ECHO_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final InetSocketAddress discardAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        = new InetSocketAddress(REMOTE_HOST, DISCARD_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static String toString(DatagramPacket dp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        return ("DatagramPacket[off=" + dp.getOffset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                + ", len=" + dp.getLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static void test(DatagramSocket ds, InetSocketAddress dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                     boolean shouldTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        DatagramPacket op = new DatagramPacket(new byte[100], 13, 42, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        rand.nextBytes(op.getData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        DatagramPacket ip = new DatagramPacket(new byte[100], 19, 100 - 19);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        out.println("pre  op: " + toString(op) + "  ip: " + toString(ip));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ds.send(op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                ds.receive(ip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                if (ip.getLength() == 0) { // ## Not sure why this happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    ip.setLength(100 - 19);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            } catch (SocketTimeoutException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if (shouldTimeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    out.println("Receive timed out, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        out.println("rtt: " + (System.currentTimeMillis() - start));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        out.println("post op: " + toString(op) + "  ip: " + toString(ip));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        for (int i = 0; i < ip.getLength(); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (ip.getData()[ip.getOffset() + i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                != op.getData()[op.getOffset() + i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throw new Exception("Incorrect data received");
5807
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    95
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    96
        if (!(ip.getSocketAddress().equals(dst))) {
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    97
            throw new Exception("Incorrect sender address, expected: " + dst
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    98
                + " actual: " + ip.getSocketAddress());
d34ed576e234 4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents: 5506
diff changeset
    99
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static void test(InetSocketAddress dst,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                     int timeout, boolean shouldTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                     boolean connect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        out.println("dst: " + dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        DatagramSocket ds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // Original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            ds = new DatagramSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            DatagramChannel dc = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            ds = dc.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            ds.bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        out.println("socket: " + ds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (connect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            ds.connect(dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            out.println("connect: " + ds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        InetSocketAddress src = new InetSocketAddress(ds.getLocalAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                                      ds.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        out.println("src: " + src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (timeout > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            ds.setSoTimeout(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        out.println("timeout: " + ds.getSoTimeout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        for (int i = 0; i < 5; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            test(ds, dst, shouldTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // Leave the socket open so that we don't reuse the old src address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        //ds.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        test(echoAddress, 0, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        test(echoAddress, 0, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        test(echoAddress, 5000, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        test(discardAddress, 10, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}