author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 5788 | 50d4ad1a780f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @summary Test DatagramChannel's send and receive methods |
|
26 |
* @author Mike McCloskey |
|
27 |
*/ |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.net.*; |
|
31 |
import java.nio.*; |
|
32 |
import java.nio.channels.*; |
|
33 |
import java.nio.charset.*; |
|
34 |
||
35 |
||
36 |
public class SRTest { |
|
37 |
||
38 |
static PrintStream log = System.err; |
|
39 |
||
40 |
public static void main(String[] args) throws Exception { |
|
41 |
test(); |
|
42 |
} |
|
43 |
||
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 | 50 |
log.println("Classic RW: OK"); |
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 | 54 |
log.println("Classic R, Nio W: OK"); |
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 | 58 |
log.println("Classic W, Nio R: OK"); |
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 | 62 |
log.println("Nio RW: OK"); |
63 |
} |
|
64 |
||
65 |
static void invoke(Sprintable reader, Sprintable writer) throws Exception { |
|
66 |
Thread readerThread = new Thread(reader); |
|
67 |
readerThread.start(); |
|
68 |
Thread.sleep(50); |
|
69 |
||
70 |
Thread writerThread = new Thread(writer); |
|
71 |
writerThread.start(); |
|
72 |
||
73 |
writerThread.join(); |
|
74 |
readerThread.join(); |
|
75 |
||
76 |
reader.throwException(); |
|
77 |
writer.throwException(); |
|
78 |
} |
|
79 |
||
80 |
public interface Sprintable extends Runnable { |
|
81 |
public void throwException() throws Exception; |
|
82 |
} |
|
83 |
||
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 | 86 |
Exception e = null; |
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 | 92 |
public void throwException() throws Exception { |
93 |
if (e != null) |
|
94 |
throw e; |
|
95 |
} |
|
96 |
||
97 |
public void run() { |
|
98 |
try { |
|
99 |
DatagramSocket ds = new DatagramSocket(); |
|
100 |
String dataString = "hello"; |
|
101 |
byte[] data = dataString.getBytes(); |
|
102 |
InetAddress address = InetAddress.getLocalHost(); |
|
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 | 105 |
ds.send(dp); |
106 |
Thread.sleep(50); |
|
107 |
ds.send(dp); |
|
108 |
} catch (Exception ex) { |
|
109 |
e = ex; |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
||
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 | 116 |
Exception e = null; |
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 | 122 |
public void throwException() throws Exception { |
123 |
if (e != null) |
|
124 |
throw e; |
|
125 |
} |
|
126 |
||
127 |
public void run() { |
|
128 |
try { |
|
129 |
DatagramChannel dc = DatagramChannel.open(); |
|
130 |
ByteBuffer bb = ByteBuffer.allocateDirect(256); |
|
131 |
bb.put("hello".getBytes()); |
|
132 |
bb.flip(); |
|
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 | 135 |
dc.send(bb, isa); |
136 |
Thread.sleep(50); |
|
137 |
dc.send(bb, isa); |
|
138 |
} catch (Exception ex) { |
|
139 |
e = ex; |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
||
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 | 146 |
Exception e = null; |
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 | 156 |
public void throwException() throws Exception { |
157 |
if (e != null) |
|
158 |
throw e; |
|
159 |
} |
|
160 |
||
161 |
public void run() { |
|
162 |
try { |
|
163 |
byte[] buf = new byte[256]; |
|
164 |
DatagramPacket dp = new DatagramPacket(buf, buf.length); |
|
165 |
ds.receive(dp); |
|
166 |
String received = new String(dp.getData()); |
|
167 |
log.println(received); |
|
168 |
ds.close(); |
|
169 |
} catch (Exception ex) { |
|
170 |
e = ex; |
|
171 |
} |
|
172 |
} |
|
173 |
} |
|
174 |
||
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 | 177 |
Exception e = null; |
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 | 187 |
public void throwException() throws Exception { |
188 |
if (e != null) |
|
189 |
throw e; |
|
190 |
} |
|
191 |
||
192 |
public void run() { |
|
193 |
try { |
|
194 |
ByteBuffer bb = ByteBuffer.allocateDirect(100); |
|
195 |
SocketAddress sa = dc.receive(bb); |
|
196 |
bb.flip(); |
|
197 |
CharBuffer cb = Charset.forName("US-ASCII"). |
|
198 |
newDecoder().decode(bb); |
|
199 |
log.println("From: "+sa+ " said " +cb); |
|
200 |
dc.close(); |
|
201 |
} catch (Exception ex) { |
|
202 |
e = ex; |
|
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
} |