提交 5c06ae9b 编写于 作者: A alanb

6993126: (aio) remove AsynchronousDatagramChannel

Reviewed-by: chegar
上级 676d64f3
......@@ -33,7 +33,6 @@ FILES_src = \
java/nio/channels/AsynchronousByteChannel.java \
java/nio/channels/AsynchronousChannel.java \
java/nio/channels/AsynchronousChannelGroup.java \
java/nio/channels/AsynchronousDatagramChannel.java \
java/nio/channels/AsynchronousFileChannel.java \
java/nio/channels/AsynchronousServerSocketChannel.java \
java/nio/channels/AsynchronousSocketChannel.java \
......@@ -207,7 +206,6 @@ FILES_src = \
sun/nio/ch/SelChImpl.java \
sun/nio/ch/ServerSocketAdaptor.java \
sun/nio/ch/ServerSocketChannelImpl.java \
sun/nio/ch/SimpleAsynchronousDatagramChannelImpl.java \
sun/nio/ch/SinkChannelImpl.java \
sun/nio/ch/SocketAdaptor.java \
sun/nio/ch/SocketChannelImpl.java \
......
......@@ -232,8 +232,6 @@
* <td>An asynchronous channel to a stream-oriented connecting socket</td></tr>
* <tr><td valign=top><tt>{@link java.nio.channels.AsynchronousServerSocketChannel}&nbsp;&nbsp;</tt></td>
* <td>An asynchronous channel to a stream-oriented listening socket</td></tr>
* <tr><td valign=top><tt>{@link java.nio.channels.AsynchronousDatagramChannel}</tt></td>
* <td>An asynchronous channel to a datagram-oriented socket</td></tr>
* <tr><td valign=top><tt>{@link java.nio.channels.CompletionHandler}</tt></td>
* <td>A handler for consuming the result of an asynchronous operation</td></tr>
* <tr><td valign=top><tt>{@link java.nio.channels.AsynchronousChannelGroup}</tt></td>
......
......@@ -26,7 +26,6 @@
package java.nio.channels.spi;
import java.nio.channels.*;
import java.net.ProtocolFamily;
import java.io.IOException;
import java.util.Iterator;
import java.util.ServiceLoader;
......@@ -239,26 +238,4 @@ public abstract class AsynchronousChannelProvider {
*/
public abstract AsynchronousSocketChannel openAsynchronousSocketChannel
(AsynchronousChannelGroup group) throws IOException;
/**
* Opens an asynchronous datagram channel.
*
* @param family
* The protocol family, or {@code null} for the default protocol
* family
* @param group
* The group to which the channel is bound, or {@code null} to
* bind to the default group
*
* @return The new channel
*
* @throws IllegalChannelGroupException
* If the provider that created the group differs from this provider
* @throws ShutdownChannelGroupException
* The group is shutdown
* @throws IOException
* If an I/O error occurs
*/
public abstract AsynchronousDatagramChannel openAsynchronousDatagramChannel
(ProtocolFamily family, AsynchronousChannelGroup group) throws IOException;
}
......@@ -29,7 +29,6 @@ import java.nio.channels.*;
import java.nio.channels.spi.AsynchronousChannelProvider;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.net.ProtocolFamily;
import java.io.IOException;
public class LinuxAsynchronousChannelProvider
......@@ -88,12 +87,4 @@ public class LinuxAsynchronousChannelProvider
{
return new UnixAsynchronousSocketChannelImpl(toPort(group));
}
@Override
public AsynchronousDatagramChannel openAsynchronousDatagramChannel(ProtocolFamily family,
AsynchronousChannelGroup group)
throws IOException
{
return new SimpleAsynchronousDatagramChannelImpl(family, toPort(group));
}
}
......@@ -29,7 +29,6 @@ import java.nio.channels.*;
import java.nio.channels.spi.AsynchronousChannelProvider;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.net.ProtocolFamily;
import java.io.IOException;
public class SolarisAsynchronousChannelProvider
......@@ -91,12 +90,4 @@ public class SolarisAsynchronousChannelProvider
{
return new UnixAsynchronousSocketChannelImpl(toEventPort(group));
}
@Override
public AsynchronousDatagramChannel openAsynchronousDatagramChannel(ProtocolFamily family,
AsynchronousChannelGroup group)
throws IOException
{
return new SimpleAsynchronousDatagramChannelImpl(family, toEventPort(group));
}
}
......@@ -29,7 +29,6 @@ import java.nio.channels.*;
import java.nio.channels.spi.AsynchronousChannelProvider;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.net.ProtocolFamily;
import java.io.IOException;
public class WindowsAsynchronousChannelProvider
......@@ -90,12 +89,4 @@ public class WindowsAsynchronousChannelProvider
{
return new WindowsAsynchronousSocketChannelImpl(toIocp(group));
}
@Override
public AsynchronousDatagramChannel openAsynchronousDatagramChannel(ProtocolFamily family,
AsynchronousChannelGroup group)
throws IOException
{
return new SimpleAsynchronousDatagramChannelImpl(family, toIocp(group));
}
}
......@@ -89,10 +89,9 @@ public class Basic {
}
// create channel that is bound to group
AsynchronousChannel ch;
switch (rand.nextInt(3)) {
switch (rand.nextInt(2)) {
case 0 : ch = AsynchronousSocketChannel.open(group); break;
case 1 : ch = AsynchronousServerSocketChannel.open(group); break;
case 2 : ch = AsynchronousDatagramChannel.open(null, group); break;
default : throw new AssertionError();
}
group.shutdown();
......@@ -128,18 +127,9 @@ public class Basic {
}
// I/O in progress
AsynchronousChannel ch;
if (rand.nextBoolean()) {
AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel
.open(group).bind(new InetSocketAddress(0));
listener.accept();
ch = listener;
} else {
AsynchronousDatagramChannel adc =
AsynchronousDatagramChannel.open(null, group);
adc.receive(ByteBuffer.allocate(100));
ch = adc;
}
AsynchronousServerSocketChannel ch = AsynchronousServerSocketChannel
.open(group).bind(new InetSocketAddress(0));
ch.accept();
// forceful shutdown
group.shutdownNow();
......
/*
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4527345 6842687
* @summary Unit test for AsynchronousDatagramChannel
*/
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.net.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
public class Basic {
public static void main(String[] args) throws Exception {
doReceiveTests();
doReadTests();
doSendTests();
doWriteTests();
doCancelTests();
doMulticastTests();
}
// basic receive tests
static void doReceiveTests() throws Exception {
final byte[] msg = "hello".getBytes();
AsynchronousDatagramChannel ch = AsynchronousDatagramChannel.open()
.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress)(ch.getLocalAddress())).getPort();
InetAddress rh = InetAddress.getLocalHost();
final SocketAddress sa = new InetSocketAddress(rh, port);
DatagramChannel sender = DatagramChannel.open();
ByteBuffer dst = ByteBuffer.allocateDirect(100);
// Test: datagram packet received immediately
sender.send(ByteBuffer.wrap(msg), sa);
dst.clear();
ch.receive(dst).get(1, TimeUnit.SECONDS);
if (dst.flip().remaining() != msg.length)
throw new RuntimeException("Unexpected number of bytes read");
// Test: datagram packet not received immediately
dst.clear();
final CountDownLatch latch = new CountDownLatch(1);
ch.receive(dst, (Void)null, new CompletionHandler<SocketAddress,Void>() {
public void completed(SocketAddress source, Void att) {
latch.countDown();
}
public void failed (Throwable exc, Void att) {
}
});
Thread.sleep(2000);
sender.send(ByteBuffer.wrap(msg), sa);
latch.await(2, TimeUnit.SECONDS); // wait for completion handler
// Test: timeout
dst.clear();
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
ch.receive(dst, 2, TimeUnit.SECONDS, (Void)null, new CompletionHandler<SocketAddress,Void>() {
public void completed(SocketAddress source, Void att) {
}
public void failed (Throwable exc, Void att) {
exception.set(exc);
}
});
Throwable result;
while ((result = exception.get()) == null) {
Thread.sleep(100);
}
if (!(result instanceof InterruptedByTimeoutException))
throw new RuntimeException("InterruptedByTimeoutException expected");
// AsynchronousCloseException
dst = ByteBuffer.allocateDirect(100);
exception.set(null);
ch.receive(dst, (Void)null, new CompletionHandler<SocketAddress,Void>() {
public void completed(SocketAddress source, Void att) {
}
public void failed (Throwable exc, Void att) {
exception.set(exc);
}
});
ch.close();
while ((result = exception.get()) == null) {
Thread.sleep(100);
}
if (!(result instanceof AsynchronousCloseException))
throw new RuntimeException("AsynchronousCloseException expected");
// done
sender.close();
}
// basic read tests
static void doReadTests() throws Exception {
final byte[] msg = "hello".getBytes();
AsynchronousDatagramChannel ch = AsynchronousDatagramChannel.open()
.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress)(ch.getLocalAddress())).getPort();
InetAddress lh = InetAddress.getLocalHost();
final SocketAddress sa = new InetSocketAddress(lh, port);
DatagramChannel sender = DatagramChannel.open();
ByteBuffer dst = ByteBuffer.allocateDirect(100);
// Test: not connected
try {
ch.read(dst);
throw new RuntimeException("NotYetConnectedException expected");
} catch (NotYetConnectedException e) {
}
// connect the channel
sender.bind(new InetSocketAddress(0));
ch.connect(new InetSocketAddress(lh,
((InetSocketAddress)(sender.getLocalAddress())).getPort()));
// Test: datagram packet received immediately
sender.send(ByteBuffer.wrap(msg), sa);
dst.clear();
ch.read(dst).get(1, TimeUnit.SECONDS);
if (dst.flip().remaining() != msg.length)
throw new RuntimeException("Unexpected number of bytes read");
// Test: datagram packet not received immediately
dst.clear();
final CountDownLatch l1 = new CountDownLatch(1);
ch.read(dst, (Void)null, new CompletionHandler<Integer,Void>() {
public void completed(Integer bytesRead, Void att) {
l1.countDown();
}
public void failed (Throwable exc, Void att) {
}
});
Thread.sleep(2000);
sender.send(ByteBuffer.wrap(msg), sa);
l1.await(2, TimeUnit.SECONDS);
// Test: timeout
dst.clear();
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
ch.read(dst, 2, TimeUnit.SECONDS, (Void)null, new CompletionHandler<Integer,Void>() {
public void completed(Integer bytesRead, Void att) {
}
public void failed (Throwable exc, Void att) {
exception.set(exc);
}
});
Throwable result;
while ((result = exception.get()) == null) {
Thread.sleep(100);
}
if (!(result instanceof InterruptedByTimeoutException))
throw new RuntimeException("InterruptedByTimeoutException expected");
// AsynchronousCloseException
dst.clear();
exception.set(null);
ch.read(dst, (Void)null, new CompletionHandler<Integer,Void>() {
public void completed(Integer bytesRead, Void att) {
}
public void failed (Throwable exc, Void att) {
exception.set(exc);
}
});
ch.close();
while ((result = exception.get()) == null) {
Thread.sleep(100);
}
if (!(result instanceof AsynchronousCloseException))
throw new RuntimeException("AsynchronousCloseException expected");
// done
sender.close();
}
// basic send tests
static void doSendTests() throws Exception {
final byte[] msg = "hello".getBytes();
DatagramChannel reader = DatagramChannel.open()
.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress)(reader.getLocalAddress())).getPort();
InetAddress rh = InetAddress.getLocalHost();
SocketAddress sa = new InetSocketAddress(rh, port);
AsynchronousDatagramChannel ch = AsynchronousDatagramChannel.open();
// Test: send datagram packet to reader
int bytesSent = ch.send(ByteBuffer.wrap(msg), sa).get();
if (bytesSent != msg.length)
throw new RuntimeException("Unexpected number of bytes sent");
// check received
ByteBuffer dst = ByteBuffer.allocateDirect(100);
reader.receive(dst);
dst.flip();
if (dst.remaining() != msg.length)
throw new RuntimeException("Unexpected number of bytes received");
// Test: send datagram packet to reader and check completion handler
// is invoked
final CountDownLatch l2 = new CountDownLatch(1);
ch.send(ByteBuffer.wrap(msg), sa, (Void)null, new CompletionHandler<Integer,Void>() {
public void completed(Integer bytesSent, Void att) {
if (bytesSent != msg.length)
throw new RuntimeException("Unexpected number of bytes received");
l2.countDown();
}
public void failed (Throwable exc, Void att) {
}
});
l2.await(5, TimeUnit.SECONDS);
// check received
dst.clear();
reader.receive(dst);
dst.flip();
if (dst.remaining() != msg.length)
throw new RuntimeException("Unexpected number of bytes received");
// Test: check that failed method is invoked
ch.close();
final CountDownLatch l3 = new CountDownLatch(1);
ch.send(ByteBuffer.wrap(msg), sa, (Void)null, new CompletionHandler<Integer,Void>() {
public void completed(Integer bytesSent, Void att) {
throw new RuntimeException("completed method invoked");
}
public void failed (Throwable exc, Void att) {
if (exc instanceof ClosedChannelException) {
l3.countDown();
} else {
throw new RuntimeException(exc);
}
}
});
l3.await(5, TimeUnit.SECONDS);
// done
reader.close();
}
// basic write tests
static void doWriteTests() throws Exception {
final byte[] msg = "hello".getBytes();
DatagramChannel reader = DatagramChannel.open()
.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress)(reader.getLocalAddress())).getPort();
InetAddress rh = InetAddress.getLocalHost();
SocketAddress sa = new InetSocketAddress(rh, port);
AsynchronousDatagramChannel ch = AsynchronousDatagramChannel.open();
// Test: unconnected
try {
ch.write(ByteBuffer.wrap(msg)).get();
throw new RuntimeException("NotYetConnectedException expected");
} catch (NotYetConnectedException e) {
}
// Test: connect, and write datagram
ch.connect(sa);
int bytesSent = ch.write(ByteBuffer.wrap(msg)).get();
if (bytesSent != msg.length)
throw new RuntimeException("Unexpected number of bytes sent");
// check received
ByteBuffer dst = ByteBuffer.allocateDirect(100);
reader.receive(dst);
dst.flip();
if (dst.remaining() != msg.length)
throw new RuntimeException("Unexpected number of bytes received");
// Test: write datagram and check completion handler is invoked
final CountDownLatch l2 = new CountDownLatch(1);
ch.write(ByteBuffer.wrap(msg), (Void)null, new CompletionHandler<Integer,Void>() {
public void completed(Integer bytesSent, Void att) {
if (bytesSent != msg.length)
throw new RuntimeException("Unexpected number of bytes received");
l2.countDown();
}
public void failed (Throwable exc, Void att) {
}
});
l2.await(5, TimeUnit.SECONDS);
// check received
dst.clear();
reader.receive(dst);
dst.flip();
if (dst.remaining() != msg.length)
throw new RuntimeException("Unexpected number of bytes received");
// done
ch.close();
reader.close();
}
static void cancelAndCheck(Future<?> result)
throws InterruptedException
{
boolean cancelled = result.cancel(false);
if (!cancelled)
throw new RuntimeException("Not cancelled");
if (!result.isDone())
throw new RuntimeException("Should be done");
try {
result.get();
throw new RuntimeException("Result not expected");
} catch (CancellationException e) {
// expected
} catch (ExecutionException e) {
throw new RuntimeException("Should not fail");
}
}
// basic cancel tests
static void doCancelTests() throws Exception {
InetAddress lh = InetAddress.getLocalHost();
// receive
for (int i=0; i<2; i++) {
AsynchronousDatagramChannel ch =
AsynchronousDatagramChannel.open().bind(new InetSocketAddress(0));
Future<SocketAddress> remote = ch.receive(ByteBuffer.allocate(100));
cancelAndCheck(remote);
ch.close();
}
// read
for (int i=0; i<2; i++) {
AsynchronousDatagramChannel ch =
AsynchronousDatagramChannel.open().bind(new InetSocketAddress(0));
ch.connect(new InetSocketAddress(lh,
((InetSocketAddress)(ch.getLocalAddress())).getPort()));
final CountDownLatch latch = new CountDownLatch(1);
long timeout = (i == 0) ? 0L : 60L;
Future<Integer> result = ch.read(ByteBuffer.allocate(100));
cancelAndCheck(result);
ch.close();
}
}
// basic multicast test
static void doMulticastTests() throws Exception {
final byte[] msg = "hello".getBytes();
InetAddress lh = InetAddress.getLocalHost();
NetworkInterface interf = NetworkInterface.getByInetAddress(lh);
if (interf.isLoopback() || !interf.supportsMulticast()) {
System.out.println("Multicasting not tested");
return;
}
AsynchronousDatagramChannel ch = AsynchronousDatagramChannel
.open(StandardProtocolFamily.INET, null)
.setOption(StandardSocketOption.SO_REUSEADDR, true)
.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress)(ch.getLocalAddress())).getPort();
// join group
InetAddress group = InetAddress.getByName("225.4.5.6");
MembershipKey key = ch.join(group, interf);
// check key
if (key.channel() != ch)
throw new RuntimeException("Not the expected channel");
// send message to group
DatagramChannel sender = DatagramChannel.open();
sender.send(ByteBuffer.wrap(msg), new InetSocketAddress(group, port));
sender.close();
// check message received
ByteBuffer dst = ByteBuffer.allocate(200);
SocketAddress source = ch.receive(dst).get(2, TimeUnit.SECONDS);
if (!((InetSocketAddress)source).getAddress().equals(lh))
throw new RuntimeException("Unexpected source");
// done
ch.close();
}
}
......@@ -23,7 +23,6 @@
import java.nio.channels.spi.AsynchronousChannelProvider;
import java.nio.channels.*;
import java.net.ProtocolFamily;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.io.IOException;
......@@ -59,11 +58,4 @@ public class Provider1 extends AsynchronousChannelProvider {
{
throw new RuntimeException();
}
@Override
public AsynchronousDatagramChannel openAsynchronousDatagramChannel
(ProtocolFamily family, AsynchronousChannelGroup group) throws IOException
{
throw new RuntimeException();
}
}
......@@ -23,7 +23,6 @@
import java.nio.channels.spi.AsynchronousChannelProvider;
import java.nio.channels.*;
import java.net.ProtocolFamily;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.io.IOException;
......@@ -59,11 +58,4 @@ public class Provider2 extends AsynchronousChannelProvider {
{
throw new RuntimeException();
}
@Override
public AsynchronousDatagramChannel openAsynchronousDatagramChannel
(ProtocolFamily family, AsynchronousChannelGroup group) throws IOException
{
throw new RuntimeException();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册