jdk/test/java/nio/channels/DatagramChannel/SRTest.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5788 50d4ad1a780f
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: 5788
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @summary Test DatagramChannel's send and receive methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class SRTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static PrintStream log = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static void test() throws Exception {
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    45
        ClassicReader classicReader;
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    46
        NioReader nioReader;
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    47
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    48
        classicReader = new ClassicReader();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    49
        invoke(classicReader, new ClassicWriter(classicReader.port()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        log.println("Classic RW: OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    52
        classicReader = new ClassicReader();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    53
        invoke(classicReader, new NioWriter(classicReader.port()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        log.println("Classic R, Nio W: OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    56
        nioReader = new NioReader();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    57
        invoke(nioReader, new ClassicWriter(nioReader.port()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        log.println("Classic W, Nio R: OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    60
        nioReader = new NioReader();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    61
        invoke(nioReader, new NioWriter(nioReader.port()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        log.println("Nio RW: OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static void invoke(Sprintable reader, Sprintable writer) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        Thread readerThread = new Thread(reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        readerThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Thread writerThread = new Thread(writer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        writerThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        writerThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        readerThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        reader.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        writer.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public interface Sprintable extends Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public void throwException() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static class ClassicWriter implements Sprintable {
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    85
        final int port;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        Exception e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    88
        ClassicWriter(int port) {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    89
            this.port = port;
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    90
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
    91
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public void throwException() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                DatagramSocket ds = new DatagramSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                String dataString = "hello";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                byte[] data = dataString.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                InetAddress address = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                DatagramPacket dp = new DatagramPacket(data, data.length,
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   104
                                                       address, port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ds.send(dp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                ds.send(dp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                e = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static class NioWriter implements Sprintable {
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   115
        final int port;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Exception e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   118
        NioWriter(int port) {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   119
            this.port = port;
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   120
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   121
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public void throwException() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                DatagramChannel dc = DatagramChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                ByteBuffer bb = ByteBuffer.allocateDirect(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                bb.put("hello".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                InetAddress address = InetAddress.getLocalHost();
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   134
                InetSocketAddress isa = new InetSocketAddress(address, port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                dc.send(bb, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                dc.send(bb, isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                e = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static class ClassicReader implements Sprintable {
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   145
        final DatagramSocket ds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Exception e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   148
        ClassicReader() throws IOException {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   149
            this.ds = new DatagramSocket();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   150
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   151
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   152
        int port() {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   153
            return ds.getLocalPort();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   154
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   155
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        public void throwException() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                byte[] buf = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                DatagramPacket dp = new DatagramPacket(buf, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                ds.receive(dp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                String received = new String(dp.getData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                log.println(received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                ds.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                e = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public static class NioReader implements Sprintable {
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   176
        final DatagramChannel dc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        Exception e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
5788
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   179
        NioReader() throws IOException {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   180
            this.dc = DatagramChannel.open().bind(new InetSocketAddress(0));
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   181
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   182
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   183
        int port() {
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   184
            return dc.socket().getLocalPort();
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   185
        }
50d4ad1a780f 6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents: 5506
diff changeset
   186
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        public void throwException() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                ByteBuffer bb = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                SocketAddress sa = dc.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                CharBuffer cb = Charset.forName("US-ASCII").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    newDecoder().decode(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                log.println("From: "+sa+ " said " +cb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                e = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}