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 Test for nonblocking connect and finishConnect 27 * @requires (os.family == "linux") 28 * @library .. /test/lib 29 * @build jdk.test.lib.Utils TestServers 30 * @build RsocketTest 31 * @run main/othervm -Djava.net.preferIPv4Stack=true BasicConnect 32 */ 33 34 import java.net.InetSocketAddress; 35 import java.nio.ByteBuffer; 36 import java.nio.channels.SelectionKey; 37 import java.nio.channels.Selector; 38 import java.nio.channels.SocketChannel; 39 import java.nio.channels.spi.SelectorProvider; 40 import java.util.Iterator; 41 import java.util.Set; 42 import jdk.net.Sockets; 43 44 /** 45 * Typically there would be more than one channel registered to select 46 * on, this test is just a very simple version with only one channel 47 * registered for the connectSelector. 48 */ 49 50 public class BasicConnect { 51 52 public static void main(String[] args) throws Exception { 53 if (!RsocketTest.isRsocketAvailable()) 54 return; 55 56 Selector connectSelector = 57 Sockets.openRdmaSelector(); 58 try { 59 TestServers.EchoServer echoServer 60 = TestServers.EchoServer.startNewServer(100); 61 InetSocketAddress isa 62 = new InetSocketAddress(echoServer.getAddress(), 63 echoServer.getPort()); 64 SocketChannel sc = Sockets.openRdmaSocketChannel(); 65 sc.configureBlocking(false); 66 boolean result = sc.connect(isa); 67 if (result) { 68 System.out.println("Socket immediately connected on " 69 + System.getProperty("os.name") 70 + ": " + sc); 71 } 72 while (!result) { 73 SelectionKey connectKey = sc.register(connectSelector, 74 SelectionKey.OP_CONNECT); 75 int keysAdded = connectSelector.select(); 76 if (keysAdded > 0) { 77 Set readyKeys = connectSelector.selectedKeys(); 78 Iterator i = readyKeys.iterator(); 79 while (i.hasNext()) { 80 SelectionKey sk = (SelectionKey)i.next(); 81 i.remove(); 82 SocketChannel nextReady = (SocketChannel)sk.channel(); 83 result = nextReady.finishConnect(); 84 if (result) 85 sk.cancel(); 86 } 87 } 88 } 89 90 byte[] bs = new byte[] { (byte)0xca, (byte)0xfe, 91 (byte)0xba, (byte)0xbe }; 92 ByteBuffer bb = ByteBuffer.wrap(bs); 93 sc.configureBlocking(true); 94 sc.write(bb); 95 bb.rewind(); 96 97 ByteBuffer bb2 = ByteBuffer.allocateDirect(100); 98 int n = sc.read(bb2); 99 bb2.flip(); 100 101 sc.close(); 102 connectSelector.close(); 103 104 if (!bb.equals(bb2)) 105 throw new Exception("Echoed bytes incorrect: Sent " 106 + bb + ", got " + bb2); 107 } catch (Exception e) { 108 e.printStackTrace(); 109 throw new RuntimeException("Test Failed!"); 110 } 111 } 112 }