jdk/src/share/classes/sun/nio/ch/SocketOptsImpl.java
changeset 1152 29d6145d1097
parent 1151 4070cecdb99d
child 1153 6b88c071a015
equal deleted inserted replaced
1151:4070cecdb99d 1152:29d6145d1097
     1 /*
       
     2  * Copyright 2001 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.nio.ch;
       
    27 
       
    28 import java.io.FileDescriptor;
       
    29 import java.io.IOException;
       
    30 import java.net.NetworkInterface;
       
    31 import java.net.SocketOptions;
       
    32 import java.nio.channels.*;
       
    33 
       
    34 
       
    35 class SocketOptsImpl
       
    36     implements SocketOpts
       
    37 {
       
    38 
       
    39     static abstract class Dispatcher {
       
    40         abstract int getInt(int opt) throws IOException;
       
    41         abstract void setInt(int opt, int arg) throws IOException;
       
    42         // Others that pass addresses, etc., will come later
       
    43     }
       
    44 
       
    45     private final Dispatcher d;
       
    46 
       
    47     SocketOptsImpl(Dispatcher d) {
       
    48         this.d = d;
       
    49     }
       
    50 
       
    51     protected boolean getBoolean(int opt) throws IOException {
       
    52         return d.getInt(opt) > 0;
       
    53     }
       
    54 
       
    55     protected void setBoolean(int opt, boolean b) throws IOException {
       
    56         d.setInt(opt, b ? 1 : 0);
       
    57     }
       
    58 
       
    59     protected int getInt(int opt) throws IOException {
       
    60         return d.getInt(opt);
       
    61     }
       
    62 
       
    63     protected void setInt(int opt, int n) throws IOException {
       
    64         d.setInt(opt, n);
       
    65     }
       
    66 
       
    67     protected NetworkInterface getNetworkInterface(int opt)
       
    68         throws IOException
       
    69     {
       
    70         throw new UnsupportedOperationException("NYI");
       
    71     }
       
    72 
       
    73     protected void setNetworkInterface(int opt, NetworkInterface ni)
       
    74         throws IOException
       
    75     {
       
    76         throw new UnsupportedOperationException("NYI");
       
    77     }
       
    78 
       
    79     protected void addToString(StringBuffer sb, String s) {
       
    80         char c = sb.charAt(sb.length() - 1);
       
    81         if ((c != '[') && (c != '='))
       
    82             sb.append(' ');
       
    83         sb.append(s);
       
    84     }
       
    85 
       
    86     protected void addToString(StringBuffer sb, int n) {
       
    87         addToString(sb, Integer.toString(n));
       
    88     }
       
    89 
       
    90 
       
    91     // SO_BROADCAST
       
    92 
       
    93     public boolean broadcast() throws IOException {
       
    94         return getBoolean(SocketOptions.SO_BROADCAST);
       
    95     }
       
    96 
       
    97     public SocketOpts broadcast(boolean b) throws IOException {
       
    98         setBoolean(SocketOptions.SO_BROADCAST, b);
       
    99         return this;
       
   100     }
       
   101 
       
   102 
       
   103     // SO_KEEPALIVE
       
   104 
       
   105     public boolean keepAlive() throws IOException {
       
   106         return getBoolean(SocketOptions.SO_KEEPALIVE);
       
   107     }
       
   108 
       
   109     public SocketOpts keepAlive(boolean b) throws IOException {
       
   110         setBoolean(SocketOptions.SO_KEEPALIVE, b);
       
   111         return this;
       
   112     }
       
   113 
       
   114 
       
   115     // SO_LINGER
       
   116 
       
   117     public int linger() throws IOException {
       
   118         return getInt(SocketOptions.SO_LINGER);
       
   119     }
       
   120 
       
   121     public SocketOpts linger(int n) throws IOException {
       
   122         setInt(SocketOptions.SO_LINGER, n);
       
   123         return this;
       
   124     }
       
   125 
       
   126 
       
   127     // SO_OOBINLINE
       
   128 
       
   129     public boolean outOfBandInline() throws IOException {
       
   130         return getBoolean(SocketOptions.SO_OOBINLINE);
       
   131     }
       
   132 
       
   133     public SocketOpts outOfBandInline(boolean b) throws IOException {
       
   134         setBoolean(SocketOptions.SO_OOBINLINE, b);
       
   135         return this;
       
   136     }
       
   137 
       
   138 
       
   139     // SO_RCVBUF
       
   140 
       
   141     public int receiveBufferSize() throws IOException {
       
   142         return getInt(SocketOptions.SO_RCVBUF);
       
   143     }
       
   144 
       
   145     public SocketOpts receiveBufferSize(int n) throws IOException {
       
   146         if (n <= 0)
       
   147             throw new IllegalArgumentException("Invalid receive size");
       
   148         setInt(SocketOptions.SO_RCVBUF, n);
       
   149         return this;
       
   150     }
       
   151 
       
   152 
       
   153     // SO_SNDBUF
       
   154 
       
   155     public int sendBufferSize() throws IOException {
       
   156         return getInt(SocketOptions.SO_SNDBUF);
       
   157     }
       
   158 
       
   159     public SocketOpts sendBufferSize(int n) throws IOException {
       
   160         if (n <= 0)
       
   161             throw new IllegalArgumentException("Invalid send size");
       
   162         setInt(SocketOptions.SO_SNDBUF, n);
       
   163         return this;
       
   164     }
       
   165 
       
   166 
       
   167     // SO_REUSEADDR
       
   168 
       
   169     public boolean reuseAddress() throws IOException {
       
   170         return getBoolean(SocketOptions.SO_REUSEADDR);
       
   171     }
       
   172 
       
   173     public SocketOpts reuseAddress(boolean b) throws IOException {
       
   174         setBoolean(SocketOptions.SO_REUSEADDR, b);
       
   175         return this;
       
   176     }
       
   177 
       
   178 
       
   179     // toString
       
   180 
       
   181     protected void toString(StringBuffer sb) throws IOException {
       
   182         int n;
       
   183         if (broadcast())
       
   184             addToString(sb, "broadcast");
       
   185         if (keepAlive())
       
   186             addToString(sb, "keepalive");
       
   187         if ((n = linger()) > 0) {
       
   188             addToString(sb, "linger=");
       
   189             addToString(sb, n);
       
   190         }
       
   191         if (outOfBandInline())
       
   192             addToString(sb, "oobinline");
       
   193         if ((n = receiveBufferSize()) > 0) {
       
   194             addToString(sb, "rcvbuf=");
       
   195             addToString(sb, n);
       
   196         }
       
   197         if ((n = sendBufferSize()) > 0) {
       
   198             addToString(sb, "sndbuf=");
       
   199             addToString(sb, n);
       
   200         }
       
   201         if (reuseAddress())
       
   202             addToString(sb, "reuseaddr");
       
   203     }
       
   204 
       
   205     public String toString() {
       
   206         StringBuffer sb = new StringBuffer();
       
   207         sb.append(this.getClass().getInterfaces()[0].getName());
       
   208         sb.append('[');
       
   209         int i = sb.length();
       
   210         try {
       
   211             toString(sb);
       
   212         } catch (IOException x) {
       
   213             sb.setLength(i);
       
   214             sb.append("closed");
       
   215         }
       
   216         sb.append(']');
       
   217         return sb.toString();
       
   218     }
       
   219 
       
   220 
       
   221     // IP-specific socket options
       
   222 
       
   223     static class IP
       
   224         extends SocketOptsImpl
       
   225         implements SocketOpts.IP
       
   226     {
       
   227 
       
   228         IP(Dispatcher d) {
       
   229             super(d);
       
   230         }
       
   231 
       
   232 
       
   233         // IP_MULTICAST_IF2
       
   234         // ## Do we need IP_MULTICAST_IF also?
       
   235 
       
   236         public NetworkInterface multicastInterface() throws IOException {
       
   237             return getNetworkInterface(SocketOptions.IP_MULTICAST_IF2);
       
   238         }
       
   239 
       
   240         public SocketOpts.IP multicastInterface(NetworkInterface ni)
       
   241             throws IOException
       
   242         {
       
   243             setNetworkInterface(SocketOptions.IP_MULTICAST_IF2, ni);
       
   244             return this;
       
   245         }
       
   246 
       
   247 
       
   248         // IP_MULTICAST_LOOP
       
   249 
       
   250         public boolean multicastLoop() throws IOException {
       
   251             return getBoolean(SocketOptions.IP_MULTICAST_LOOP);
       
   252         }
       
   253 
       
   254         public SocketOpts.IP multicastLoop(boolean b) throws IOException {
       
   255             setBoolean(SocketOptions.IP_MULTICAST_LOOP, b);
       
   256             return this;
       
   257         }
       
   258 
       
   259 
       
   260         // IP_TOS
       
   261 
       
   262         public int typeOfService() throws IOException {
       
   263             return getInt(SocketOptions.IP_TOS);
       
   264         }
       
   265 
       
   266         public SocketOpts.IP typeOfService(int tos) throws IOException {
       
   267             setInt(SocketOptions.IP_TOS, tos);
       
   268             return this;
       
   269         }
       
   270 
       
   271 
       
   272         // toString
       
   273 
       
   274         protected void toString(StringBuffer sb) throws IOException {
       
   275             super.toString(sb);
       
   276             int n;
       
   277             if ((n = typeOfService()) > 0) {
       
   278                 addToString(sb, "tos=");
       
   279                 addToString(sb, n);
       
   280             }
       
   281         }
       
   282 
       
   283 
       
   284         // TCP-specific IP options
       
   285 
       
   286         public static class TCP
       
   287             extends SocketOptsImpl.IP
       
   288             implements SocketOpts.IP.TCP
       
   289         {
       
   290 
       
   291             TCP(Dispatcher d) {
       
   292                 super(d);
       
   293             }
       
   294 
       
   295             // TCP_NODELAY
       
   296 
       
   297             public boolean noDelay() throws IOException {
       
   298                 return getBoolean(SocketOptions.TCP_NODELAY);
       
   299             }
       
   300 
       
   301             public SocketOpts.IP.TCP noDelay(boolean b) throws IOException {
       
   302                 setBoolean(SocketOptions.TCP_NODELAY, b);
       
   303                 return this;
       
   304             }
       
   305 
       
   306 
       
   307             // toString
       
   308 
       
   309             protected void toString(StringBuffer sb) throws IOException {
       
   310                 super.toString(sb);
       
   311                 if (noDelay())
       
   312                     addToString(sb, "nodelay");
       
   313             }
       
   314 
       
   315         }
       
   316     }
       
   317 
       
   318 }