src/jdk.net/share/classes/jdk/net/RdmaSockets.java
branchrsocket-branch
changeset 57115 512e7cc6ccce
child 57117 7f1b415bfede
equal deleted inserted replaced
53485:b743968ad646 57115:512e7cc6ccce
       
     1 /*
       
     2  * Copyright (c) 2018, 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 
       
    26 package jdk.net;
       
    27 
       
    28 import java.net.ProtocolFamily;
       
    29 import java.net.ServerSocket;
       
    30 import java.net.Socket;
       
    31 import java.net.SocketException;
       
    32 import java.net.SocketOption;
       
    33 import java.net.StandardSocketOptions;
       
    34 import java.nio.channels.Selector;
       
    35 import java.nio.channels.ServerSocketChannel;
       
    36 import java.nio.channels.SocketChannel;
       
    37 import java.nio.channels.spi.SelectorProvider;
       
    38 import java.io.IOException;
       
    39 import java.util.Objects;
       
    40 import jdk.internal.net.rdma.RdmaPollSelectorProvider;
       
    41 import jdk.internal.net.rdma.RdmaSocketProvider;
       
    42 
       
    43 /**
       
    44  * Factory methods for creating RDMA-based TCP sockets and channels.
       
    45  *
       
    46  * <p>The {@link #openSocket(ProtocolFamily family) openSocket} and {@link
       
    47  * #openServerSocket(ProtocolFamily family) openServerSocket} methods
       
    48  * create RDMA-based TCP sockets.
       
    49  *
       
    50  * <p>The {@link #openSelector() openSelector}, {@link
       
    51  * #openSocketChannel(ProtocolFamily family) openSocketChannel}, and {@link
       
    52  * #openServerSocketChannel(ProtocolFamily family) openServerSocketChannel}
       
    53  * methods create selectors and selectable channels for use with RDMA sockets.
       
    54  * These objects are created by a {@link java.nio.channels.spi.SelectorProvider
       
    55  * SelectorProvider} that is not the default {@code SelectorProvider}.
       
    56  * Consequently, selectable channels to RDMA sockets may not be multiplexed
       
    57  * with selectable channels created by the default selector provider. Its
       
    58  * selector provider does not support datagram channels and pipes.
       
    59  * The {@link java.nio.channels.spi.SelectorProvider#openDatagramChannel
       
    60  * openDatagramChannel} and
       
    61  * {@link java.nio.channels.spi.SelectorProvider#openPipe openPipe} methods
       
    62  * throw {@link java.lang.UnsupportedOperationException
       
    63  * UnsupportedOperationException}.
       
    64  *
       
    65  * @since 12
       
    66  */
       
    67 public class RdmaSockets {
       
    68 
       
    69     private RdmaSockets() {}
       
    70 
       
    71     /**
       
    72      * Creates an unbound and unconnected RDMA socket.
       
    73      *
       
    74      * <p> An RDMA socket supports the same socket options that {@link
       
    75      * java.net.Socket java.net.Socket} defines. In addition, it supports the
       
    76      * socket options specified by {@link RdmaSocketOptions}.
       
    77      *
       
    78      * <p> When binding the socket to a local address, or invoking {@code
       
    79      * connect} to connect the socket, the socket address specified to those
       
    80      * methods must correspond to the protocol family specified here.
       
    81      *
       
    82      * @param   family
       
    83      *          The protocol family
       
    84      *
       
    85      * @throws IOException
       
    86      *         If an I/O error occurs
       
    87      * @throws NullPointerException
       
    88      *         If name is {@code null}
       
    89      * @throws UnsupportedOperationException
       
    90      *         If RDMA sockets are not supported on this platform or if the
       
    91      *         specified protocol family is not supported. For example, if
       
    92      *         the parameter is {@link java.net.StandardProtocolFamily#INET6
       
    93      *         StandardProtocolFamily.INET6} but IPv6 is not enabled on the
       
    94      *         platform.
       
    95      */
       
    96     public static Socket openSocket(ProtocolFamily family) throws IOException {
       
    97         Objects.requireNonNull("protocol family is null");
       
    98         return RdmaSocketProvider.openSocket(family);
       
    99     }
       
   100 
       
   101     /**
       
   102      * Creates an unbound RDMA server socket.
       
   103      *
       
   104      * <p> An RDMA socket supports the same socket options that {@link
       
   105      * java.net.ServerSocket java.net.ServerSocket} defines.
       
   106      *
       
   107      * <p> When binding the socket to an address, the socket address specified
       
   108      * to the {@code bind} method must correspond to the protocol family
       
   109      * specified here.
       
   110      *
       
   111      * @param   family
       
   112      *          The protocol family
       
   113      *
       
   114      * @throws IOException
       
   115      *         If an I/O error occurs
       
   116      * @throws NullPointerException
       
   117      *         If name is {@code null}
       
   118      * @throws UnsupportedOperationException
       
   119      *         If RDMA sockets are not supported on this platform or if the
       
   120      *         specified protocol family is not supported. For example, if
       
   121      *         the parameter is {@link java.net.StandardProtocolFamily#INET6
       
   122      *         StandardProtocolFamily.INET6} but IPv6 is not enabled on the
       
   123      *         platform.
       
   124      */
       
   125     public static ServerSocket openServerSocket(ProtocolFamily family)
       
   126             throws IOException {
       
   127         Objects.requireNonNull("protocol family is null");
       
   128         return RdmaSocketProvider.openServerSocket(family);
       
   129     }
       
   130 
       
   131     /**
       
   132      * Opens a socket channel to an RDMA socket. A newly created socket channel
       
   133      * is {@link SocketChannel#isOpen() open}, not yet bound to a {@link
       
   134      * SocketChannel#getLocalAddress() local address}, and not yet
       
   135      * {@link SocketChannel#isConnected() connected}.
       
   136      *
       
   137      * <p> A socket channel to an RDMA socket supports all of the socket options
       
   138      * specified by {@code SocketChannel}. In addition, it supports the
       
   139      * socket options specified by {@link RdmaSocketOptions}.
       
   140      *
       
   141      * <p> When binding the channel's socket to a local address, or invoking
       
   142      * {@code connect} to connect channel's socket, the socket address specified
       
   143      * to those methods must correspond to the protocol family specified here.
       
   144      *
       
   145      * @param   family
       
   146      *          The protocol family
       
   147      *
       
   148      * @throws IOException
       
   149      *         If an I/O error occurs
       
   150      * @throws NullPointerException
       
   151      *         If name is {@code null}
       
   152      * @throws UnsupportedOperationException
       
   153      *         If RDMA sockets are not supported on this platform or if the
       
   154      *         specified protocol family is not supported. For example, if
       
   155      *         the parameter is {@link java.net.StandardProtocolFamily#INET6
       
   156      *         StandardProtocolFamily.INET6} but IPv6 is not enabled on the
       
   157      *         platform.
       
   158      */
       
   159     public static SocketChannel openSocketChannel(ProtocolFamily family)
       
   160             throws IOException {
       
   161         Objects.requireNonNull("protocol family is null");
       
   162         SelectorProvider provider = RdmaPollSelectorProvider.provider();
       
   163         return ((RdmaPollSelectorProvider)provider).openSocketChannel(family);
       
   164     }
       
   165 
       
   166     /**
       
   167      * Opens a server socket channel to an RDMA socket. A newly created socket
       
   168      * channel is {@link SocketChannel#isOpen() open} but not yet bound to a
       
   169      * {@link SocketChannel#getLocalAddress() local address}.
       
   170      *
       
   171      * <p> When binding the channel's socket to an address, the socket address
       
   172      * specified to the {@code bind} method must correspond to the protocol
       
   173      * family specified here.
       
   174      *
       
   175      * @param   family
       
   176      *          The protocol family
       
   177      *
       
   178      * @throws IOException
       
   179      *         If an I/O error occurs
       
   180      * @throws NullPointerException
       
   181      *         If name is {@code null}
       
   182      * @throws UnsupportedOperationException
       
   183      *         If RDMA sockets are not supported on this platform or if the
       
   184      *         specified protocol family is not supported. For example, if
       
   185      *         the parameter is {@link java.net.StandardProtocolFamily#INET6
       
   186      *         StandardProtocolFamily.INET6} but IPv6 is not enabled on the
       
   187      *         platform.
       
   188      */
       
   189     public static ServerSocketChannel openServerSocketChannel(
       
   190             ProtocolFamily family) throws IOException {
       
   191         Objects.requireNonNull("protocol family is null");
       
   192         SelectorProvider provider = RdmaPollSelectorProvider.provider();
       
   193         return ((RdmaPollSelectorProvider)provider)
       
   194                 .openServerSocketChannel(family);
       
   195     }
       
   196 
       
   197     /**
       
   198      * Opens a selector to multiplex selectable channels to RDMA sockets.
       
   199      *
       
   200      * @throws IOException
       
   201      *         If an I/O error occurs
       
   202      * @throws UnsupportedOperationException
       
   203      *         If RDMA sockets are not supported on this platform
       
   204      */
       
   205     public static Selector openSelector() throws IOException {
       
   206         SelectorProvider provider = RdmaPollSelectorProvider.provider();
       
   207         return provider.openSelector();
       
   208     }
       
   209 }