0N/A/*
2362N/A * Copyright (c) 2006, 2008, 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
368N/A * @bug 6285901 6501089
0N/A * @summary Check no data is written to wrong socket channel during async closing.
0N/A * @author Xueming Shen
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.net.*;
0N/A
0N/Apublic class AsyncCloseChannel {
368N/A static volatile boolean failed = false;
368N/A static volatile boolean keepGoing = true;
368N/A static int maxAcceptCount = 100;
368N/A static volatile int acceptCount = 0;
0N/A static String host = "127.0.0.1";
368N/A static int sensorPort;
368N/A static int targetPort;
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A if (System.getProperty("os.name").startsWith("Windows")) {
0N/A System.err.println("WARNING: Still does not work on Windows!");
0N/A return;
0N/A }
0N/A Thread ss = new SensorServer(); ss.start();
0N/A Thread ts = new TargetServer(); ts.start();
368N/A
368N/A sensorPort = ((ServerThread)ss).server.getLocalPort();
368N/A targetPort = ((ServerThread)ts).server.getLocalPort();
368N/A
0N/A Thread sc = new SensorClient(); sc.start();
0N/A Thread tc = new TargetClient(); tc.start();
0N/A
0N/A while(acceptCount < maxAcceptCount && !failed) {
368N/A Thread.sleep(10);
0N/A }
0N/A keepGoing = false;
0N/A try {
0N/A ss.interrupt();
0N/A ts.interrupt();
0N/A sc.interrupt();
0N/A tc.interrupt();
0N/A } catch (Exception e) {}
0N/A if (failed)
0N/A throw new RuntimeException("AsyncCloseChannel2 failed after <"
0N/A + acceptCount + "> times of accept!");
0N/A }
0N/A
368N/A static class SensorServer extends ServerThread {
0N/A public void runEx() throws Exception {
0N/A while(keepGoing) {
0N/A try {
0N/A final Socket s = server.accept();
0N/A new Thread() {
0N/A public void run() {
0N/A try {
0N/A int c = s.getInputStream().read();
0N/A if(c != -1) {
0N/A // No data is ever written to the peer's socket!
368N/A System.err.println("Oops: read a character: "
0N/A + (char) c);
0N/A failed = true;
0N/A }
0N/A } catch (IOException ex) {
0N/A ex.printStackTrace();
0N/A } finally {
0N/A closeIt(s);
0N/A }
0N/A }
0N/A }.start();
0N/A } catch (IOException ex) {
368N/A System.err.println("Exception on sensor server " + ex.getMessage());
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
368N/A static class TargetServer extends ServerThread {
0N/A public void runEx() throws Exception {
0N/A while (keepGoing) {
0N/A try {
0N/A final Socket s = server.accept();
0N/A acceptCount++;
0N/A new Thread() {
0N/A public void run() {
0N/A boolean empty = true;
0N/A try {
0N/A for(;;) {
0N/A int c = s.getInputStream().read();
0N/A if(c == -1) {
0N/A if(!empty)
0N/A break;
0N/A }
0N/A empty = false;
0N/A }
0N/A } catch (IOException ex) {
0N/A ex.printStackTrace();
0N/A } finally {
0N/A closeIt(s);
0N/A }
0N/A }
0N/A }.start();
0N/A } catch (IOException ex) {
368N/A System.err.println("Exception on target server " + ex.getMessage());
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A static class SensorClient extends Thread {
0N/A private static boolean wake;
0N/A private static SensorClient theClient;
0N/A public void run() {
0N/A while (keepGoing) {
0N/A Socket s = null;
0N/A try {
0N/A s = new Socket();
0N/A synchronized(this) {
368N/A while(!wake && keepGoing) {
0N/A try {
0N/A wait();
0N/A } catch (InterruptedException ex) { }
0N/A }
368N/A wake = false;
0N/A }
0N/A s.connect(new InetSocketAddress(host, sensorPort));
0N/A try {
0N/A Thread.sleep(10);
0N/A } catch (InterruptedException ex) { }
0N/A } catch (IOException ex) {
368N/A System.err.println("Exception on sensor client " + ex.getMessage());
0N/A } finally {
0N/A if(s != null) {
0N/A try {
0N/A s.close();
0N/A } catch(IOException ex) { ex.printStackTrace();}
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public SensorClient() {
0N/A theClient = this;
0N/A }
0N/A
0N/A public static void wakeMe() {
0N/A synchronized(theClient) {
0N/A wake = true;
0N/A theClient.notify();
0N/A }
0N/A }
0N/A }
0N/A
0N/A static class TargetClient extends Thread {
0N/A volatile boolean ready = false;
0N/A public void run() {
0N/A while(keepGoing) {
0N/A try {
0N/A final SocketChannel s = SocketChannel.open(
0N/A new InetSocketAddress(host, targetPort));
0N/A s.finishConnect();
0N/A s.socket().setSoLinger(false, 0);
0N/A ready = false;
0N/A Thread t = new Thread() {
0N/A public void run() {
0N/A ByteBuffer b = ByteBuffer.allocate(1);
0N/A try {
0N/A for(;;) {
0N/A b.clear();
0N/A b.put((byte) 'A');
0N/A b.flip();
0N/A s.write(b);
0N/A ready = true;
0N/A }
0N/A } catch (IOException ex) {
0N/A if(!(ex instanceof ClosedChannelException))
368N/A System.err.println("Exception in target client child "
0N/A + ex.toString());
0N/A }
0N/A }
0N/A };
0N/A t.start();
368N/A while(!ready && keepGoing) {
368N/A try {
368N/A Thread.sleep(10);
368N/A } catch (InterruptedException ex) {}
368N/A }
0N/A s.close();
0N/A SensorClient.wakeMe();
0N/A t.join();
0N/A } catch (IOException ex) {
368N/A System.err.println("Exception in target client parent "
0N/A + ex.getMessage());
0N/A } catch (InterruptedException ex) {}
0N/A }
0N/A }
0N/A }
0N/A
368N/A static abstract class ServerThread extends Thread {
368N/A ServerSocket server;
368N/A public ServerThread() {
368N/A super();
368N/A try {
368N/A server = new ServerSocket(0);
368N/A } catch (IOException ex) {
368N/A ex.printStackTrace();
368N/A }
368N/A }
368N/A
368N/A public void interrupt() {
368N/A super.interrupt();
368N/A if (server != null) {
368N/A try {
368N/A server.close();
368N/A } catch (IOException ex) {
368N/A ex.printStackTrace();
368N/A }
368N/A }
368N/A }
0N/A public void run() {
0N/A try {
0N/A runEx();
0N/A } catch (Exception ex) {
0N/A ex.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A abstract void runEx() throws Exception;
0N/A }
0N/A
0N/A public static void closeIt(Socket s) {
0N/A try {
0N/A if(s != null)
0N/A s.close();
0N/A } catch (IOException ex) { }
0N/A }
0N/A}