0N/A/*
2362N/A * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
1021N/A * @bug 4512723 6621689
1021N/A * @summary Test that connect/send/receive with unbound DatagramChannel causes
1021N/A * the channel's socket to be bound to a local address
0N/A */
0N/A
0N/Aimport java.net.*;
1021N/Aimport java.nio.ByteBuffer;
1021N/Aimport java.nio.channels.DatagramChannel;
1021N/Aimport java.io.IOException;
1021N/A
1021N/Apublic class NotBound {
1021N/A
1021N/A static void checkBound(DatagramChannel dc) throws IOException {
1021N/A if (dc.getLocalAddress() == null)
1021N/A throw new RuntimeException("Not bound??");
1021N/A }
0N/A
1021N/A // starts a thread to send a datagram to the given channel once the channel
1021N/A // is bound to a local address
1021N/A static void wakeupWhenBound(final DatagramChannel dc) {
1021N/A Runnable wakeupTask = new Runnable() {
1021N/A public void run() {
1021N/A try {
1021N/A // poll for local address
1021N/A InetSocketAddress local;
1021N/A do {
1021N/A Thread.sleep(50);
1021N/A local = (InetSocketAddress)dc.getLocalAddress();
1021N/A } while (local == null);
1021N/A
1021N/A // send message to channel to wakeup receiver
1021N/A DatagramChannel sender = DatagramChannel.open();
1021N/A try {
1021N/A ByteBuffer bb = ByteBuffer.wrap("hello".getBytes());
1021N/A InetAddress lh = InetAddress.getLocalHost();
1021N/A SocketAddress target =
1021N/A new InetSocketAddress(lh, local.getPort());
1021N/A sender.send(bb, target);
1021N/A } finally {
1021N/A sender.close();
1021N/A }
1021N/A
1021N/A } catch (Exception x) {
1021N/A x.printStackTrace();
1021N/A }
1021N/A }};
1021N/A new Thread(wakeupTask).start();
0N/A }
0N/A
1021N/A public static void main(String[] args) throws IOException {
1021N/A DatagramChannel dc;
1021N/A
1021N/A // connect
1021N/A dc = DatagramChannel.open();
1021N/A try {
1021N/A DatagramChannel peer = DatagramChannel.open()
1021N/A .bind(new InetSocketAddress(0));
1021N/A int peerPort = ((InetSocketAddress)(peer.getLocalAddress())).getPort();
1021N/A try {
1021N/A dc.connect(new InetSocketAddress(InetAddress.getLocalHost(), peerPort));
1021N/A checkBound(dc);
1021N/A } finally {
1021N/A peer.close();
1021N/A }
1021N/A } finally {
1021N/A dc.close();
1021N/A }
1021N/A
1021N/A // send
1021N/A dc = DatagramChannel.open();
1021N/A try {
1021N/A ByteBuffer bb = ByteBuffer.wrap("ignore this".getBytes());
1021N/A SocketAddress target =
1021N/A new InetSocketAddress(InetAddress.getLocalHost(), 5000);
1021N/A dc.send(bb, target);
1021N/A checkBound(dc);
1021N/A } finally {
1021N/A dc.close();
1021N/A }
1021N/A
1021N/A // receive (blocking)
1021N/A dc = DatagramChannel.open();
1021N/A try {
1021N/A ByteBuffer bb = ByteBuffer.allocateDirect(128);
1021N/A wakeupWhenBound(dc);
1021N/A SocketAddress sender = dc.receive(bb);
1021N/A if (sender == null)
1021N/A throw new RuntimeException("Sender should not be null");
1021N/A checkBound(dc);
1021N/A } finally {
1021N/A dc.close();
1021N/A }
1021N/A
1021N/A // receive (non-blocking)
1021N/A dc = DatagramChannel.open();
1021N/A try {
1021N/A dc.configureBlocking(false);
1021N/A ByteBuffer bb = ByteBuffer.allocateDirect(128);
1021N/A SocketAddress sender = dc.receive(bb);
1021N/A if (sender != null)
1021N/A throw new RuntimeException("Sender should be null");
1021N/A checkBound(dc);
1021N/A } finally {
1021N/A dc.close();
1021N/A }
0N/A }
0N/A}