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