author | dxu |
Tue, 08 Jan 2013 20:37:27 +0000 | |
changeset 15007 | 31d8a6072b16 |
parent 5506 | 202f599c92aa |
child 19607 | bee007586d06 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
*/ |
|
28 |
||
29 |
package sun.nio.ch; |
|
30 |
||
31 |
import java.io.IOException; |
|
32 |
import java.net.InetAddress; |
|
33 |
import java.net.InetSocketAddress; |
|
34 |
import java.nio.*; |
|
35 |
import java.nio.channels.*; |
|
36 |
import java.nio.channels.spi.*; |
|
37 |
import java.security.AccessController; |
|
38 |
import java.security.PrivilegedExceptionAction; |
|
39 |
import java.security.PrivilegedActionException; |
|
40 |
import java.util.Random; |
|
41 |
||
42 |
||
43 |
/** |
|
44 |
* A simple Pipe implementation based on a socket connection. |
|
45 |
*/ |
|
46 |
||
47 |
class PipeImpl |
|
48 |
extends Pipe |
|
49 |
{ |
|
50 |
||
51 |
// Source and sink channels |
|
52 |
private SourceChannel source; |
|
53 |
private SinkChannel sink; |
|
54 |
||
55 |
// Random object for handshake values |
|
56 |
private static final Random rnd; |
|
57 |
||
58 |
static { |
|
59 |
Util.load(); |
|
60 |
byte[] someBytes = new byte[8]; |
|
61 |
boolean resultOK = IOUtil.randomBytes(someBytes); |
|
62 |
if (resultOK) { |
|
63 |
rnd = new Random(ByteBuffer.wrap(someBytes).getLong()); |
|
64 |
} else { |
|
65 |
rnd = new Random(); |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
private class Initializer |
|
895 | 70 |
implements PrivilegedExceptionAction<Void> |
2 | 71 |
{ |
72 |
||
73 |
private final SelectorProvider sp; |
|
74 |
||
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
75 |
private IOException ioe = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
76 |
|
2 | 77 |
private Initializer(SelectorProvider sp) { |
78 |
this.sp = sp; |
|
79 |
} |
|
80 |
||
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
81 |
@Override |
895 | 82 |
public Void run() throws IOException { |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
83 |
LoopbackConnector connector = new LoopbackConnector(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
84 |
connector.run(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
85 |
if (ioe instanceof ClosedByInterruptException) { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
86 |
ioe = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
87 |
Thread connThread = new Thread(connector) { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
88 |
@Override |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
89 |
public void interrupt() {} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
90 |
}; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
91 |
connThread.start(); |
2 | 92 |
for (;;) { |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
93 |
try { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
94 |
connThread.join(); |
2 | 95 |
break; |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
96 |
} catch (InterruptedException ex) {} |
2 | 97 |
} |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
98 |
Thread.currentThread().interrupt(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
99 |
} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
100 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
101 |
if (ioe != null) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
102 |
throw new IOException("Unable to establish loopback connection", ioe); |
2 | 103 |
|
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
104 |
return null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
105 |
} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
106 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
107 |
private class LoopbackConnector implements Runnable { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
108 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
109 |
@Override |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
110 |
public void run() { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
111 |
ServerSocketChannel ssc = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
112 |
SocketChannel sc1 = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
113 |
SocketChannel sc2 = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
114 |
|
2 | 115 |
try { |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
116 |
// Loopback address |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
117 |
InetAddress lb = InetAddress.getByName("127.0.0.1"); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
118 |
assert(lb.isLoopbackAddress()); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
119 |
InetSocketAddress sa = null; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
120 |
for(;;) { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
121 |
// Bind ServerSocketChannel to a port on the loopback |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
122 |
// address |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
123 |
if (ssc == null || !ssc.isOpen()) { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
124 |
ssc = ServerSocketChannel.open(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
125 |
ssc.socket().bind(new InetSocketAddress(lb, 0)); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
126 |
sa = new InetSocketAddress(lb, ssc.socket().getLocalPort()); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
127 |
} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
128 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
129 |
// Establish connection (assume connections are eagerly |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
130 |
// accepted) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
131 |
sc1 = SocketChannel.open(sa); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
132 |
ByteBuffer bb = ByteBuffer.allocate(8); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
133 |
long secret = rnd.nextLong(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
134 |
bb.putLong(secret).flip(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
135 |
sc1.write(bb); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
136 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
137 |
// Get a connection and verify it is legitimate |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
138 |
sc2 = ssc.accept(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
139 |
bb.clear(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
140 |
sc2.read(bb); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
141 |
bb.rewind(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
142 |
if (bb.getLong() == secret) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
143 |
break; |
2 | 144 |
sc2.close(); |
15007
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
145 |
sc1.close(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
146 |
} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
147 |
|
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
148 |
// Create source and sink channels |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
149 |
source = new SourceChannelImpl(sp, sc1); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
150 |
sink = new SinkChannelImpl(sp, sc2); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
151 |
} catch (IOException e) { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
152 |
try { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
153 |
if (sc1 != null) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
154 |
sc1.close(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
155 |
if (sc2 != null) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
156 |
sc2.close(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
157 |
} catch (IOException e2) {} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
158 |
ioe = e; |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
159 |
} finally { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
160 |
try { |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
161 |
if (ssc != null) |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
162 |
ssc.close(); |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
163 |
} catch (IOException e2) {} |
31d8a6072b16
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win]
dxu
parents:
5506
diff
changeset
|
164 |
} |
2 | 165 |
} |
166 |
} |
|
167 |
} |
|
168 |
||
169 |
PipeImpl(final SelectorProvider sp) throws IOException { |
|
170 |
try { |
|
171 |
AccessController.doPrivileged(new Initializer(sp)); |
|
172 |
} catch (PrivilegedActionException x) { |
|
173 |
throw (IOException)x.getCause(); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
public SourceChannel source() { |
|
178 |
return source; |
|
179 |
} |
|
180 |
||
181 |
public SinkChannel sink() { |
|
182 |
return sink; |
|
183 |
} |
|
184 |
||
185 |
} |