1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 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 * 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. 22 */ 23 24 /* @test 25 * @bug 8195160 26 * @summary Unit test for server-socket channels 27 * @requires (os.family == "linux") 28 * @library .. /test/lib /test/jdk/java/nio/channels 29 * @build RsocketTest 30 * @run main/othervm -Djava.net.preferIPv4Stack=true Basic 31 */ 32 33 import java.io.IOException; 34 import java.io.PrintStream; 35 import java.net.InetAddress; 36 import java.net.InetSocketAddress; 37 import java.nio.ByteBuffer; 38 import java.nio.channels.ServerSocketChannel; 39 import java.nio.channels.SocketChannel; 40 import java.util.Random; 41 import jdk.net.Sockets; 42 43 public class Basic { 44 45 static PrintStream log = System.err; 46 47 static class Server 48 extends TestThread 49 { 50 ServerSocketChannel ssc; 51 boolean block; 52 53 Server(ServerSocketChannel ssc, boolean block) { 54 super("Server", Basic.log); 55 this.ssc = ssc; 56 this.block = block; 57 } 58 59 void go() throws Exception { 60 log.println("Server: Listening " 61 + (block ? "(blocking)" : "(non-blocking)")); 62 if (!block) 63 ssc.configureBlocking(false); 64 log.println(" " + ssc); 65 SocketChannel sc = null; 66 for (;;) { 67 sc = ssc.accept(); 68 if (sc != null) { 69 break; 70 } 71 log.println("Server: Sleeping..."); 72 Thread.sleep(50); 73 } 74 log.println("Server: Accepted " + sc); 75 ByteBuffer bb = ByteBuffer.allocateDirect(100); 76 if (sc.read(bb) != 1) 77 throw new Exception("Read failed"); 78 bb.flip(); 79 byte b = bb.get(); 80 log.println("Server: Read " + b + ", writing " + (b + 1)); 81 bb.clear(); 82 bb.put((byte)43); 83 bb.flip(); 84 if (sc.write(bb) != 1) 85 throw new Exception("Write failed"); 86 sc.close(); 87 ssc.close(); 88 log.println("Server: Finished"); 89 } 90 91 } 92 93 static class Client 94 extends TestThread 95 { 96 int port; 97 boolean dally; 98 99 Client(int port, boolean block) { 100 super("Client", Basic.log); 101 this.port = port; 102 this.dally = !block; 103 } 104 105 public void go() throws Exception { 106 if (dally) 107 Thread.sleep(200); 108 InetSocketAddress isa 109 = new InetSocketAddress(InetAddress.getLocalHost(), port); 110 log.println("Client: Connecting to " + isa); 111 SocketChannel sc = Sockets.openRdmaSocketChannel(); 112 sc.connect(isa); 113 log.println("Client: Connected"); 114 ByteBuffer bb = ByteBuffer.allocateDirect(512); 115 bb.put((byte)42).flip(); 116 log.println("Client: Writing " + bb.get(0)); 117 if (sc.write(bb) != 1) 118 throw new Exception("Write failed"); 119 bb.clear(); 120 if (sc.read(bb) != 1) 121 throw new Exception("Read failed"); 122 bb.flip(); 123 if (bb.get() != 43) 124 throw new Exception("Read " + bb.get(bb.position() - 1)); 125 log.println("Client: Read " + bb.get(0)); 126 sc.close(); 127 log.println("Client: Finished"); 128 } 129 130 } 131 132 static void test(boolean block) throws Exception { 133 ServerSocketChannel ssc = Sockets.openRdmaServerSocketChannel(); 134 ssc.socket().setReuseAddress(true); 135 InetAddress lh = InetAddress.getLocalHost(); 136 int port; 137 Random r = new Random(); 138 for (;;) { 139 port = r.nextInt((1 << 16) - 1024) + 1024; 140 InetSocketAddress isa = new InetSocketAddress(lh, port); 141 try { 142 ssc.socket().bind(isa); 143 } catch (IOException x) { 144 continue; 145 } 146 break; 147 } 148 149 Server server = new Server(ssc, block); 150 Client client = new Client(port, block); 151 server.start(); 152 client.start(); 153 if ((server.finish(0) & client.finish(0)) == 0) 154 throw new Exception("Failure"); 155 log.println(); 156 } 157 158 public static void main(String[] args) throws Exception { 159 if (!RsocketTest.isRsocketAvailable()) 160 return; 161 162 log.println(); 163 test(true); 164 test(false); 165 } 166 167 }