author | alanb |
Sun, 31 Aug 2008 18:39:01 +0100 | |
changeset 1152 | 29d6145d1097 |
parent 2 | 90ce3da70b43 |
child 1247 | b4c26443dee5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2000-2006 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.io.FileDescriptor; |
|
29 |
import java.io.IOException; |
|
30 |
import java.lang.reflect.*; |
|
31 |
import java.net.*; |
|
32 |
import java.nio.channels.*; |
|
33 |
import java.nio.channels.spi.*; |
|
34 |
import java.security.AccessController; |
|
35 |
import java.security.PrivilegedAction; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
36 |
import java.util.*; |
2 | 37 |
|
38 |
||
39 |
/** |
|
40 |
* An implementation of ServerSocketChannels |
|
41 |
*/ |
|
42 |
||
43 |
class ServerSocketChannelImpl |
|
44 |
extends ServerSocketChannel |
|
45 |
implements SelChImpl |
|
46 |
{ |
|
47 |
||
48 |
// Used to make native close and configure calls |
|
49 |
private static NativeDispatcher nd; |
|
50 |
||
51 |
// Our file descriptor |
|
52 |
private final FileDescriptor fd; |
|
53 |
||
54 |
// fd value needed for dev/poll. This value will remain valid |
|
55 |
// even after the value in the file descriptor object has been set to -1 |
|
56 |
private int fdVal; |
|
57 |
||
58 |
// ID of native thread currently blocked in this channel, for signalling |
|
59 |
private volatile long thread = 0; |
|
60 |
||
61 |
// Lock held by thread currently blocked in this channel |
|
62 |
private final Object lock = new Object(); |
|
63 |
||
64 |
// Lock held by any thread that modifies the state fields declared below |
|
65 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
66 |
private final Object stateLock = new Object(); |
|
67 |
||
68 |
// -- The following fields are protected by stateLock |
|
69 |
||
70 |
// Channel state, increases monotonically |
|
71 |
private static final int ST_UNINITIALIZED = -1; |
|
72 |
private static final int ST_INUSE = 0; |
|
73 |
private static final int ST_KILLED = 1; |
|
74 |
private int state = ST_UNINITIALIZED; |
|
75 |
||
76 |
// Binding |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
77 |
private SocketAddress localAddress; // null => unbound |
2 | 78 |
|
79 |
// Our socket adaptor, if any |
|
80 |
ServerSocket socket; |
|
81 |
||
82 |
// -- End of fields protected by stateLock |
|
83 |
||
84 |
||
85 |
public ServerSocketChannelImpl(SelectorProvider sp) throws IOException { |
|
86 |
super(sp); |
|
87 |
this.fd = Net.serverSocket(true); |
|
88 |
this.fdVal = IOUtil.fdVal(fd); |
|
89 |
this.state = ST_INUSE; |
|
90 |
} |
|
91 |
||
92 |
public ServerSocketChannelImpl(SelectorProvider sp, FileDescriptor fd) |
|
93 |
throws IOException |
|
94 |
{ |
|
95 |
super(sp); |
|
96 |
this.fd = fd; |
|
97 |
this.fdVal = IOUtil.fdVal(fd); |
|
98 |
this.state = ST_INUSE; |
|
99 |
localAddress = Net.localAddress(fd); |
|
100 |
} |
|
101 |
||
102 |
public ServerSocket socket() { |
|
103 |
synchronized (stateLock) { |
|
104 |
if (socket == null) |
|
105 |
socket = ServerSocketAdaptor.create(this); |
|
106 |
return socket; |
|
107 |
} |
|
108 |
} |
|
109 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
110 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
111 |
public SocketAddress getLocalAddress() throws IOException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
112 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
113 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
114 |
return null; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
115 |
return localAddress; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
116 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
117 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
118 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
119 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
120 |
public ServerSocketChannel setOption(SocketOption name, Object value) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
121 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
122 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
123 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
124 |
throw new NullPointerException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
125 |
if (!options().contains(name)) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
126 |
throw new IllegalArgumentException("invalid option name"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
127 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
128 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
129 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
130 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
131 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
132 |
// no options that require special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
133 |
Net.setSocketOption(fd, Net.UNSPEC, name, value); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
134 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
135 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
136 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
137 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
138 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
139 |
@SuppressWarnings("unchecked") |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
140 |
public <T> T getOption(SocketOption<T> name) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
141 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
142 |
{ |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
143 |
if (name == null) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
144 |
throw new NullPointerException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
145 |
if (!options().contains(name)) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
146 |
throw new IllegalArgumentException("invalid option name"); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
147 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
148 |
synchronized (stateLock) { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
149 |
if (!isOpen()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
150 |
throw new ClosedChannelException(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
151 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
152 |
// no options that require special handling |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
153 |
return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
154 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
155 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
156 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
157 |
private static class LazyInitialization { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
158 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
159 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
160 |
private static Set<SocketOption<?>> defaultOptions() { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
161 |
HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(2); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
162 |
set.add(StandardSocketOption.SO_RCVBUF); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
163 |
set.add(StandardSocketOption.SO_REUSEADDR); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
164 |
return Collections.unmodifiableSet(set); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
165 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
166 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
167 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
168 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
169 |
public final Set<SocketOption<?>> options() { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
170 |
return LazyInitialization.defaultOptions; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
171 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
172 |
|
2 | 173 |
public boolean isBound() { |
174 |
synchronized (stateLock) { |
|
175 |
return localAddress != null; |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
public SocketAddress localAddress() { |
|
180 |
synchronized (stateLock) { |
|
181 |
return localAddress; |
|
182 |
} |
|
183 |
} |
|
184 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
185 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
186 |
public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException { |
2 | 187 |
synchronized (lock) { |
188 |
if (!isOpen()) |
|
189 |
throw new ClosedChannelException(); |
|
190 |
if (isBound()) |
|
191 |
throw new AlreadyBoundException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
192 |
InetSocketAddress isa = (local == null) ? new InetSocketAddress(0) : |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
193 |
Net.checkAddress(local); |
2 | 194 |
SecurityManager sm = System.getSecurityManager(); |
195 |
if (sm != null) |
|
196 |
sm.checkListen(isa.getPort()); |
|
197 |
Net.bind(fd, isa.getAddress(), isa.getPort()); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
198 |
Net.listen(fd, backlog < 1 ? 50 : backlog); |
2 | 199 |
synchronized (stateLock) { |
200 |
localAddress = Net.localAddress(fd); |
|
201 |
} |
|
202 |
} |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
203 |
return this; |
2 | 204 |
} |
205 |
||
206 |
public SocketChannel accept() throws IOException { |
|
207 |
synchronized (lock) { |
|
208 |
if (!isOpen()) |
|
209 |
throw new ClosedChannelException(); |
|
210 |
if (!isBound()) |
|
211 |
throw new NotYetBoundException(); |
|
212 |
SocketChannel sc = null; |
|
213 |
||
214 |
int n = 0; |
|
215 |
FileDescriptor newfd = new FileDescriptor(); |
|
216 |
InetSocketAddress[] isaa = new InetSocketAddress[1]; |
|
217 |
||
218 |
try { |
|
219 |
begin(); |
|
220 |
if (!isOpen()) |
|
221 |
return null; |
|
222 |
thread = NativeThread.current(); |
|
223 |
for (;;) { |
|
224 |
n = accept0(this.fd, newfd, isaa); |
|
225 |
if ((n == IOStatus.INTERRUPTED) && isOpen()) |
|
226 |
continue; |
|
227 |
break; |
|
228 |
} |
|
229 |
} finally { |
|
230 |
thread = 0; |
|
231 |
end(n > 0); |
|
232 |
assert IOStatus.check(n); |
|
233 |
} |
|
234 |
||
235 |
if (n < 1) |
|
236 |
return null; |
|
237 |
||
238 |
IOUtil.configureBlocking(newfd, true); |
|
239 |
InetSocketAddress isa = isaa[0]; |
|
240 |
sc = new SocketChannelImpl(provider(), newfd, isa); |
|
241 |
SecurityManager sm = System.getSecurityManager(); |
|
242 |
if (sm != null) { |
|
243 |
try { |
|
244 |
sm.checkAccept(isa.getAddress().getHostAddress(), |
|
245 |
isa.getPort()); |
|
246 |
} catch (SecurityException x) { |
|
247 |
sc.close(); |
|
248 |
throw x; |
|
249 |
} |
|
250 |
} |
|
251 |
return sc; |
|
252 |
||
253 |
} |
|
254 |
} |
|
255 |
||
256 |
protected void implConfigureBlocking(boolean block) throws IOException { |
|
257 |
IOUtil.configureBlocking(fd, block); |
|
258 |
} |
|
259 |
||
260 |
protected void implCloseSelectableChannel() throws IOException { |
|
261 |
synchronized (stateLock) { |
|
262 |
nd.preClose(fd); |
|
263 |
long th = thread; |
|
264 |
if (th != 0) |
|
265 |
NativeThread.signal(th); |
|
266 |
if (!isRegistered()) |
|
267 |
kill(); |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
public void kill() throws IOException { |
|
272 |
synchronized (stateLock) { |
|
273 |
if (state == ST_KILLED) |
|
274 |
return; |
|
275 |
if (state == ST_UNINITIALIZED) { |
|
276 |
state = ST_KILLED; |
|
277 |
return; |
|
278 |
} |
|
279 |
assert !isOpen() && !isRegistered(); |
|
280 |
nd.close(fd); |
|
281 |
state = ST_KILLED; |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
/** |
|
286 |
* Translates native poll revent set into a ready operation set |
|
287 |
*/ |
|
288 |
public boolean translateReadyOps(int ops, int initialOps, |
|
289 |
SelectionKeyImpl sk) { |
|
290 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
291 |
int oldOps = sk.nioReadyOps(); |
|
292 |
int newOps = initialOps; |
|
293 |
||
294 |
if ((ops & PollArrayWrapper.POLLNVAL) != 0) { |
|
295 |
// This should only happen if this channel is pre-closed while a |
|
296 |
// selection operation is in progress |
|
297 |
// ## Throw an error if this channel has not been pre-closed |
|
298 |
return false; |
|
299 |
} |
|
300 |
||
301 |
if ((ops & (PollArrayWrapper.POLLERR |
|
302 |
| PollArrayWrapper.POLLHUP)) != 0) { |
|
303 |
newOps = intOps; |
|
304 |
sk.nioReadyOps(newOps); |
|
305 |
return (newOps & ~oldOps) != 0; |
|
306 |
} |
|
307 |
||
308 |
if (((ops & PollArrayWrapper.POLLIN) != 0) && |
|
309 |
((intOps & SelectionKey.OP_ACCEPT) != 0)) |
|
310 |
newOps |= SelectionKey.OP_ACCEPT; |
|
311 |
||
312 |
sk.nioReadyOps(newOps); |
|
313 |
return (newOps & ~oldOps) != 0; |
|
314 |
} |
|
315 |
||
316 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
317 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
318 |
} |
|
319 |
||
320 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
321 |
return translateReadyOps(ops, 0, sk); |
|
322 |
} |
|
323 |
||
324 |
/** |
|
325 |
* Translates an interest operation set into a native poll event set |
|
326 |
*/ |
|
327 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
328 |
int newOps = 0; |
|
329 |
||
330 |
// Translate ops |
|
331 |
if ((ops & SelectionKey.OP_ACCEPT) != 0) |
|
332 |
newOps |= PollArrayWrapper.POLLIN; |
|
333 |
// Place ops into pollfd array |
|
334 |
sk.selector.putEventOps(sk, newOps); |
|
335 |
} |
|
336 |
||
337 |
public FileDescriptor getFD() { |
|
338 |
return fd; |
|
339 |
} |
|
340 |
||
341 |
public int getFDVal() { |
|
342 |
return fdVal; |
|
343 |
} |
|
344 |
||
345 |
public String toString() { |
|
346 |
StringBuffer sb = new StringBuffer(); |
|
347 |
sb.append(this.getClass().getName()); |
|
348 |
sb.append('['); |
|
349 |
if (!isOpen()) |
|
350 |
sb.append("closed"); |
|
351 |
else { |
|
352 |
synchronized (stateLock) { |
|
353 |
if (localAddress() == null) { |
|
354 |
sb.append("unbound"); |
|
355 |
} else { |
|
356 |
sb.append(localAddress().toString()); |
|
357 |
} |
|
358 |
} |
|
359 |
} |
|
360 |
sb.append(']'); |
|
361 |
return sb.toString(); |
|
362 |
} |
|
363 |
||
364 |
// -- Native methods -- |
|
365 |
||
366 |
// Accepts a new connection, setting the given file descriptor to refer to |
|
367 |
// the new socket and setting isaa[0] to the socket's remote address. |
|
368 |
// Returns 1 on success, or IOStatus.UNAVAILABLE (if non-blocking and no |
|
369 |
// connections are pending) or IOStatus.INTERRUPTED. |
|
370 |
// |
|
371 |
private native int accept0(FileDescriptor ssfd, FileDescriptor newfd, |
|
372 |
InetSocketAddress[] isaa) |
|
373 |
throws IOException; |
|
374 |
||
375 |
private static native void initIDs(); |
|
376 |
||
377 |
static { |
|
378 |
Util.load(); |
|
379 |
initIDs(); |
|
380 |
nd = new SocketDispatcher(); |
|
381 |
} |
|
382 |
||
383 |
} |