View Javadoc
1   /*
2    * Copyright (c) 2009, 2013, 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.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle 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 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.
24   */
25  package com.sun.nio.sctp;
26  
27  import java.net.SocketAddress;
28  import java.net.InetAddress;
29  import java.io.IOException;
30  import java.util.Set;
31  import java.nio.ByteBuffer;
32  import java.nio.channels.spi.AbstractSelectableChannel;
33  import java.nio.channels.spi.SelectorProvider;
34  import java.nio.channels.ClosedChannelException;
35  import java.nio.channels.SelectionKey;
36  
37  /**
38   * A selectable channel for message-oriented connected SCTP sockets.
39   *
40   * <P> An SCTP channel can only control one SCTP association.
41   * An {@code SCTPChannel} is created by invoking one of the
42   * {@link #open open} methods of this class. A newly-created channel is open but
43   * not yet connected, that is, there is no association setup with a remote peer.
44   * An attempt to invoke an I/O operation upon an unconnected
45   * channel will cause a {@link java.nio.channels.NotYetConnectedException} to be
46   * thrown. An association can be setup by connecting the channel using one of
47   * its {@link #connect connect} methods. Once connected, the channel remains
48   * connected until it is closed. Whether or not a channel is connected may be
49   * determined by invoking {@link #getRemoteAddresses getRemoteAddresses}.
50   *
51   * <p> SCTP channels support <i>non-blocking connection:</i>&nbsp;A
52   * channel may be created and the process of establishing the link to
53   * the remote socket may be initiated via the {@link #connect connect} method
54   * for later completion by the {@link #finishConnect finishConnect} method.
55   * Whether or not a connection operation is in progress may be determined by
56   * invoking the {@link #isConnectionPending isConnectionPending} method.
57   *
58   * <p> Socket options are configured using the
59   * {@link #setOption(SctpSocketOption,Object) setOption} method. An SCTP
60   * channel support the following options:
61   * <blockquote>
62   * <table border summary="Socket options">
63   *   <tr>
64   *     <th>Option Name</th>
65   *     <th>Description</th>
66   *   </tr>
67   *   <tr>
68   *     <td> {@link SctpStandardSocketOptions#SCTP_DISABLE_FRAGMENTS
69   *                                          SCTP_DISABLE_FRAGMENTS} </td>
70   *     <td> Enables or disables message fragmentation </td>
71   *   </tr>
72   *   <tr>
73   *     <td> {@link SctpStandardSocketOptions#SCTP_EXPLICIT_COMPLETE
74   *                                          SCTP_EXPLICIT_COMPLETE} </td>
75   *     <td> Enables or disables explicit message completion </td>
76   *   </tr>
77   *    <tr>
78   *     <td> {@link SctpStandardSocketOptions#SCTP_FRAGMENT_INTERLEAVE
79   *                                          SCTP_FRAGMENT_INTERLEAVE} </td>
80   *     <td> Controls how the presentation of messages occur for the message
81   *          receiver </td>
82   *   </tr>
83   *   <tr>
84   *     <td> {@link SctpStandardSocketOptions#SCTP_INIT_MAXSTREAMS
85   *                                          SCTP_INIT_MAXSTREAMS} </td>
86   *     <td> The maximum number of streams requested by the local endpoint during
87   *          association initialization </td>
88   *   </tr>
89   *   <tr>
90   *     <td> {@link SctpStandardSocketOptions#SCTP_NODELAY SCTP_NODELAY} </td>
91   *     <td> Enables or disable a Nagle-like algorithm </td>
92   *   </tr>
93   *   <tr>
94   *     <td> {@link SctpStandardSocketOptions#SCTP_PRIMARY_ADDR
95   *                                          SCTP_PRIMARY_ADDR} </td>
96   *     <td> Requests that the local SCTP stack use the given peer address as the
97   *          association primary </td>
98   *   </tr>
99   *   <tr>
100  *     <td> {@link SctpStandardSocketOptions#SCTP_SET_PEER_PRIMARY_ADDR
101  *                                          SCTP_SET_PEER_PRIMARY_ADDR} </td>
102  *     <td> Requests that the peer mark the enclosed address as the association
103  *          primary </td>
104  *   </tr>
105  *   <tr>
106  *     <td> {@link SctpStandardSocketOptions#SO_SNDBUF
107  *                                          SO_SNDBUF} </td>
108  *     <td> The size of the socket send buffer </td>
109  *   </tr>
110  *   <tr>
111  *     <td> {@link SctpStandardSocketOptions#SO_RCVBUF
112  *                                          SO_RCVBUF} </td>
113  *     <td> The size of the socket receive buffer </td>
114  *   </tr>
115  *   <tr>
116  *     <td> {@link SctpStandardSocketOptions#SO_LINGER
117  *                                          SO_LINGER} </td>
118  *     <td> Linger on close if data is present (when configured in blocking mode
119  *          only) </td>
120  *   </tr>
121  * </table>
122  * </blockquote>
123  * Additional (implementation specific) options may also be supported. The list
124  * of options supported is obtained by invoking the {@link #supportedOptions()
125  * supportedOptions}  method.
126  *
127  * <p> SCTP channels are safe for use by multiple concurrent threads.
128  * They support concurrent reading and writing, though at most one thread may be
129  * reading and at most one thread may be writing at any given time. The
130  * {@link #connect connect} and {@link #finishConnect
131  * finishConnect} methods are mutually synchronized against each other, and
132  * an attempt to initiate a send or receive operation while an invocation of one
133  * of these methods is in progress will block until that invocation is complete.
134  *
135  * @since 1.7
136  */
137 @jdk.Exported
138 public abstract class SctpChannel
139     extends AbstractSelectableChannel
140 {
141     /**
142      * Initializes a new instance of this class.
143      *
144      * @param  provider
145      *         The selector provider for this channel
146      */
147     protected SctpChannel(SelectorProvider provider) {
148         super(provider);
149     }
150 
151     /**
152      * Opens an SCTP channel.
153      *
154      * <P> The new channel is unbound and unconnected.
155      *
156      * @return  A new SCTP channel
157      *
158      * @throws  UnsupportedOperationException
159      *          If the SCTP protocol is not supported
160      *
161      * @throws  IOException
162      *          If an I/O error occurs
163      */
164     public static SctpChannel open() throws
165         IOException {
166         return new sun.nio.ch.sctp.SctpChannelImpl((SelectorProvider)null);
167     }
168 
169     /**
170      * Opens an SCTP channel and connects it to a remote address.
171      *
172      * <P> This is a convenience method and is equivalent to evaluating the
173      * following expression:
174      * <blockquote><pre>
175      * open().connect(remote, maxOutStreams, maxInStreams);
176      * </pre></blockquote>
177      *
178      * @param  remote
179      *         The remote address to which the new channel is to be connected
180      *
181      * @param  maxOutStreams
182      *         The number of streams that the application wishes to be able
183      *         to send to. Must be non negative and no larger than {@code 65536}.
184      *         {@code 0} to use the endpoints default value.
185      *
186      * @param  maxInStreams
187      *         The maximum number of inbound streams the application is prepared
188      *         to support. Must be non negative and no larger than {@code 65536}.
189      *         {@code 0} to use the endpoints default value.
190      *
191      * @return  A new SCTP channel connected to the given address
192      *
193      * @throws  java.nio.channels.AsynchronousCloseException
194      *          If another thread closes this channel
195      *          while the connect operation is in progress
196      *
197      * @throws  java.nio.channels.ClosedByInterruptException
198      *          If another thread interrupts the current thread
199      *          while the connect operation is in progress, thereby
200      *          closing the channel and setting the current thread's
201      *          interrupt status
202      *
203      * @throws  java.nio.channels.UnresolvedAddressException
204      *          If the given remote address is not fully resolved
205      *
206      * @throws  java.nio.channels.UnsupportedAddressTypeException
207      *          If the type of the given remote address is not supported
208      *
209      * @throws  SecurityException
210      *          If a security manager has been installed
211      *          and it does not permit access to the given remote peer
212      *
213      * @throws  UnsupportedOperationException
214      *          If the SCTP protocol is not supported
215      *
216      * @throws  IOException
217      *          If some other I/O error occurs
218      */
219     public static SctpChannel open(SocketAddress remote, int maxOutStreams,
220                    int maxInStreams) throws IOException {
221         SctpChannel ssc = SctpChannel.open();
222         ssc.connect(remote, maxOutStreams, maxInStreams);
223         return ssc;
224     }
225 
226     /**
227      * Returns the association on this channel's socket.
228      *
229      * @return  the association, or {@code null} if the channel's socket is not
230      *          connected.
231      *
232      * @throws  ClosedChannelException
233      *          If the channel is closed
234      *
235      * @throws  IOException
236      *          If some other I/O error occurs
237      */
238     public abstract Association association() throws IOException;
239 
240     /**
241      * Binds the channel's socket to a local address.
242      *
243      * <P> This method is used to establish a relationship between the socket
244      * and the local addresses. Once a relationship is established then
245      * the socket remains bound until the channel is closed. This relationship
246      * may not necesssarily be with the address {@code local} as it may be removed
247      * by {@link #unbindAddress unbindAddress}, but there will always be at least
248      * one local address bound to the channel's socket once an invocation of
249      * this method successfully completes.
250      *
251      * <P> Once the channel's socket has been successfully bound to a specific
252      * address, that is not automatically assigned, more addresses
253      * may be bound to it using {@link #bindAddress bindAddress}, or removed
254      * using {@link #unbindAddress unbindAddress}.
255      *
256      * @param  local
257      *         The local address to bind the socket, or {@code null} to
258      *         bind the socket to an automatically assigned socket address
259      *
260      * @return  This channel
261      *
262      * @throws  java.nio.channels.AlreadyConnectedException
263      *          If this channel is already connected
264      *
265      * @throws  java.nio.channels.ClosedChannelException
266      *          If this channel is closed
267      *
268      * @throws  java.nio.channels.ConnectionPendingException
269      *          If a non-blocking connection operation is already in progress on this channel
270      *
271      * @throws  java.nio.channels.AlreadyBoundException
272      *          If this channel is already bound
273      *
274      * @throws  java.nio.channels.UnsupportedAddressTypeException
275      *          If the type of the given address is not supported
276      *
277      * @throws  IOException
278      *          If some other I/O error occurs
279      *
280      * @throws  SecurityException
281      *          If a security manager has been installed and its
282      *          {@link SecurityManager#checkListen checkListen} method denies
283      *          the operation
284      */
285     public abstract SctpChannel bind(SocketAddress local)
286         throws IOException;
287 
288     /**
289      * Adds the given address to the bound addresses for the channel's
290      * socket.
291      *
292      * <P> The given address must not be the {@link
293      * java.net.InetAddress#isAnyLocalAddress wildcard} address.
294      * The channel must be first bound using {@link #bind bind} before
295      * invoking this method, otherwise {@link
296      * java.nio.channels.NotYetBoundException} is thrown. The {@link #bind bind}
297      * method takes a {@code SocketAddress} as its argument which typically
298      * contains a port number as well as an address. Addresses subquently bound
299      * using this method are simply addresses as the SCTP port number remains
300      * the same for the lifetime of the channel.
301      *
302      * <P> Adding addresses to a connected association is optional functionality.
303      * If the endpoint supports dynamic address reconfiguration then it may
304      * send the appropriate message to the peer to change the peers address
305      * lists.
306      *
307      * @param  address
308      *         The address to add to the bound addresses for the socket
309      *
310      * @return  This channel
311      *
312      * @throws  java.nio.channels.ClosedChannelException
313      *          If this channel is closed
314      *
315      * @throws  java.nio.channels.ConnectionPendingException
316      *          If a non-blocking connection operation is already in progress on
317      *          this channel
318      *
319      * @throws  java.nio.channels.NotYetBoundException
320      *          If this channel is not yet bound
321      *
322      * @throws  java.nio.channels.AlreadyBoundException
323      *          If this channel is already bound to the given address
324      *
325      * @throws  IllegalArgumentException
326      *          If address is {@code null} or the {@link
327      *          java.net.InetAddress#isAnyLocalAddress wildcard} address
328      *
329      * @throws  IOException
330      *          If some other I/O error occurs
331      */
332     public abstract SctpChannel bindAddress(InetAddress address)
333          throws IOException;
334 
335     /**
336      * Removes the given address from the bound addresses for the channel's
337      * socket.
338      *
339      * <P> The given address must not be the {@link
340      * java.net.InetAddress#isAnyLocalAddress wildcard} address.
341      * The channel must be first bound using {@link #bind bind} before
342      * invoking this method, otherwise {@link java.nio.channels.NotYetBoundException}
343      * is thrown. If this method is invoked on a channel that does not have
344      * {@code address} as one of its bound addresses or that has only one
345      * local address bound to it, then this method throws
346      * {@link IllegalUnbindException}.
347      * The initial address that the channel's socket is bound to using {@link
348      * #bind bind} may be removed from the bound addresses for the channel's socket.
349      *
350      * <P> Removing addresses from a connected association is optional
351      * functionality. If the endpoint supports dynamic address reconfiguration
352      * then it may send the appropriate message to the peer to change the peers
353      * address lists.
354      *
355      * @param  address
356      *         The address to remove from the bound addresses for the socket
357      *
358      * @return  This channel
359      *
360      * @throws  java.nio.channels.ClosedChannelException
361      *          If this channel is closed
362      *
363      * @throws  java.nio.channels.ConnectionPendingException
364      *          If a non-blocking connection operation is already in progress on
365      *          this channel
366      *
367      * @throws  java.nio.channels.NotYetBoundException
368      *          If this channel is not yet bound
369      *
370      * @throws  IllegalArgumentException
371      *          If address is {@code null} or the {@link
372      *          java.net.InetAddress#isAnyLocalAddress wildcard} address
373      *
374      * @throws  IllegalUnbindException
375      *          If {@code address} is not bound to the channel's socket. or
376      *          the channel has only one address bound to it
377      *
378      * @throws  IOException
379      *          If some other I/O error occurs
380      */
381     public abstract SctpChannel unbindAddress(InetAddress address)
382          throws IOException;
383 
384     /**
385      * Connects this channel's socket.
386      *
387      * <P> If this channel is in non-blocking mode then an invocation of this
388      * method initiates a non-blocking connection operation.  If the connection
389      * is established immediately, as can happen with a local connection, then
390      * this method returns {@code true}.  Otherwise this method returns
391      * {@code false} and the connection operation must later be completed by
392      * invoking the {@link #finishConnect finishConnect} method.
393      *
394      * <P> If this channel is in blocking mode then an invocation of this
395      * method will block until the connection is established or an I/O error
396      * occurs.
397      *
398      * <P> If a security manager has been installed then this method verifies
399      * that its {@link java.lang.SecurityManager#checkConnect checkConnect}
400      * method permits connecting to the address and port number of the given
401      * remote peer.
402      *
403      * <p> This method may be invoked at any time. If a {@link #send send} or
404      * {@link #receive receive} operation upon this channel is invoked while an
405      * invocation of this method is in progress then that operation will first
406      * block until this invocation is complete.  If a connection attempt is
407      * initiated but fails, that is, if an invocation of this method throws a
408      * checked exception, then the channel will be closed.
409      *
410      * @param  remote
411      *         The remote peer to which this channel is to be connected
412      *
413      * @return  {@code true} if a connection was established, {@code false} if
414      *          this channel is in non-blocking mode and the connection
415      *          operation is in progress
416      *
417      * @throws  java.nio.channels.AlreadyConnectedException
418      *          If this channel is already connected
419      *
420      * @throws  java.nio.channels.ConnectionPendingException
421      *          If a non-blocking connection operation is already in progress on
422      *          this channel
423      *
424      * @throws  java.nio.channels.ClosedChannelException
425      *          If this channel is closed
426      *
427      * @throws  java.nio.channels.AsynchronousCloseException
428      *          If another thread closes this channel
429      *          while the connect operation is in progress
430      *
431      * @throws  java.nio.channels.ClosedByInterruptException
432      *          If another thread interrupts the current thread
433      *          while the connect operation is in progress, thereby
434      *          closing the channel and setting the current thread's
435      *          interrupt status
436      *
437      * @throws  java.nio.channels.UnresolvedAddressException
438      *          If the given remote address is not fully resolved
439      *
440      * @throws  java.nio.channels.UnsupportedAddressTypeException
441      *          If the type of the given remote address is not supported
442      *
443      * @throws  SecurityException
444      *          If a security manager has been installed
445      *          and it does not permit access to the given remote peer
446      *
447      * @throws  IOException
448      *          If some other I/O error occurs
449      */
450     public abstract boolean connect(SocketAddress remote) throws IOException;
451 
452     /**
453      * Connects this channel's socket.
454      *
455      * <P> This is a convience method and is equivalent to evaluating the
456      * following expression:
457      * <blockquote><pre>
458      * setOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS, SctpStandardSocketOption.InitMaxStreams.create(maxInStreams, maxOutStreams))
459      *  .connect(remote);
460      * </pre></blockquote>
461      *
462      * <P> The {@code maxOutStreams} and {@code maxInStreams} parameters
463      * represent the maximum number of streams that the application wishes to be
464      * able to send to and receive from. They are negotiated with the remote
465      * peer and may be limited by the operating system.
466      *
467      * @param  remote
468      *         The remote peer to which this channel is to be connected
469      *
470      * @param  maxOutStreams
471      *         Must be non negative and no larger than {@code 65536}.
472      *         {@code 0} to use the endpoints default value.
473      *
474      * @param  maxInStreams
475      *         Must be non negative and no larger than {@code 65536}.
476      *         {@code 0} to use the endpoints default value.
477      *
478      * @return  {@code true} if a connection was established, {@code false} if
479      *          this channel is in non-blocking mode and the connection operation
480      *          is in progress
481      *
482      * @throws  java.nio.channels.AlreadyConnectedException
483      *          If this channel is already connected
484      *
485      * @throws  java.nio.channels.ConnectionPendingException
486      *          If a non-blocking connection operation is already in progress on
487      *          this channel
488      *
489      * @throws  java.nio.channels.ClosedChannelException
490      *          If this channel is closed
491      *
492      * @throws  java.nio.channels.AsynchronousCloseException
493      *          If another thread closes this channel
494      *          while the connect operation is in progress
495      *
496      * @throws  java.nio.channels.ClosedByInterruptException
497      *          If another thread interrupts the current thread
498      *          while the connect operation is in progress, thereby
499      *          closing the channel and setting the current thread's
500      *          interrupt status
501      *
502      * @throws  java.nio.channels.UnresolvedAddressException
503      *          If the given remote address is not fully resolved
504      *
505      * @throws  java.nio.channels.UnsupportedAddressTypeException
506      *          If the type of the given remote address is not supported
507      *
508      * @throws  SecurityException
509      *          If a security manager has been installed
510      *          and it does not permit access to the given remote peer
511      *
512      * @throws  IOException
513      *          If some other I/O error occurs
514      */
515     public abstract boolean connect(SocketAddress remote,
516                                     int maxOutStreams,
517                                     int maxInStreams)
518         throws IOException;
519 
520     /**
521      * Tells whether or not a connection operation is in progress on this channel.
522      *
523      * @return  {@code true} if, and only if, a connection operation has been initiated
524      *          on this channel but not yet completed by invoking the
525      *          {@link #finishConnect} method
526      */
527     public abstract boolean isConnectionPending();
528 
529     /**
530      * Finishes the process of connecting an SCTP channel.
531      *
532      * <P> A non-blocking connection operation is initiated by placing a socket
533      * channel in non-blocking mode and then invoking one of its {@link #connect
534      * connect} methods.  Once the connection is established, or the attempt has
535      * failed, the channel will become connectable and this method may
536      * be invoked to complete the connection sequence.  If the connection
537      * operation failed then invoking this method will cause an appropriate
538      * {@link java.io.IOException} to be thrown.
539      *
540      * <P> If this channel is already connected then this method will not block
541      * and will immediately return <tt>true</tt>.  If this channel is in
542      * non-blocking mode then this method will return <tt>false</tt> if the
543      * connection process is not yet complete.  If this channel is in blocking
544      * mode then this method will block until the connection either completes
545      * or fails, and will always either return <tt>true</tt> or throw a checked
546      * exception describing the failure.
547      *
548      * <P> This method may be invoked at any time. If a {@link #send send} or {@link #receive receive}
549      * operation upon this channel is invoked while an invocation of this
550      * method is in progress then that operation will first block until this
551      * invocation is complete.  If a connection attempt fails, that is, if an
552      * invocation of this method throws a checked exception, then the channel
553      * will be closed.
554      *
555      * @return  {@code true} if, and only if, this channel's socket is now
556      *          connected
557      *
558      * @throws  java.nio.channels.NoConnectionPendingException
559      *          If this channel is not connected and a connection operation
560      *          has not been initiated
561      *
562      * @throws  java.nio.channels.ClosedChannelException
563      *          If this channel is closed
564      *
565      * @throws  java.nio.channels.AsynchronousCloseException
566      *          If another thread closes this channel
567      *          while the connect operation is in progress
568      *
569      * @throws  java.nio.channels.ClosedByInterruptException
570      *          If another thread interrupts the current thread
571      *          while the connect operation is in progress, thereby
572      *          closing the channel and setting the current thread's
573      *          interrupt status
574      *
575      * @throws  IOException
576      *          If some other I/O error occurs
577      */
578     public abstract boolean finishConnect() throws IOException;
579 
580     /**
581      * Returns all of the socket addresses to which this channel's socket is
582      * bound.
583      *
584      * @return  All the socket addresses that this channel's socket is
585      *          bound to, or an empty {@code Set} if the channel's socket is not
586      *          bound
587      *
588      * @throws  ClosedChannelException
589      *          If the channel is closed
590      *
591      * @throws  IOException
592      *          If an I/O error occurs
593      */
594     public abstract Set<SocketAddress> getAllLocalAddresses()
595         throws IOException;
596 
597     /**
598      * Returns all of the remote addresses to which this channel's socket
599      * is connected.
600      *
601      * <P> If the channel is connected to a remote peer that is bound to
602      * multiple addresses then it is these addresses that the channel's socket
603      * is connected.
604      *
605      * @return  All of the remote addresses to which this channel's socket
606      *          is connected, or an empty {@code Set} if the channel's socket is
607      *          not connected
608      *
609      * @throws  ClosedChannelException
610      *          If the channel is closed
611      *
612      * @throws  IOException
613      *          If an I/O error occurs
614      */
615     public abstract Set<SocketAddress> getRemoteAddresses()
616         throws IOException;
617 
618     /**
619      * Shutdown a connection without closing the channel.
620      *
621      * <P> Sends a shutdown command to the remote peer, effectively preventing
622      * any new data from being written to the socket by either peer. Further
623      * sends will throw {@link java.nio.channels.ClosedChannelException}. The
624      * channel remains open to allow the for any data (and notifications) to be
625      * received that may have been sent by the peer before it received the
626      * shutdown command. If the channel is already shutdown then invoking this
627      * method has no effect.
628      *
629      * @return  This channel
630      *
631      * @throws  java.nio.channels.NotYetConnectedException
632      *          If this channel is not yet connected
633      *
634      * @throws  java.nio.channels.ClosedChannelException
635      *          If this channel is closed
636      *
637      * @throws  IOException
638      *          If some other I/O error occurs
639      */
640     public abstract SctpChannel shutdown() throws IOException;
641 
642     /**
643      * Returns the value of a socket option.
644      *
645      * @param   <T>
646      *          The type of the socket option value
647      *
648      * @param   name
649      *          The socket option
650      *
651      * @return  The value of the socket option. A value of {@code null} may be
652      *          a valid value for some socket options.
653      *
654      * @throws  UnsupportedOperationException
655      *          If the socket option is not supported by this channel
656      *
657      * @throws  ClosedChannelException
658      *          If this channel is closed
659      *
660      * @throws  IOException
661      *          If an I/O error occurs
662      *
663      * @see SctpStandardSocketOptions
664      */
665     public abstract <T> T getOption(SctpSocketOption<T> name)
666         throws IOException;
667 
668     /**
669      * Sets the value of a socket option.
670      *
671      * @param   <T>
672      *          The type of the socket option value
673      *
674      * @param   name
675      *          The socket option
676      *
677      * @param   value
678      *          The value of the socket option. A value of {@code null} may be
679      *          a valid value for some socket options.
680      *
681      * @return  This channel
682      *
683      * @throws  UnsupportedOperationException
684      *          If the socket option is not supported by this channel
685      *
686      * @throws  IllegalArgumentException
687      *          If the value is not a valid value for this socket option
688      *
689      * @throws  ClosedChannelException
690      *          If this channel is closed
691      *
692      * @throws  IOException
693      *          If an I/O error occurs
694      *
695      * @see SctpStandardSocketOptions
696      */
697     public abstract <T> SctpChannel setOption(SctpSocketOption<T> name, T value)
698         throws IOException;
699 
700     /**
701      * Returns a set of the socket options supported by this channel.
702      *
703      * <P> This method will continue to return the set of options even after the
704      * channel has been closed.
705      *
706      * @return  A set of the socket options supported by this channel
707      */
708     public abstract Set<SctpSocketOption<?>> supportedOptions();
709 
710     /**
711      * Returns an operation set identifying this channel's supported operations.
712      *
713      * <P> SCTP channels support connecting, reading, and writing, so this
714      * method returns <tt>(</tt>{@link SelectionKey#OP_CONNECT}
715      * <tt>|</tt>&nbsp;{@link SelectionKey#OP_READ} <tt>|</tt>&nbsp;{@link
716      * SelectionKey#OP_WRITE}<tt>)</tt>.  </p>
717      *
718      * @return  The valid-operation set
719      */
720     @Override
721     public final int validOps() {
722         return (SelectionKey.OP_READ |
723                 SelectionKey.OP_WRITE |
724                 SelectionKey.OP_CONNECT);
725     }
726 
727     /**
728      * Receives a message into the given buffer and/or handles a notification.
729      *
730      * <P> If a message or notification is immediately available, or if this
731      * channel is in blocking mode and one eventually becomes available, then
732      * the message or notification is returned or handled, respectively. If this
733      * channel is in non-blocking mode and a message or notification is not
734      * immediately available then this method immediately returns {@code null}.
735      *
736      * <P> If this method receives a message it is copied into the given byte
737      * buffer. The message is transferred into the given byte buffer starting at
738      * its current position and the buffers position is incremented by the
739      * number of bytes read. If there are fewer bytes remaining in the buffer
740      * than are required to hold the message, or the underlying input buffer
741      * does not contain the complete message, then an invocation of {@link
742      * MessageInfo#isComplete isComplete} on the returned {@code
743      * MessageInfo} will return {@code false}, and more invocations of this
744      * method will be necessary to completely consume the messgae. Only
745      * one message at a time will be partially delivered in any stream. The
746      * socket option {@link SctpStandardSocketOptions#SCTP_FRAGMENT_INTERLEAVE
747      * SCTP_FRAGMENT_INTERLEAVE} controls various aspects of what interlacing of
748      * messages occurs.
749      *
750      * <P> If this method receives a notification then the appropriate method of
751      * the given handler, if there is one, is invoked. If the handler returns
752      * {@link HandlerResult#CONTINUE CONTINUE} then this method will try to
753      * receive another message/notification, otherwise, if {@link
754      * HandlerResult#RETURN RETURN} is returned this method will return {@code
755      * null}. If an uncaught exception is thrown by the handler it will be
756      * propagated up the stack through this method.
757      *
758      * <P> This method may be invoked at any time. If another thread has
759      * already initiated a receive operation upon this channel, then an
760      * invocation of this method will block until the first operation is
761      * complete. The given handler is invoked without holding any locks used
762      * to enforce the above synchronization policy, that way handlers
763      * will not stall other threads from receiving. A handler should not invoke
764      * the {@code receive} method of this channel, if it does an
765      * {@link IllegalReceiveException} will be thrown.
766      *
767      * @param  <T>
768      *         The type of the attachment
769      *
770      * @param  dst
771      *         The buffer into which message bytes are to be transferred
772      *
773      * @param  attachment
774      *         The object to attach to the receive operation; can be
775      *         {@code null}
776      *
777      * @param  handler
778      *         A handler to handle notifications from the SCTP stack, or {@code
779      *         null} to ignore any notifications.
780      *
781      * @return  The {@code MessageInfo}, {@code null} if this channel is in
782      *          non-blocking mode and no messages are immediately available or
783      *          the notification handler returns {@link HandlerResult#RETURN
784      *          RETURN} after handling a notification
785      *
786      * @throws  java.nio.channels.ClosedChannelException
787      *          If this channel is closed
788      *
789      * @throws  java.nio.channels.AsynchronousCloseException
790      *          If another thread closes this channel
791      *          while the read operation is in progress
792      *
793      * @throws  java.nio.channels.ClosedByInterruptException
794      *          If another thread interrupts the current thread
795      *          while the read operation is in progress, thereby
796      *          closing the channel and setting the current thread's
797      *          interrupt status
798      *
799      * @throws  java.nio.channels.NotYetConnectedException
800      *          If this channel is not yet connected
801      *
802      * @throws  IllegalReceiveException
803      *          If the given handler invokes the {@code receive} method of this
804      *          channel
805      *
806      * @throws  IOException
807      *          If some other I/O error occurs
808      */
809     public abstract <T> MessageInfo receive(ByteBuffer dst,
810                                             T attachment,
811                                             NotificationHandler<T> handler)
812         throws IOException;
813 
814     /**
815      * Sends a message via this channel.
816      *
817      * <P> If this channel is in non-blocking mode and there is sufficient room
818      * in the underlying output buffer, or if this channel is in blocking mode
819      * and sufficient room becomes available, then the remaining bytes in the
820      * given byte buffer are transmitted as a single message. Sending a message
821      * is atomic unless explicit message completion {@link
822      * SctpStandardSocketOptions#SCTP_EXPLICIT_COMPLETE SCTP_EXPLICIT_COMPLETE}
823      * socket option is enabled on this channel's socket.
824      *
825      * <P> The message is transferred from the byte buffer as if by a regular
826      * {@link java.nio.channels.WritableByteChannel#write(java.nio.ByteBuffer)
827      * write} operation.
828      *
829      * <P> The bytes will be written to the stream number that is specified by
830      * {@link MessageInfo#streamNumber streamNumber} in the given {@code
831      * messageInfo}.
832      *
833      * <P> This method may be invoked at any time. If another thread has already
834      * initiated a send operation upon this channel, then an invocation of
835      * this method will block until the first operation is complete.
836      *
837      * @param  src
838      *         The buffer containing the message to be sent
839      *
840      * @param  messageInfo
841      *         Ancillary data about the message to be sent
842      *
843      * @return  The number of bytes sent, which will be either the number of
844      *          bytes that were remaining in the messages buffer when this method
845      *          was invoked or, if this channel is non-blocking, may be zero if
846      *          there was insufficient room for the message in the underlying
847      *          output buffer
848      *
849      * @throws  InvalidStreamException
850      *          If {@code streamNumner} is negative or greater than or equal to
851      *          the maximum number of outgoing streams
852      *
853      * @throws  java.nio.channels.ClosedChannelException
854      *          If this channel is closed
855      *
856      * @throws  java.nio.channels.AsynchronousCloseException
857      *          If another thread closes this channel
858      *          while the read operation is in progress
859      *
860      * @throws  java.nio.channels.ClosedByInterruptException
861      *          If another thread interrupts the current thread
862      *          while the read operation is in progress, thereby
863      *          closing the channel and setting the current thread's
864      *          interrupt status
865      *
866      * @throws  java.nio.channels.NotYetConnectedException
867      *          If this channel is not yet connected
868      *
869      * @throws  IOException
870      *          If some other I/O error occurs
871      */
872     public abstract int send(ByteBuffer src, MessageInfo messageInfo)
873         throws IOException;
874 }