blob: c792abbf41c364cad7835c4e4b341ae4ce8414db [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @summary Test SocketChannel.finishConnect
26 * @library ..
27 */
28
29import java.net.*;
30import java.nio.*;
31import java.nio.channels.*;
32import java.nio.channels.spi.SelectorProvider;
33import java.nio.charset.*;
34import java.util.*;
35
36
37public class FinishConnect {
38
39 static final int DAYTIME_PORT = 13;
40 static final String DAYTIME_HOST = TestUtil.HOST;
41
42 public static void main(String[] args) throws Exception {
43 test1(true, true);
44 test1(true, false);
45 test1(false, true);
46 test1(false, false);
47 test2();
48 }
49
50 static void test1(boolean select, boolean setBlocking) throws Exception {
51 InetSocketAddress isa
52 = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
53 DAYTIME_PORT);
54 SocketChannel sc = SocketChannel.open();
55 sc.configureBlocking(false);
56 boolean connected = sc.connect(isa);
57 int attempts = 0;
58
59 try {
60 sc.connect(isa);
61 throw new RuntimeException("Allowed another connect call");
62 } catch (IllegalStateException ise) {
63 // Correct behavior
64 }
65
66 if (setBlocking)
67 sc.configureBlocking(true);
68
69 if (!connected && select && !setBlocking) {
70 Selector selector = SelectorProvider.provider().openSelector();
71 sc.register(selector, SelectionKey.OP_CONNECT);
72 while (!connected) {
73 int keysAdded = selector.select(100);
74 if (keysAdded > 0) {
75 Set readyKeys = selector.selectedKeys();
76 Iterator i = readyKeys.iterator();
77 while (i.hasNext()) {
78 SelectionKey sk = (SelectionKey)i.next();
79 SocketChannel nextReady =
80 (SocketChannel)sk.channel();
81 connected = sc.finishConnect();
82 }
83 }
84 }
85 selector.close();
86 }
87
88 while (!connected) {
89 if (attempts++ > 30)
90 throw new RuntimeException("Failed to connect");
91 Thread.sleep(100);
92 connected = sc.finishConnect();
93 }
94
95 ByteBuffer bb = ByteBuffer.allocateDirect(100);
96 int bytesRead = 0;
97 int totalRead = 0;
98 while (totalRead < 20) {
99 bytesRead = sc.read(bb);
100 if (bytesRead > 0)
101 totalRead += bytesRead;
102 if (bytesRead < 0)
103 throw new RuntimeException("Message shorter than expected");
104 }
105 bb.position(bb.position() - 2); // Drop CRLF
106 bb.flip();
107 CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
108 System.err.println(isa + " says: \"" + cb + "\"");
109 sc.close();
110 }
111
112 static void test2() throws Exception {
113 InetSocketAddress isa
114 = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
115 DAYTIME_PORT);
116 boolean done = false;
117 int globalAttempts = 0;
118 while (!done) {
119 if (globalAttempts++ > 50)
120 throw new RuntimeException("Failed to connect");
121 SocketChannel sc = SocketChannel.open();
122 sc.configureBlocking(false);
123 boolean connected = sc.connect(isa);
124 int localAttempts = 0;
125 while (!connected) {
126 if (localAttempts++ > 500)
127 throw new RuntimeException("Failed to connect");
128 connected = sc.finishConnect();
129 if (connected) {
130 done = true;
131 break;
132 }
133 Thread.sleep(10);
134 }
135 sc.close();
136 }
137 }
138
139}