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.internal.net.rdma;
  27 
  28 import java.io.FileDescriptor;
  29 import java.io.IOException;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import sun.nio.ch.SocketDispatcher;
  33 
  34 /**
  35  * Allows different platforms to call different native methods
  36  * for read and write operations.
  37  */
  38 
  39 public class RdmaSocketDispatcher extends SocketDispatcher
  40 {
  41     private static PlatformRdmaSocketDispatcher platformRdmaSocketDispatcher =
  42             PlatformRdmaSocketDispatcher.get();
  43 
  44     protected int read(FileDescriptor fd, long address, int len)
  45             throws IOException {
  46         return platformRdmaSocketDispatcher.read(fd, address, len);
  47     }
  48 
  49     protected long readv(FileDescriptor fd, long address, int len)
  50             throws IOException {
  51         return platformRdmaSocketDispatcher.readv(fd, address, len);
  52     }
  53 
  54     protected int write(FileDescriptor fd, long address, int len)
  55             throws IOException {
  56         return platformRdmaSocketDispatcher.write(fd, address, len);
  57     }
  58 
  59     protected long writev(FileDescriptor fd, long address, int len)
  60             throws IOException {
  61         return platformRdmaSocketDispatcher.writev(fd, address, len);
  62     }
  63 
  64     protected void close(FileDescriptor fd) throws IOException {
  65         platformRdmaSocketDispatcher.close(fd);
  66     }
  67 
  68     public void preClose(FileDescriptor fd) throws IOException {
  69         platformRdmaSocketDispatcher.preClose(fd);
  70     }
  71 
  72     static class PlatformRdmaSocketDispatcher {
  73         @SuppressWarnings("unchecked")
  74         private static PlatformRdmaSocketDispatcher newInstance(String cn) {
  75             Class<PlatformRdmaSocketDispatcher> c;
  76             try {
  77                 c = (Class<PlatformRdmaSocketDispatcher>)Class.forName(cn);
  78                 return c.getConstructor(new Class<?>[] {}).newInstance();
  79             } catch (ReflectiveOperationException x) {
  80                 throw new AssertionError(x);
  81             }
  82         }
  83 
  84         private static PlatformRdmaSocketDispatcher create() {
  85             String osname = AccessController.doPrivileged(
  86                     new PrivilegedAction<String>() {
  87                         public String run() {
  88                             return System.getProperty("os.name");
  89                         }
  90                     });
  91             if ("Linux".equals(osname))
  92                 return newInstance(
  93                         "jdk.internal.net.rdma.LinuxRdmaSocketDispatcher");
  94             return new PlatformRdmaSocketDispatcher();
  95         }
  96 
  97         private static final PlatformRdmaSocketDispatcher instance = create();
  98 
  99         static PlatformRdmaSocketDispatcher get() {
 100             return instance;
 101         }
 102 
 103         protected int read(FileDescriptor fd, long address, int len)
 104                 throws IOException {
 105             throw new UnsupportedOperationException(
 106                     "unsupported socket operation");
 107         }
 108 
 109         protected long readv(FileDescriptor fd, long address, int len)
 110                 throws IOException {
 111             throw new UnsupportedOperationException(
 112                     "unsupported socket operation");
 113         }
 114 
 115         protected int write(FileDescriptor fd, long address, int len)
 116                 throws IOException {
 117             throw new UnsupportedOperationException(
 118                     "unsupported socket operation");
 119         }
 120 
 121         protected long writev(FileDescriptor fd, long address, int len)
 122                 throws IOException {
 123             throw new UnsupportedOperationException(
 124                     "unsupported socket operation");
 125         }
 126 
 127         protected void close(FileDescriptor fd) throws IOException {
 128             throw new UnsupportedOperationException(
 129                     "unsupported socket operation");
 130         }
 131 
 132         public void preClose(FileDescriptor fd) throws IOException {
 133             throw new UnsupportedOperationException(
 134                     "unsupported socket operation");
 135         }
 136     }
 137 }