< prev index next >

src/jdk.net/share/classes/jdk/net/Sockets.java

Print this page




   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 
  26 package jdk.net;
  27 
  28 import java.net.*;

  29 import java.io.IOException;

  30 import java.util.Collections;
  31 import java.util.HashMap;
  32 import java.util.HashSet;
  33 import java.util.Map;
  34 import java.util.Set;


  35 import jdk.net.ExtendedSocketOptions.PlatformSocketOptions;
  36 
  37 /**
  38  * Defines static methods to set and get socket options defined by the
  39  * {@link java.net.SocketOption} interface. All of the standard options defined
  40  * by {@link java.net.Socket}, {@link java.net.ServerSocket}, and
  41  * {@link java.net.DatagramSocket} can be set this way, as well as additional
  42  * or platform specific options supported by each socket type.
  43  * <p>
  44  * The {@link #supportedOptions(Class)} method can be called to determine
  45  * the complete set of options available (per socket type) on the
  46  * current system.
  47  * <p>










  48  * When a security manager is installed, some non-standard socket options
  49  * may require a security permission before being set or get.
  50  * The details are specified in {@link ExtendedSocketOptions}. No permission
  51  * is required for {@link java.net.StandardSocketOptions}.

  52  *
  53  * @see java.nio.channels.NetworkChannel
  54  */
  55 public class Sockets {
  56 
  57     private static final Map<Class<?>,Set<SocketOption<?>>>
  58             options = optionSets();
  59 
  60     private Sockets() {}
  61 
  62     /**
  63      * Sets the value of a socket option on a {@link java.net.Socket}
  64      *
  65      * @param s the socket
  66      * @param name The socket option
  67      * @param value The value of the socket option. May be null for some
  68      *              options.
  69      *
  70      * @throws UnsupportedOperationException if the socket does not support
  71      *         the option.


 357 
 358         static {
 359             Set<SocketOption<?>> s = new Socket().supportedOptions();
 360             available = s.contains(ExtendedSocketOptions.TCP_QUICKACK);
 361         }
 362     }
 363 
 364     /**
 365      * Tells whether TCP_KEEPALIVE options are supported.
 366      */
 367     static class KeepAliveOptions {
 368 
 369         static final boolean AVAILABLE;
 370 
 371         static {
 372             Set<SocketOption<?>> s = new Socket().supportedOptions();
 373             AVAILABLE = s.containsAll(Set.of(ExtendedSocketOptions.TCP_KEEPCOUNT,
 374                                             ExtendedSocketOptions.TCP_KEEPIDLE,
 375                                             ExtendedSocketOptions.TCP_KEEPINTERVAL));
 376         }























































































































 377     }
 378 }


   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 
  26 package jdk.net;
  27 
  28 import java.net.*;
  29 import java.nio.channels.*;
  30 import java.io.IOException;
  31 import java.lang.reflect.Field;
  32 import java.util.Collections;
  33 import java.util.HashMap;
  34 import java.util.HashSet;
  35 import java.util.Map;
  36 import java.util.Set;
  37 import rdma.ch.RdmaPollSelectorProvider;
  38 import rdma.ch.RdmaSocketImpl;
  39 import jdk.net.ExtendedSocketOptions.PlatformSocketOptions;
  40 
  41 /**
  42  * Defines static methods to set and get socket options defined by the
  43  * {@link java.net.SocketOption} interface. All of the standard options defined
  44  * by {@link java.net.Socket}, {@link java.net.ServerSocket}, and
  45  * {@link java.net.DatagramSocket} can be set this way, as well as additional
  46  * or platform specific options supported by each socket type.
  47  * <p>
  48  * The {@link #supportedOptions(Class)} method can be called to determine
  49  * the complete set of options available (per socket type) on the
  50  * current system.
  51  * <p>
  52  * The {@link #openRdmaSelector() openRdmaSelector}, {@link
  53  * #openRdmaSocketChannel() openRdmaSocketChannel}, and {@link
  54  * #openRdmaServerSocketChannel() openRdmaServerSocketChannel} methods
  55  * create selectors and selectable channels for use with RDMA sockets.
  56  * These objects are created a {@link java.nio.channels.spi.SelectorProvider
  57  * SelectorProvider} that is not the default {@code SelectorProvider}.
  58  * Consequently, selectable channels to RDMA sockets may not be multiplexed
  59  * with selectable channel created by the default selector provider. The
  60  * selector provider does not support datagram channels, its {@code
  61  * openDatagramChannel} methods throw {@link UnsupportedOperationException}.
  62  * When a security manager is installed, some non-standard socket options
  63  * may require a security permission before being set or get.
  64  * The details are specified in {@link ExtendedSocketOptions}. No permission
  65  * is required for {@link java.net.StandardSocketOptions}.
  66  * <p>
  67  *
  68  * @see java.nio.channels.NetworkChannel
  69  */
  70 public class Sockets {
  71 
  72     private static final Map<Class<?>,Set<SocketOption<?>>>
  73             options = optionSets();
  74 
  75     private Sockets() {}
  76 
  77     /**
  78      * Sets the value of a socket option on a {@link java.net.Socket}
  79      *
  80      * @param s the socket
  81      * @param name The socket option
  82      * @param value The value of the socket option. May be null for some
  83      *              options.
  84      *
  85      * @throws UnsupportedOperationException if the socket does not support
  86      *         the option.


 372 
 373         static {
 374             Set<SocketOption<?>> s = new Socket().supportedOptions();
 375             available = s.contains(ExtendedSocketOptions.TCP_QUICKACK);
 376         }
 377     }
 378 
 379     /**
 380      * Tells whether TCP_KEEPALIVE options are supported.
 381      */
 382     static class KeepAliveOptions {
 383 
 384         static final boolean AVAILABLE;
 385 
 386         static {
 387             Set<SocketOption<?>> s = new Socket().supportedOptions();
 388             AVAILABLE = s.containsAll(Set.of(ExtendedSocketOptions.TCP_KEEPCOUNT,
 389                                             ExtendedSocketOptions.TCP_KEEPIDLE,
 390                                             ExtendedSocketOptions.TCP_KEEPINTERVAL));
 391         }
 392     }
 393 
 394 
 395     /**
 396      * Creates an unconnected RDMA socket.
 397      *
 398      * <p> A RDMA socket supports the same socket options that that {@code
 399      * java.net.Socket} defines. In addition, it also supports the socket options
 400      * specified by {@link RdmaSocketOptions}.
 401      *
 402      * @apiNote The rsocket implementation on Linux only supports IPv4 addresses.
 403      * Consequently, attempting to bind or connect to an IPv6 address will fail
 404      * with {@code IllegalArgumentException}.
 405      *
 406      * @throws IOException
 407      *         If an I/O error occurs
 408      * @throws UnsupportedOperationException
 409      *         If RDMA sockets are not supported on this platform
 410      *
 411      * @since 12
 412      */
 413     public static Socket openRdmaSocket() throws IOException {
 414         RdmaSocketImpl impl = new RdmaSocketImpl();
 415         Socket s = new Socket(impl) {};
 416         return s;
 417     }
 418 
 419     /**
 420      * Creates an unbound RDMA server socket.
 421      *
 422      * <p> A RDMA socket supports the same socket options that that {@code
 423      * java.net.ServerSocket} defines.
 424      *
 425      * @apiNote The rsocket implementation on Linux only supports IPv4 addresses.
 426      * Consequently, attempting to bind to an IPv6 address will fail with
 427      * {@code IllegalArgumentException}.
 428      *
 429      * @throws IOException
 430      *         If an I/O error occurs
 431      * @throws UnsupportedOperationException
 432      *         If RDMA sockets are not supported on this platform
 433      *
 434      * @since N
 435      */
 436     public static ServerSocket openRdmaServerSocket() throws IOException {
 437         SocketImpl impl = new RdmaSocketImpl();
 438         ServerSocket ss = new ServerSocket(impl) {
 439             public Socket accept() throws IOException {
 440                 if (isClosed())
 441                 throw new SocketException("Socket is closed");
 442                 if (!isBound())
 443                     throw new SocketException("Socket is not bound yet");
 444 
 445                 Socket s = openRdmaSocket();
 446                 implAccept(s);
 447                 return s;
 448             }    
 449         };
 450         return ss;
 451     }
 452 
 453     /**
 454      * Opens a socket channel to a RDMA socket. A newly-created socket channel
 455      * is {@link SocketChannel#isOpen() open}, not yet bound to a {@link
 456      * SocketChannel#getLocalAddress() local address}, and not yet
 457      * {@link SocketChannel#isConnected() connected}.
 458      *
 459      * <p> A socket channel to a RDMA socket supports all of the socket options
 460      * specified by {@code SocketChannel}. In addition, it also supports the
 461      * socket options specified by {@link RdmaSocketOptions}.
 462      *
 463      * @apiNote The rsocket implementation on Linux only supports IPv4 addresses.
 464      * Consequently, attempting to bind or connect to an IPv6 address will fail
 465      * with {@code UnsupportedAddressTypeException}.
 466      *
 467      * @throws IOException
 468      *         If an I/O error occurs
 469      * @throws UnsupportedOperationException
 470      *         If RDMA sockets are not supported on this platform
 471      *
 472      * @since 12
 473      */
 474     public static SocketChannel openRdmaSocketChannel() throws IOException {
 475         return RdmaPollSelectorProvider.provider().openSocketChannel();
 476     }
 477 
 478     /**
 479      * Opens a server-socket channel to a RDMA socket. A newly-created socket
 480      * channel is {@link SocketChannel#isOpen() open} but not yet bound to a
 481      * {@link SocketChannel#getLocalAddress() local address}.
 482      *
 483      * @apiNote The rsocket implementation on Linux only supports IPv4 addresses.
 484      * Consequently, attempting to bind to an IPv6 address will fail with
 485      * {@code UnsupportedAddressTypeException}.
 486      *
 487      * @throws IOException
 488      *         If an I/O error occurs
 489      * @throws UnsupportedOperationException
 490      *         If RDMA sockets are not supported on this platform
 491      *
 492      * @since 12
 493      */
 494     public static ServerSocketChannel openRdmaServerSocketChannel()
 495         throws IOException {
 496         return RdmaPollSelectorProvider.provider().openServerSocketChannel();
 497     }
 498  
 499     /**
 500      * Opens a selector to multiplex selectable channels to RDMA sockets.
 501      *
 502      * @throws IOException
 503      *         If an I/O error occurs
 504      * @throws UnsupportedOperationException
 505      *         If RDMA sockets are not supported on this platform
 506      *
 507      * @since 12
 508      */
 509     public static Selector openRdmaSelector() throws IOException {
 510         return RdmaPollSelectorProvider.provider().openSelector();
 511     }
 512 }
< prev index next >