1 /* 2 * Copyright (c) 2000, 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 /* 25 * @test 26 * @bug 8195160 27 * @summary Test RdmaSelector with RdmaServerSocketChannels 28 * @requires (os.family == "linux") 29 * @library .. /test/lib 30 * @build RsocketTest 31 * @run main/othervm -Djava.net.preferIPv4Stack=true BasicAccept 32 */ 33 34 import java.io.*; 35 import java.net.*; 36 import java.nio.*; 37 import java.nio.channels.*; 38 import java.nio.channels.spi.SelectorProvider; 39 import java.util.*; 40 import jdk.net.Sockets; 41 42 public class BasicAccept { 43 44 static void server(ServerSocketChannel ssc) throws Exception { 45 Selector acceptSelector = Sockets.openRdmaSelector(); 46 try { 47 ssc.configureBlocking(false); 48 SelectionKey acceptKey 49 = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT); 50 for (;;) { 51 int n = acceptSelector.select(); 52 if (Thread.interrupted()) 53 break; 54 if (n == 0) 55 continue; 56 Set<SelectionKey> readyKeys = acceptSelector.selectedKeys(); 57 Iterator<SelectionKey> i = readyKeys.iterator(); 58 while (i.hasNext()) { 59 SelectionKey sk = i.next(); 60 i.remove(); 61 ServerSocketChannel nextReady 62 = (ServerSocketChannel)sk.channel(); 63 SocketChannel sc = nextReady.accept(); 64 ByteBuffer bb = ByteBuffer.wrap(new byte[] { 42 }); 65 sc.write(bb); 66 sc.close(); 67 } 68 } 69 } finally { 70 acceptSelector.close(); 71 } 72 } 73 74 private static class Server extends TestThread { 75 final ServerSocketChannel ssc; 76 Server() throws IOException { 77 super("Server", System.err); 78 this.ssc = Sockets.openRdmaServerSocketChannel() 79 .bind(new InetSocketAddress(InetAddress.getLocalHost(), 0)); 80 } 81 int port() { 82 return ssc.socket().getLocalPort(); 83 } 84 void go() throws Exception { 85 try { 86 server(ssc); 87 } finally { 88 ssc.close(); 89 } 90 } 91 } 92 93 static void client(int port) throws Exception { 94 // Get a connection from the server 95 InetAddress lh = InetAddress.getLocalHost(); 96 InetSocketAddress isa 97 = new InetSocketAddress(lh, port); 98 int connectFailures = 0; 99 boolean result = false; 100 SocketChannel sc = Sockets.openRdmaSocketChannel(); 101 for (;;) { 102 try { 103 result = sc.connect(isa); 104 break; 105 } catch (java.net.ConnectException e) { 106 connectFailures++; 107 if (connectFailures > 30) 108 throw new RuntimeException("Cannot connect"); 109 Thread.currentThread().sleep(100); 110 sc = Sockets.openRdmaSocketChannel(); 111 } 112 } 113 if (result) { 114 System.err.println("Connected"); 115 } else { 116 // Only happens when server and client are on separate machines 117 System.err.println("Connection pending..."); 118 connectFailures = 0; 119 while (!result) { 120 try { 121 result = sc.finishConnect(); 122 if (!result) 123 System.err.println("Not finished"); 124 Thread.sleep(50); 125 } catch (java.net.ConnectException e) { 126 Thread.sleep(100); 127 connectFailures++; 128 if (connectFailures > 30) 129 throw new RuntimeException("Cannot finish connecting"); 130 } 131 } 132 System.err.println("Finished connecting"); 133 } 134 135 ByteBuffer bb = ByteBuffer.allocateDirect(1024); 136 if (sc.read(bb) < 0) 137 throw new RuntimeException("Failed to read from server"); 138 if (bb.get(0) != 42) 139 throw new RuntimeException("Read wrong byte from server"); 140 System.err.println("Read from server"); 141 sc.close(); 142 } 143 144 public static void main(String[] args) throws Exception { 145 if (!RsocketTest.isRsocketAvailable()) 146 return; 147 148 Server server = new Server(); 149 server.start(); 150 try { 151 client(server.port()); 152 } finally { 153 server.interrupt(); 154 server.finish(2000); 155 } 156 } 157 158 }