author | chegar |
Mon, 29 Jun 2009 14:53:10 +0100 | |
changeset 3072 | a801b122142f |
parent 2542 | d859108aea12 |
child 3320 | a7c037dd2e14 |
permissions | -rw-r--r-- |
2542 | 1 |
/* |
2 |
* Copyright 2009 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 |
package sun.nio.ch; |
|
26 |
||
27 |
import java.net.InetAddress; |
|
28 |
import java.net.SocketAddress; |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
29 |
import java.net.SocketException; |
2542 | 30 |
import java.net.InetSocketAddress; |
31 |
import java.io.FileDescriptor; |
|
32 |
import java.io.IOException; |
|
33 |
import java.util.Collections; |
|
34 |
import java.util.Set; |
|
35 |
import java.util.HashSet; |
|
36 |
import java.util.HashMap; |
|
37 |
import java.nio.ByteBuffer; |
|
38 |
import java.nio.channels.SelectionKey; |
|
39 |
import java.nio.channels.AlreadyBoundException; |
|
40 |
import java.nio.channels.ClosedChannelException; |
|
41 |
import java.nio.channels.NotYetBoundException; |
|
42 |
import java.nio.channels.spi.SelectorProvider; |
|
43 |
import com.sun.nio.sctp.AbstractNotificationHandler; |
|
44 |
import com.sun.nio.sctp.Association; |
|
45 |
import com.sun.nio.sctp.AssociationChangeNotification; |
|
46 |
import com.sun.nio.sctp.HandlerResult; |
|
47 |
import com.sun.nio.sctp.IllegalReceiveException; |
|
48 |
import com.sun.nio.sctp.InvalidStreamException; |
|
49 |
import com.sun.nio.sctp.IllegalUnbindException; |
|
50 |
import com.sun.nio.sctp.NotificationHandler; |
|
51 |
import com.sun.nio.sctp.MessageInfo; |
|
52 |
import com.sun.nio.sctp.SctpChannel; |
|
53 |
import com.sun.nio.sctp.SctpMultiChannel; |
|
54 |
import com.sun.nio.sctp.SctpSocketOption; |
|
55 |
import static com.sun.nio.sctp.SctpStandardSocketOption.*; |
|
56 |
import static sun.nio.ch.SctpResultContainer.*; |
|
57 |
||
58 |
/** |
|
59 |
* An implementation of SctpMultiChannel |
|
60 |
*/ |
|
61 |
public class SctpMultiChannelImpl extends SctpMultiChannel |
|
62 |
implements SelChImpl |
|
63 |
{ |
|
64 |
/* Used to make native close and preClose calls */ |
|
65 |
private static NativeDispatcher nd; |
|
66 |
||
67 |
private final FileDescriptor fd; |
|
68 |
||
69 |
private final int fdVal; |
|
70 |
||
71 |
/* IDs of native threads doing send and receives, for signalling */ |
|
72 |
private volatile long receiverThread = 0; |
|
73 |
private volatile long senderThread = 0; |
|
74 |
||
75 |
/* Lock held by current receiving thread */ |
|
76 |
private final Object receiveLock = new Object(); |
|
77 |
||
78 |
/* Lock held by current sending thread */ |
|
79 |
private final Object sendLock = new Object(); |
|
80 |
||
81 |
/* Lock held by any thread that modifies the state fields declared below |
|
82 |
* DO NOT invoke a blocking I/O operation while holding this lock! */ |
|
83 |
private final Object stateLock = new Object(); |
|
84 |
||
85 |
private enum ChannelState { |
|
86 |
UNINITIALIZED, |
|
87 |
KILLPENDING, |
|
88 |
KILLED, |
|
89 |
} |
|
90 |
||
91 |
/* -- The following fields are protected by stateLock -- */ |
|
92 |
private ChannelState state = ChannelState.UNINITIALIZED; |
|
93 |
||
94 |
/* Binding: Once bound the port will remain constant. */ |
|
95 |
int port = -1; |
|
96 |
private HashSet<InetSocketAddress> localAddresses = new HashSet<InetSocketAddress>(); |
|
97 |
/* Has the channel been bound to the wildcard address */ |
|
98 |
private boolean wildcard; /* false */ |
|
99 |
||
100 |
/* Keeps a map of addresses to association, and visa versa */ |
|
101 |
private HashMap<SocketAddress, Association> addressMap = |
|
102 |
new HashMap<SocketAddress, Association>(); |
|
103 |
private HashMap<Association, Set<SocketAddress>> associationMap = |
|
104 |
new HashMap<Association, Set<SocketAddress>>(); |
|
105 |
||
106 |
/* -- End of fields protected by stateLock -- */ |
|
107 |
||
108 |
/* If an association has been shutdown mark it for removal after |
|
109 |
* the user handler has been invoked */ |
|
110 |
private final ThreadLocal<Association> associationToRemove = |
|
111 |
new ThreadLocal<Association>() { |
|
112 |
@Override protected Association initialValue() { |
|
113 |
return null; |
|
114 |
} |
|
115 |
}; |
|
116 |
||
117 |
/* A notification handler cannot invoke receive */ |
|
118 |
private final ThreadLocal<Boolean> receiveInvoked = |
|
119 |
new ThreadLocal<Boolean>() { |
|
120 |
@Override protected Boolean initialValue() { |
|
121 |
return Boolean.FALSE; |
|
122 |
} |
|
123 |
}; |
|
124 |
||
125 |
public SctpMultiChannelImpl(SelectorProvider provider) |
|
126 |
throws IOException { |
|
127 |
//TODO: update provider, remove public modifier |
|
128 |
super(provider); |
|
129 |
this.fd = SctpNet.socket(false /*one-to-many*/); |
|
130 |
this.fdVal = IOUtil.fdVal(fd); |
|
131 |
} |
|
132 |
||
133 |
@Override |
|
134 |
public SctpMultiChannel bind(SocketAddress local, int backlog) |
|
135 |
throws IOException { |
|
136 |
synchronized (receiveLock) { |
|
137 |
synchronized (sendLock) { |
|
138 |
synchronized (stateLock) { |
|
139 |
ensureOpen(); |
|
140 |
if (isBound()) |
|
141 |
throw new AlreadyBoundException(); |
|
142 |
InetSocketAddress isa = (local == null) ? |
|
143 |
new InetSocketAddress(0) : Net.checkAddress(local); |
|
144 |
||
145 |
SecurityManager sm = System.getSecurityManager(); |
|
146 |
if (sm != null) |
|
147 |
sm.checkListen(isa.getPort()); |
|
148 |
Net.bind(fd, isa.getAddress(), isa.getPort()); |
|
149 |
||
150 |
InetSocketAddress boundIsa = Net.localAddress(fd); |
|
151 |
port = boundIsa.getPort(); |
|
152 |
localAddresses.add(isa); |
|
153 |
if (isa.getAddress().isAnyLocalAddress()) |
|
154 |
wildcard = true; |
|
155 |
||
156 |
Net.listen(fd, backlog < 1 ? 50 : backlog); |
|
157 |
} |
|
158 |
} |
|
159 |
} |
|
160 |
return this; |
|
161 |
} |
|
162 |
||
163 |
@Override |
|
164 |
public SctpMultiChannel bindAddress(InetAddress address) |
|
165 |
throws IOException { |
|
166 |
return bindUnbindAddress(address, true); |
|
167 |
} |
|
168 |
||
169 |
@Override |
|
170 |
public SctpMultiChannel unbindAddress(InetAddress address) |
|
171 |
throws IOException { |
|
172 |
return bindUnbindAddress(address, false); |
|
173 |
} |
|
174 |
||
175 |
private SctpMultiChannel bindUnbindAddress(InetAddress address, |
|
176 |
boolean add) |
|
177 |
throws IOException { |
|
178 |
if (address == null) |
|
179 |
throw new IllegalArgumentException(); |
|
180 |
||
181 |
synchronized (receiveLock) { |
|
182 |
synchronized (sendLock) { |
|
183 |
synchronized (stateLock) { |
|
184 |
if (!isOpen()) |
|
185 |
throw new ClosedChannelException(); |
|
186 |
if (!isBound()) |
|
187 |
throw new NotYetBoundException(); |
|
188 |
if (wildcard) |
|
189 |
throw new IllegalStateException( |
|
190 |
"Cannot add or remove addresses from a channel that is bound to the wildcard address"); |
|
191 |
if (address.isAnyLocalAddress()) |
|
192 |
throw new IllegalArgumentException( |
|
193 |
"Cannot add or remove the wildcard address"); |
|
194 |
if (add) { |
|
195 |
for (InetSocketAddress addr : localAddresses) { |
|
196 |
if (addr.getAddress().equals(address)) { |
|
197 |
throw new AlreadyBoundException(); |
|
198 |
} |
|
199 |
} |
|
200 |
} else { /*removing */ |
|
201 |
/* Verify that there is more than one address |
|
202 |
* and that address is already bound */ |
|
203 |
if (localAddresses.size() <= 1) |
|
204 |
throw new IllegalUnbindException("Cannot remove address from a channel with only one address bound"); |
|
205 |
boolean foundAddress = false; |
|
206 |
for (InetSocketAddress addr : localAddresses) { |
|
207 |
if (addr.getAddress().equals(address)) { |
|
208 |
foundAddress = true; |
|
209 |
break; |
|
210 |
} |
|
211 |
} |
|
212 |
if (!foundAddress ) |
|
213 |
throw new IllegalUnbindException("Cannot remove address from a channel that is not bound to that address"); |
|
214 |
} |
|
215 |
||
216 |
SctpNet.bindx(fdVal, new InetAddress[]{address}, port, add); |
|
217 |
||
218 |
/* Update our internal Set to reflect the addition/removal */ |
|
219 |
if (add) |
|
220 |
localAddresses.add(new InetSocketAddress(address, port)); |
|
221 |
else { |
|
222 |
for (InetSocketAddress addr : localAddresses) { |
|
223 |
if (addr.getAddress().equals(address)) { |
|
224 |
localAddresses.remove(addr); |
|
225 |
break; |
|
226 |
} |
|
227 |
} |
|
228 |
} |
|
229 |
} |
|
230 |
} |
|
231 |
} |
|
232 |
return this; |
|
233 |
} |
|
234 |
||
235 |
@Override |
|
236 |
public Set<Association> associations() |
|
237 |
throws ClosedChannelException, NotYetBoundException { |
|
238 |
synchronized (stateLock) { |
|
239 |
if (!isOpen()) |
|
240 |
throw new ClosedChannelException(); |
|
241 |
if (!isBound()) |
|
242 |
throw new NotYetBoundException(); |
|
243 |
||
244 |
return Collections.unmodifiableSet(associationMap.keySet()); |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
private boolean isBound() { |
|
249 |
synchronized (stateLock) { |
|
250 |
return port == -1 ? false : true; |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
private void ensureOpen() throws IOException { |
|
255 |
synchronized (stateLock) { |
|
256 |
if (!isOpen()) |
|
257 |
throw new ClosedChannelException(); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
private void receiverCleanup() throws IOException { |
|
262 |
synchronized (stateLock) { |
|
263 |
receiverThread = 0; |
|
264 |
if (state == ChannelState.KILLPENDING) |
|
265 |
kill(); |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
private void senderCleanup() throws IOException { |
|
270 |
synchronized (stateLock) { |
|
271 |
senderThread = 0; |
|
272 |
if (state == ChannelState.KILLPENDING) |
|
273 |
kill(); |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
@Override |
|
278 |
protected void implConfigureBlocking(boolean block) throws IOException { |
|
279 |
IOUtil.configureBlocking(fd, block); |
|
280 |
} |
|
281 |
||
282 |
@Override |
|
283 |
public void implCloseSelectableChannel() throws IOException { |
|
284 |
synchronized (stateLock) { |
|
285 |
nd.preClose(fd); |
|
286 |
||
287 |
if (receiverThread != 0) |
|
288 |
NativeThread.signal(receiverThread); |
|
289 |
||
290 |
if (senderThread != 0) |
|
291 |
NativeThread.signal(senderThread); |
|
292 |
||
293 |
if (!isRegistered()) |
|
294 |
kill(); |
|
295 |
} |
|
296 |
} |
|
297 |
||
298 |
@Override |
|
299 |
public FileDescriptor getFD() { |
|
300 |
return fd; |
|
301 |
} |
|
302 |
||
303 |
@Override |
|
304 |
public int getFDVal() { |
|
305 |
return fdVal; |
|
306 |
} |
|
307 |
||
308 |
/** |
|
309 |
* Translates native poll revent ops into a ready operation ops |
|
310 |
*/ |
|
311 |
private boolean translateReadyOps(int ops, int initialOps, |
|
312 |
SelectionKeyImpl sk) { |
|
313 |
int intOps = sk.nioInterestOps(); |
|
314 |
int oldOps = sk.nioReadyOps(); |
|
315 |
int newOps = initialOps; |
|
316 |
||
317 |
if ((ops & PollArrayWrapper.POLLNVAL) != 0) { |
|
318 |
/* This should only happen if this channel is pre-closed while a |
|
319 |
* selection operation is in progress |
|
320 |
* ## Throw an error if this channel has not been pre-closed */ |
|
321 |
return false; |
|
322 |
} |
|
323 |
||
324 |
if ((ops & (PollArrayWrapper.POLLERR |
|
325 |
| PollArrayWrapper.POLLHUP)) != 0) { |
|
326 |
newOps = intOps; |
|
327 |
sk.nioReadyOps(newOps); |
|
328 |
return (newOps & ~oldOps) != 0; |
|
329 |
} |
|
330 |
||
331 |
if (((ops & PollArrayWrapper.POLLIN) != 0) && |
|
332 |
((intOps & SelectionKey.OP_READ) != 0)) |
|
333 |
newOps |= SelectionKey.OP_READ; |
|
334 |
||
335 |
if (((ops & PollArrayWrapper.POLLOUT) != 0) && |
|
336 |
((intOps & SelectionKey.OP_WRITE) != 0)) |
|
337 |
newOps |= SelectionKey.OP_WRITE; |
|
338 |
||
339 |
sk.nioReadyOps(newOps); |
|
340 |
return (newOps & ~oldOps) != 0; |
|
341 |
} |
|
342 |
||
343 |
@Override |
|
344 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
345 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
346 |
} |
|
347 |
||
348 |
@Override |
|
349 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
350 |
return translateReadyOps(ops, 0, sk); |
|
351 |
} |
|
352 |
||
353 |
@Override |
|
354 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
355 |
int newOps = 0; |
|
356 |
if ((ops & SelectionKey.OP_READ) != 0) |
|
357 |
newOps |= PollArrayWrapper.POLLIN; |
|
358 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
|
359 |
newOps |= PollArrayWrapper.POLLOUT; |
|
360 |
sk.selector.putEventOps(sk, newOps); |
|
361 |
} |
|
362 |
||
363 |
@Override |
|
364 |
public void kill() throws IOException { |
|
365 |
synchronized (stateLock) { |
|
366 |
if (state == ChannelState.KILLED) |
|
367 |
return; |
|
368 |
if (state == ChannelState.UNINITIALIZED) { |
|
369 |
state = ChannelState.KILLED; |
|
370 |
return; |
|
371 |
} |
|
372 |
assert !isOpen() && !isRegistered(); |
|
373 |
||
374 |
/* Postpone the kill if there is a thread sending or receiving. */ |
|
375 |
if (receiverThread == 0 && senderThread == 0) { |
|
376 |
nd.close(fd); |
|
377 |
state = ChannelState.KILLED; |
|
378 |
} else { |
|
379 |
state = ChannelState.KILLPENDING; |
|
380 |
} |
|
381 |
} |
|
382 |
} |
|
383 |
||
384 |
@Override |
|
385 |
public <T> SctpMultiChannel setOption(SctpSocketOption<T> name, |
|
386 |
T value, |
|
387 |
Association association) |
|
388 |
throws IOException { |
|
389 |
if (name == null) |
|
390 |
throw new NullPointerException(); |
|
391 |
if (!(supportedOptions().contains(name))) |
|
392 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
|
393 |
||
394 |
synchronized (stateLock) { |
|
395 |
if (association != null && (name.equals(SCTP_PRIMARY_ADDR) || |
|
396 |
name.equals(SCTP_SET_PEER_PRIMARY_ADDR))) { |
|
397 |
checkAssociation(association); |
|
398 |
} |
|
399 |
if (!isOpen()) |
|
400 |
throw new ClosedChannelException(); |
|
401 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
402 |
int assocId = association == null ? 0 : association.associationID(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
403 |
SctpNet.setSocketOption(fdVal, name, value, assocId); |
2542 | 404 |
} |
405 |
return this; |
|
406 |
} |
|
407 |
||
408 |
@Override |
|
409 |
@SuppressWarnings("unchecked") |
|
410 |
public <T> T getOption(SctpSocketOption<T> name, Association association) |
|
411 |
throws IOException { |
|
412 |
if (name == null) |
|
413 |
throw new NullPointerException(); |
|
414 |
if (!supportedOptions().contains(name)) |
|
415 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
|
416 |
||
417 |
synchronized (stateLock) { |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
418 |
if (association != null && (name.equals(SCTP_PRIMARY_ADDR) || |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
419 |
name.equals(SCTP_SET_PEER_PRIMARY_ADDR))) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
420 |
checkAssociation(association); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
421 |
} |
2542 | 422 |
if (!isOpen()) |
423 |
throw new ClosedChannelException(); |
|
424 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
425 |
int assocId = association == null ? 0 : association.associationID(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
426 |
return (T)SctpNet.getSocketOption(fdVal, name, assocId); |
2542 | 427 |
} |
428 |
} |
|
429 |
||
430 |
private static class DefaultOptionsHolder { |
|
431 |
static final Set<SctpSocketOption<?>> defaultOptions = defaultOptions(); |
|
432 |
||
433 |
private static Set<SctpSocketOption<?>> defaultOptions() { |
|
434 |
HashSet<SctpSocketOption<?>> set = new HashSet<SctpSocketOption<?>>(10); |
|
435 |
set.add(SCTP_DISABLE_FRAGMENTS); |
|
436 |
set.add(SCTP_EXPLICIT_COMPLETE); |
|
437 |
set.add(SCTP_FRAGMENT_INTERLEAVE); |
|
438 |
set.add(SCTP_INIT_MAXSTREAMS); |
|
439 |
set.add(SCTP_NODELAY); |
|
440 |
set.add(SCTP_PRIMARY_ADDR); |
|
441 |
set.add(SCTP_SET_PEER_PRIMARY_ADDR); |
|
442 |
set.add(SO_SNDBUF); |
|
443 |
set.add(SO_RCVBUF); |
|
444 |
set.add(SO_LINGER); |
|
445 |
return Collections.unmodifiableSet(set); |
|
446 |
} |
|
447 |
} |
|
448 |
||
449 |
@Override |
|
450 |
public final Set<SctpSocketOption<?>> supportedOptions() { |
|
451 |
return DefaultOptionsHolder.defaultOptions; |
|
452 |
} |
|
453 |
||
454 |
@Override |
|
455 |
public <T> MessageInfo receive(ByteBuffer buffer, |
|
456 |
T attachment, |
|
457 |
NotificationHandler<T> handler) |
|
458 |
throws IOException { |
|
459 |
if (buffer == null) |
|
460 |
throw new IllegalArgumentException("buffer cannot be null"); |
|
461 |
||
462 |
if (buffer.isReadOnly()) |
|
463 |
throw new IllegalArgumentException("Read-only buffer"); |
|
464 |
||
465 |
if (receiveInvoked.get()) |
|
466 |
throw new IllegalReceiveException( |
|
467 |
"cannot invoke receive from handler"); |
|
468 |
receiveInvoked.set(Boolean.TRUE); |
|
469 |
||
470 |
try { |
|
471 |
SctpResultContainer resultContainer = new SctpResultContainer(); |
|
472 |
do { |
|
473 |
resultContainer.clear(); |
|
474 |
synchronized (receiveLock) { |
|
475 |
ensureOpen(); |
|
476 |
if (!isBound()) |
|
477 |
throw new NotYetBoundException(); |
|
478 |
||
479 |
int n = 0; |
|
480 |
try { |
|
481 |
begin(); |
|
482 |
||
483 |
synchronized (stateLock) { |
|
484 |
if(!isOpen()) |
|
485 |
return null; |
|
486 |
receiverThread = NativeThread.current(); |
|
487 |
} |
|
488 |
||
489 |
do { |
|
490 |
n = receive(fdVal, buffer, resultContainer); |
|
491 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
492 |
||
493 |
} finally { |
|
494 |
receiverCleanup(); |
|
495 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
496 |
assert IOStatus.check(n); |
|
497 |
} |
|
498 |
||
499 |
if (!resultContainer.isNotification()) { |
|
500 |
/* message or nothing */ |
|
501 |
if (resultContainer.hasSomething()) { |
|
502 |
/* Set the association before returning */ |
|
503 |
SctpMessageInfoImpl info = |
|
504 |
resultContainer.getMessageInfo(); |
|
505 |
info.setAssociation(lookupAssociation(info. |
|
506 |
associationID())); |
|
507 |
SecurityManager sm = System.getSecurityManager(); |
|
508 |
if (sm != null) { |
|
509 |
InetSocketAddress isa = (InetSocketAddress)info.address(); |
|
510 |
if (!addressMap.containsKey(isa)) { |
|
511 |
/* must be a new association */ |
|
512 |
try { |
|
513 |
sm.checkAccept(isa.getAddress().getHostAddress(), |
|
514 |
isa.getPort()); |
|
515 |
} catch (SecurityException se) { |
|
516 |
buffer.clear(); |
|
517 |
throw se; |
|
518 |
} |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
assert info.association() != null; |
|
523 |
return info; |
|
524 |
} else { |
|
525 |
/* Non-blocking may return null if nothing available*/ |
|
526 |
return null; |
|
527 |
} |
|
528 |
} else { /* notification */ |
|
529 |
synchronized (stateLock) { |
|
530 |
handleNotificationInternal( |
|
531 |
resultContainer); |
|
532 |
} |
|
533 |
} |
|
534 |
} /* receiveLock */ |
|
535 |
} while (handler == null ? true : |
|
536 |
(invokeNotificationHandler(resultContainer, handler, attachment) |
|
537 |
== HandlerResult.CONTINUE)); |
|
538 |
} finally { |
|
539 |
receiveInvoked.set(Boolean.FALSE); |
|
540 |
} |
|
541 |
||
542 |
return null; |
|
543 |
} |
|
544 |
||
545 |
private int receive(int fd, |
|
546 |
ByteBuffer dst, |
|
547 |
SctpResultContainer resultContainer) |
|
548 |
throws IOException { |
|
549 |
int pos = dst.position(); |
|
550 |
int lim = dst.limit(); |
|
551 |
assert (pos <= lim); |
|
552 |
int rem = (pos <= lim ? lim - pos : 0); |
|
553 |
if (dst instanceof DirectBuffer && rem > 0) |
|
554 |
return receiveIntoNativeBuffer(fd, resultContainer, dst, rem, pos); |
|
555 |
||
556 |
/* Substitute a native buffer. */ |
|
557 |
int newSize = Math.max(rem, 1); |
|
558 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize); |
|
559 |
try { |
|
560 |
int n = receiveIntoNativeBuffer(fd, resultContainer, bb, newSize, 0); |
|
561 |
bb.flip(); |
|
562 |
if (n > 0 && rem > 0) |
|
563 |
dst.put(bb); |
|
564 |
return n; |
|
565 |
} finally { |
|
566 |
Util.releaseTemporaryDirectBuffer(bb); |
|
567 |
} |
|
568 |
} |
|
569 |
||
570 |
private int receiveIntoNativeBuffer(int fd, |
|
571 |
SctpResultContainer resultContainer, |
|
572 |
ByteBuffer bb, |
|
573 |
int rem, |
|
574 |
int pos) |
|
575 |
throws IOException { |
|
576 |
int n = receive0(fd, resultContainer, ((DirectBuffer)bb).address() + pos, rem); |
|
577 |
if (n > 0) |
|
578 |
bb.position(pos + n); |
|
579 |
return n; |
|
580 |
} |
|
581 |
||
582 |
private InternalNotificationHandler internalNotificationHandler = |
|
583 |
new InternalNotificationHandler(); |
|
584 |
||
585 |
private void handleNotificationInternal(SctpResultContainer resultContainer) |
|
586 |
{ |
|
587 |
invokeNotificationHandler(resultContainer, |
|
588 |
internalNotificationHandler, null); |
|
589 |
} |
|
590 |
||
591 |
private class InternalNotificationHandler<T> |
|
592 |
extends AbstractNotificationHandler<T> |
|
593 |
{ |
|
594 |
@Override |
|
595 |
public HandlerResult handleNotification( |
|
596 |
AssociationChangeNotification not, T unused) { |
|
597 |
SctpAssocChange sac = (SctpAssocChange) not; |
|
598 |
||
599 |
/* Update map to reflect change in association */ |
|
600 |
switch (not.event()) { |
|
601 |
case COMM_UP : |
|
602 |
Association newAssociation = new SctpAssociationImpl |
|
603 |
(sac.assocId(), sac.maxInStreams(), sac.maxOutStreams()); |
|
604 |
addAssociation(newAssociation); |
|
605 |
break; |
|
606 |
case SHUTDOWN : |
|
607 |
case COMM_LOST : |
|
608 |
//case RESTART: ??? |
|
609 |
/* mark association for removal after user handler invoked*/ |
|
610 |
associationToRemove.set(lookupAssociation(sac.assocId())); |
|
611 |
} |
|
612 |
return HandlerResult.CONTINUE; |
|
613 |
} |
|
614 |
} |
|
615 |
||
616 |
private <T> HandlerResult invokeNotificationHandler( |
|
617 |
SctpResultContainer resultContainer, |
|
618 |
NotificationHandler<T> handler, |
|
619 |
T attachment) { |
|
620 |
HandlerResult result; |
|
621 |
SctpNotification notification = resultContainer.notification(); |
|
622 |
notification.setAssociation(lookupAssociation(notification.assocId())); |
|
623 |
||
624 |
if (!(handler instanceof AbstractNotificationHandler)) { |
|
625 |
result = handler.handleNotification(notification, attachment); |
|
626 |
} else { /* AbstractNotificationHandler */ |
|
627 |
AbstractNotificationHandler absHandler = |
|
628 |
(AbstractNotificationHandler)handler; |
|
629 |
switch(resultContainer.type()) { |
|
630 |
case ASSOCIATION_CHANGED : |
|
631 |
result = absHandler.handleNotification( |
|
632 |
resultContainer.getAssociationChanged(), attachment); |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
633 |
break; |
2542 | 634 |
case PEER_ADDRESS_CHANGED : |
635 |
result = absHandler.handleNotification( |
|
636 |
resultContainer.getPeerAddressChanged(), attachment); |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
637 |
break; |
2542 | 638 |
case SEND_FAILED : |
639 |
result = absHandler.handleNotification( |
|
640 |
resultContainer.getSendFailed(), attachment); |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
641 |
break; |
2542 | 642 |
case SHUTDOWN : |
643 |
result = absHandler.handleNotification( |
|
644 |
resultContainer.getShutdown(), attachment); |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
645 |
break; |
2542 | 646 |
default : |
647 |
/* implementation specific handlers */ |
|
648 |
result = absHandler.handleNotification( |
|
649 |
resultContainer.notification(), attachment); |
|
650 |
} |
|
651 |
} |
|
652 |
||
653 |
if (!(handler instanceof InternalNotificationHandler)) { |
|
654 |
/* Only remove associations after user handler |
|
655 |
* has finished with them */ |
|
656 |
Association assoc = associationToRemove.get(); |
|
657 |
if (assoc != null) { |
|
658 |
removeAssociation(assoc); |
|
659 |
associationToRemove.set(null); |
|
660 |
} |
|
661 |
||
662 |
} |
|
663 |
||
664 |
return result; |
|
665 |
} |
|
666 |
||
667 |
private Association lookupAssociation(int assocId) { |
|
668 |
/* Lookup the association in our internal map */ |
|
669 |
synchronized (stateLock) { |
|
670 |
Set<Association> assocs = associationMap.keySet(); |
|
671 |
for (Association a : assocs) { |
|
672 |
if (a.associationID() == assocId) { |
|
673 |
return a; |
|
674 |
} |
|
675 |
} |
|
676 |
} |
|
677 |
return null; |
|
678 |
} |
|
679 |
||
680 |
private void addAssociation(Association association) { |
|
681 |
synchronized (stateLock) { |
|
682 |
int assocId = association.associationID(); |
|
683 |
Set<SocketAddress> addresses = null; |
|
684 |
||
685 |
try { |
|
686 |
addresses = SctpNet.getRemoteAddresses(fdVal, assocId); |
|
687 |
} catch (IOException unused) { |
|
688 |
/* OK, determining connected addresses may not be possible |
|
689 |
* shutdown, connection lost, etc */ |
|
690 |
} |
|
691 |
||
692 |
associationMap.put(association, addresses); |
|
693 |
if (addresses != null) { |
|
694 |
for (SocketAddress addr : addresses) |
|
695 |
addressMap.put(addr, association); |
|
696 |
} |
|
697 |
} |
|
698 |
} |
|
699 |
||
700 |
private void removeAssociation(Association association) { |
|
701 |
synchronized (stateLock) { |
|
702 |
int assocId = association.associationID(); |
|
703 |
Set<SocketAddress> addresses = null; |
|
704 |
||
705 |
try { |
|
706 |
addresses = SctpNet.getRemoteAddresses(fdVal, assocId); |
|
707 |
} catch (IOException unused) { |
|
708 |
/* OK, determining connected addresses may not be possible |
|
709 |
* shutdown, connection lost, etc */ |
|
710 |
} |
|
711 |
||
712 |
Set<Association> assocs = associationMap.keySet(); |
|
713 |
for (Association a : assocs) { |
|
714 |
if (a.associationID() == assocId) { |
|
715 |
associationMap.remove(a); |
|
716 |
break; |
|
717 |
} |
|
718 |
} |
|
719 |
if (addresses != null) { |
|
720 |
for (SocketAddress addr : addresses) |
|
721 |
addressMap.remove(addr); |
|
722 |
} else { |
|
723 |
/* We cannot determine the connected addresses */ |
|
724 |
Set<java.util.Map.Entry<SocketAddress, Association>> addrAssocs = |
|
725 |
addressMap.entrySet(); |
|
726 |
for (java.util.Map.Entry<SocketAddress, Association> entry : addrAssocs) { |
|
727 |
if (entry.getValue().equals(association)) { |
|
728 |
addressMap.remove(entry.getKey()); |
|
729 |
} |
|
730 |
} |
|
731 |
} |
|
732 |
} |
|
733 |
} |
|
734 |
||
735 |
/** |
|
736 |
* @throws IllegalArgumentException |
|
737 |
* If the given association is not controlled by this channel |
|
738 |
* |
|
739 |
* @return {@code true} if, and only if, the given association is one |
|
740 |
* of the current associations controlled by this channel |
|
741 |
*/ |
|
742 |
private boolean checkAssociation(Association messageAssoc) { |
|
743 |
synchronized (stateLock) { |
|
744 |
for (Association association : associationMap.keySet()) { |
|
745 |
if (messageAssoc.equals(association)) { |
|
746 |
return true; |
|
747 |
} |
|
748 |
} |
|
749 |
} |
|
750 |
throw new IllegalArgumentException( |
|
751 |
"Given Association is not controlled by this channel"); |
|
752 |
} |
|
753 |
||
754 |
private void checkStreamNumber(Association assoc, int streamNumber) { |
|
755 |
synchronized (stateLock) { |
|
756 |
if (streamNumber < 0 || streamNumber >= assoc.maxOutboundStreams()) |
|
757 |
throw new InvalidStreamException(); |
|
758 |
} |
|
759 |
} |
|
760 |
||
761 |
/* TODO: Add support for ttl and isComplete to both 121 12M |
|
762 |
* SCTP_EOR not yet supported on reference platforms |
|
763 |
* TTL support limited... |
|
764 |
*/ |
|
765 |
@Override |
|
766 |
public int send(ByteBuffer buffer, MessageInfo messageInfo) |
|
767 |
throws IOException { |
|
768 |
if (buffer == null) |
|
769 |
throw new IllegalArgumentException("buffer cannot be null"); |
|
770 |
||
771 |
if (messageInfo == null) |
|
772 |
throw new IllegalArgumentException("messageInfo cannot be null"); |
|
773 |
||
774 |
synchronized (sendLock) { |
|
775 |
ensureOpen(); |
|
776 |
||
777 |
if (!isBound()) |
|
778 |
bind(null, 0); |
|
779 |
||
780 |
int n = 0; |
|
781 |
try { |
|
782 |
int assocId = -1; |
|
783 |
SocketAddress address = null; |
|
784 |
begin(); |
|
785 |
||
786 |
synchronized (stateLock) { |
|
787 |
if(!isOpen()) |
|
788 |
return 0; |
|
789 |
senderThread = NativeThread.current(); |
|
790 |
||
791 |
/* Determine what address or association to send to */ |
|
792 |
Association assoc = messageInfo.association(); |
|
793 |
InetSocketAddress addr = (InetSocketAddress)messageInfo.address(); |
|
794 |
if (assoc != null) { |
|
795 |
checkAssociation(assoc); |
|
796 |
checkStreamNumber(assoc, messageInfo.streamNumber()); |
|
797 |
assocId = assoc.associationID(); |
|
798 |
/* have we also got a preferred address */ |
|
799 |
if (addr != null) { |
|
800 |
if (!assoc.equals(addressMap.get(addr))) |
|
801 |
throw new IllegalArgumentException("given preferred address is not part of this association"); |
|
802 |
address = addr; |
|
803 |
} |
|
804 |
} else if (addr != null) { |
|
805 |
address = addr; |
|
806 |
Association association = addressMap.get(addr); |
|
807 |
if (association != null) { |
|
808 |
checkStreamNumber(association, messageInfo.streamNumber()); |
|
809 |
assocId = association.associationID(); |
|
810 |
||
811 |
} else { /* must be new association */ |
|
812 |
SecurityManager sm = System.getSecurityManager(); |
|
813 |
if (sm != null) |
|
814 |
sm.checkConnect(addr.getAddress().getHostAddress(), |
|
815 |
addr.getPort()); |
|
816 |
} |
|
817 |
} else { |
|
818 |
throw new AssertionError( |
|
819 |
"Both association and address cannot be null"); |
|
820 |
} |
|
821 |
} |
|
822 |
||
823 |
do { |
|
824 |
n = send(fdVal, buffer, assocId, address, messageInfo); |
|
825 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
826 |
||
827 |
return IOStatus.normalize(n); |
|
828 |
} finally { |
|
829 |
senderCleanup(); |
|
830 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
831 |
assert IOStatus.check(n); |
|
832 |
} |
|
833 |
} |
|
834 |
} |
|
835 |
||
836 |
private int send(int fd, |
|
837 |
ByteBuffer src, |
|
838 |
int assocId, |
|
839 |
SocketAddress target, |
|
840 |
MessageInfo messageInfo) |
|
841 |
throws IOException { |
|
842 |
int streamNumber = messageInfo.streamNumber(); |
|
843 |
boolean unordered = messageInfo.isUnordered(); |
|
844 |
int ppid = messageInfo.payloadProtocolID(); |
|
845 |
int pos = src.position(); |
|
846 |
int lim = src.limit(); |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
847 |
assert (pos <= lim && streamNumber >= 0); |
2542 | 848 |
int rem = (pos <= lim ? lim - pos : 0); |
849 |
||
850 |
if (src instanceof DirectBuffer) |
|
851 |
return sendFromNativeBuffer(fd, src, rem, pos, target, assocId, |
|
852 |
streamNumber, unordered, ppid); |
|
853 |
||
854 |
/* Substitute a native buffer */ |
|
855 |
ByteBuffer bb = Util.getTemporaryDirectBuffer(rem); |
|
856 |
try { |
|
857 |
bb.put(src); |
|
858 |
bb.flip(); |
|
859 |
/* Do not update src until we see how many bytes were written */ |
|
860 |
src.position(pos); |
|
861 |
||
862 |
int n = sendFromNativeBuffer(fd, bb, rem, pos, target, assocId, |
|
863 |
streamNumber, unordered, ppid); |
|
864 |
if (n > 0) { |
|
865 |
/* now update src */ |
|
866 |
src.position(pos + n); |
|
867 |
} |
|
868 |
return n; |
|
869 |
} finally { |
|
870 |
Util.releaseTemporaryDirectBuffer(bb); |
|
871 |
} |
|
872 |
} |
|
873 |
||
874 |
private int sendFromNativeBuffer(int fd, |
|
875 |
ByteBuffer bb, |
|
876 |
int rem, |
|
877 |
int pos, |
|
878 |
SocketAddress target, |
|
879 |
int assocId, |
|
880 |
int streamNumber, |
|
881 |
boolean unordered, |
|
882 |
int ppid) |
|
883 |
throws IOException { |
|
884 |
int written = send0(fd, ((DirectBuffer)bb).address() + pos, |
|
885 |
rem, target, assocId, streamNumber, unordered, ppid); |
|
886 |
if (written > 0) |
|
887 |
bb.position(pos + written); |
|
888 |
return written; |
|
889 |
} |
|
890 |
||
891 |
@Override |
|
892 |
public SctpMultiChannel shutdown(Association association) |
|
893 |
throws IOException { |
|
894 |
synchronized (stateLock) { |
|
895 |
checkAssociation(association); |
|
896 |
if (!isOpen()) |
|
897 |
throw new ClosedChannelException(); |
|
898 |
||
899 |
SctpNet.shutdown(fdVal, association.associationID()); |
|
900 |
} |
|
901 |
return this; |
|
902 |
} |
|
903 |
||
904 |
@Override |
|
905 |
public Set<SocketAddress> getAllLocalAddresses() |
|
906 |
throws IOException { |
|
907 |
synchronized (stateLock) { |
|
908 |
if (!isOpen()) |
|
909 |
throw new ClosedChannelException(); |
|
910 |
if (!isBound()) |
|
911 |
return Collections.EMPTY_SET; |
|
912 |
||
913 |
return SctpNet.getLocalAddresses(fdVal); |
|
914 |
} |
|
915 |
} |
|
916 |
||
917 |
@Override |
|
918 |
public Set<SocketAddress> getRemoteAddresses(Association association) |
|
919 |
throws IOException { |
|
920 |
synchronized (stateLock) { |
|
921 |
checkAssociation(association); |
|
922 |
if (!isOpen()) |
|
923 |
throw new ClosedChannelException(); |
|
924 |
||
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
925 |
try { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
926 |
return SctpNet.getRemoteAddresses(fdVal, association.associationID()); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
927 |
} catch (SocketException se) { |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
928 |
/* a valid association should always have remote addresses */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
929 |
Set<SocketAddress> addrs = associationMap.get(association); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
930 |
return addrs != null ? addrs : Collections.EMPTY_SET; |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
931 |
} |
2542 | 932 |
} |
933 |
} |
|
934 |
||
935 |
@Override |
|
936 |
public SctpChannel branch(Association association) |
|
937 |
throws IOException { |
|
938 |
synchronized (stateLock) { |
|
3072
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
939 |
checkAssociation(association); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
940 |
if (!isOpen()) |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
941 |
throw new ClosedChannelException(); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
942 |
|
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
943 |
FileDescriptor bFd = SctpNet.branch(fdVal, |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
944 |
association.associationID()); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
945 |
/* successfully branched, we can now remove it from assoc list */ |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
946 |
removeAssociation(association); |
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
947 |
|
a801b122142f
6855335: Several changes in the SCTP implementation.
chegar
parents:
2542
diff
changeset
|
948 |
return new SctpChannelImpl(provider(), bFd, association); |
2542 | 949 |
} |
950 |
} |
|
951 |
||
952 |
/* Use common native implementation shared between |
|
953 |
* one-to-one and one-to-many */ |
|
954 |
private static int receive0(int fd, |
|
955 |
SctpResultContainer resultContainer, |
|
956 |
long address, |
|
957 |
int length) |
|
958 |
throws IOException{ |
|
959 |
return SctpChannelImpl.receive0(fd, resultContainer, address, |
|
960 |
length); |
|
961 |
} |
|
962 |
||
963 |
private static int send0(int fd, |
|
964 |
long address, |
|
965 |
int length, |
|
966 |
SocketAddress target, |
|
967 |
int assocId, |
|
968 |
int streamNumber, |
|
969 |
boolean unordered, |
|
970 |
int ppid) |
|
971 |
throws IOException { |
|
972 |
return SctpChannelImpl.send0(fd, address, length, target, assocId, |
|
973 |
streamNumber, unordered, ppid); |
|
974 |
} |
|
975 |
||
976 |
static { |
|
977 |
Util.load(); /* loads nio & net native libraries */ |
|
978 |
java.security.AccessController.doPrivileged( |
|
979 |
new sun.security.action.LoadLibraryAction("sctp")); |
|
980 |
nd = new SctpSocketDispatcher(); |
|
981 |
} |
|
982 |
} |