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