Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the EPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the EPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 
 package org.jruby.ext.socket;
 
 
Encapsulates behavior across the primary socket types Socket, ServerSocket, and Datagramsocket.
 
 public enum SocketType {
     SOCKET(.) {
         private Socket toSocket(Channel channel) {
             return ((SocketChannel)channel).socket();
         }
 
         public int getSoTimeout(Channel channelthrows IOException {
             return toSocket(channel).getSoTimeout();
         }
 
         public void setSoTimeout(Channel channelint timeoutthrows IOException {
             toSocket(channel).setSoTimeout(timeout);
         }
 
         public boolean getReuseAddress(Channel channelthrows IOException {
             return toSocket(channel).getReuseAddress();
         }
 
         public void setReuseAddress(Channel channelboolean reusethrows IOException {
             toSocket(channel).setReuseAddress(reuse);
         }
 
         public int getSendBufferSize(Channel channelthrows IOException {
             return toSocket(channel).getSendBufferSize();
         }
 
         public void setSendBufferSize(Channel channelint sizethrows IOException {
             toSocket(channel).setSendBufferSize(size);
         }
 
         public int getReceiveBufferSize(Channel channelthrows IOException {
             return toSocket(channel).getReceiveBufferSize();
         }
 
         public void setReceiveBufferSize(Channel channelint ithrows IOException {
             toSocket(channel).setReceiveBufferSize(i);
         }
 
         public boolean getOOBInline(Channel channelthrows IOException {
             return toSocket(channel).getOOBInline();
         }
 
         public void setOOBInline(Channel channelboolean bthrows IOException {
             toSocket(channel).setOOBInline(b);
         }
 
         public int getSoLinger(Channel channelthrows IOException {
             return toSocket(channel).getSoLinger();
         }
 
         public void setSoLinger(Channel channelboolean bint ithrows IOException {
             toSocket(channel).setSoLinger(bi);
        }
        public boolean getKeepAlive(Channel channelthrows IOException {
            return toSocket(channel).getKeepAlive();
        }
        public void setKeepAlive(Channel channelboolean bthrows IOException {
            toSocket(channel).setKeepAlive(b);
        }
        public boolean getTcpNoDelay(Channel channelthrows IOException {
            return toSocket(channel).getTcpNoDelay();
        }
        public void setTcpNoDelay(Channel channelboolean bthrows IOException {
            toSocket(channel).setTcpNoDelay(b);
        }
        public SocketAddress getRemoteSocketAddress(Channel channel) {
            return toSocket(channel).getRemoteSocketAddress();
        }
        public SocketAddress getLocalSocketAddress(Channel channel) {
            return toSocket(channel).getLocalSocketAddress();
        }
        public void shutdownInput(Channel channel)throws IOException {
            toSocket(channel).shutdownInput();
        }
        public void shutdownOutput(Channel channel)throws IOException {
            toSocket(channel).shutdownOutput();
        }
    },
    SERVER(.) {
        private ServerSocket toSocket(Channel channel) {
            return ((ServerSocketChannel)channel).socket();
        }
        public int getSoTimeout(Channel channelthrows IOException {
            return toSocket(channel).getSoTimeout();
        }
        public void setSoTimeout(Channel channelint timeoutthrows IOException {
            toSocket(channel).setSoTimeout(timeout);
        }
        public boolean getReuseAddress(Channel channelthrows IOException {
            return toSocket(channel).getReuseAddress();
        }
        public void setReuseAddress(Channel channelboolean reusethrows IOException {
            toSocket(channel).setReuseAddress(reuse);
        }
        public int getSendBufferSize(Channel channelthrows IOException {
            return 0;
        }
        public void setSendBufferSize(Channel channelint sizethrows IOException {
        }
        public int getReceiveBufferSize(Channel channelthrows IOException {
            return toSocket(channel).getReceiveBufferSize();
        }
        public void setReceiveBufferSize(Channel channelint ithrows IOException {
            toSocket(channel).setReceiveBufferSize(i);
        }
        public SocketAddress getLocalSocketAddress(Channel channel) {
            return toSocket(channel).getLocalSocketAddress();
        }
    },
    DATAGRAM(.) {
        private DatagramSocket toSocket(Channel channel) {
            return ((DatagramChannel)channel).socket();
        }
        
        public int getSoTimeout(Channel channelthrows IOException {
            return toSocket(channel).getSoTimeout();
        }
        public void setSoTimeout(Channel channelint timeoutthrows IOException {
            toSocket(channel).setSoTimeout(timeout);
        }
        public boolean getReuseAddress(Channel channelthrows IOException {
            return toSocket(channel).getReuseAddress();
        }
        public void setReuseAddress(Channel channelboolean reusethrows IOException {
            toSocket(channel).setReuseAddress(reuse);
        }
        public int getSendBufferSize(Channel channelthrows IOException {
            return toSocket(channel).getSendBufferSize();
        }
        public void setSendBufferSize(Channel channelint sizethrows IOException {
            toSocket(channel).setSendBufferSize(size);
        }
        public int getReceiveBufferSize(Channel channelthrows IOException {
            return toSocket(channel).getReceiveBufferSize();
        }
        public void setReceiveBufferSize(Channel channelint ithrows IOException {
            toSocket(channel).setReceiveBufferSize(i);
        }
        public boolean getBroadcast(Channel channelthrows IOException {
            return toSocket(channel).getBroadcast();
        }
        public void setBroadcast(Channel channelboolean bthrows IOException {
            toSocket(channel).setBroadcast(b);
        }
        public SocketAddress getRemoteSocketAddress(Channel channel) {
            return toSocket(channel).getRemoteSocketAddress();
        }
        public SocketAddress getLocalSocketAddress(Channel channel) {
            return toSocket(channel).getLocalSocketAddress();
        }
    },
    UNIX(.) {
        private UnixSocketChannel toSocket(Channel channel) {
            return (UnixSocketChannel)channel;
        }
        public void shutdownInput(Channel channel)throws IOException {
            toSocket(channel).shutdownInput();
        }
        public void shutdownOutput(Channel channelthrows IOException {
            toSocket(channel).shutdownOutput();
        }
    },
    UNKNOWN(.);
    
    public static SocketType forChannel(Channel channel) {
        if (channel instanceof SocketChannel) {
            return ;
            
        } else if (channel instanceof ServerSocketChannel) {
            return ;
        
        } else if (channel instanceof DatagramChannel) {
            return ;
        } else if (channel instanceof UnixSocketChannel) {
            return ;
        }
        return ;
    }
    public int getSoTimeout(Channel channelthrows IOException { return 0; }
    public void setSoTimeout(Channel channelint timeoutthrows IOException {}
    public boolean getReuseAddress(Channel channelthrows IOException { return false; }
    public void setReuseAddress(Channel channelboolean reusethrows IOException {}
    public int getSendBufferSize(Channel channelthrows IOException { return 0; }
    public void setSendBufferSize(Channel channelint sizethrows IOException {}
    public int getReceiveBufferSize(Channel channelthrows IOException { return 0; }
    public void setReceiveBufferSize(Channel channelint ithrows IOException {}
    public boolean getOOBInline(Channel channelthrows IOException { return false; }
    public void setOOBInline(Channel channelboolean bthrows IOException {}
    public int getSoLinger(Channel channelthrows IOException { return 0; }
    public void setSoLinger(Channel channelboolean bint ithrows IOException {}
    public boolean getKeepAlive(Channel channelthrows IOException { return false; }
    public void setKeepAlive(Channel channelboolean bthrows IOException {}
    public boolean getTcpNoDelay(Channel channelthrows IOException { return false; }
    public void setTcpNoDelay(Channel channelboolean bthrows IOException {}
    public boolean getBroadcast(Channel channelthrows IOException { return false; }
    public void setBroadcast(Channel channelboolean bthrows IOException {}
    public void shutdownInput(Channel channelthrows IOException {}
    public void shutdownOutput(Channel channelthrows IOException {}
    public SocketAddress getRemoteSocketAddress(Channel channel) {
        return null;
    }
    public SocketAddress getLocalSocketAddress(Channel channel) {
        return null;
    }
    public Sock getSocketType() {
        return ;
    }
    
    public int getSocketOption(Channel channelSocketOption optionthrows IOException {
        switch (option) {
            case :
                return getBroadcast(channel) ? 1 : 0;
            case :
                return getKeepAlive(channel) ? 1 : 0;
            case : {
                int linger = getSoLinger(channel);
                return linger < 0 ? 0 : linger;
            }
            case :
                return getOOBInline(channel) ? 1 : 0;
            case :
                return getReceiveBufferSize(channel);
            case :
                return getReuseAddress(channel) ? 1 : 0;
            case :
                return getSendBufferSize(channel);
            case :
            case :
                return getSoTimeout(channel);
            case :
                return getSocketType().intValue();
            // Can't support the rest with Java
            case :
                return 1;
            case :
                return 2048;
            case :
            case :
            case :
            case :
                return 0;
        }
        return 0;
    }
    public void setSocketOption(Channel channelSocketOption optionint valuethrows IOException {
        switch (option) {
            case :
                setBroadcast(channelasBoolean(value));
                break;
            case :
                setKeepAlive(channelasBoolean(value));
                break;
            case :
                setSoLinger(channelvalue <= 0, value);
                break;
            case :
                setOOBInline(channelasBoolean(value));
                break;
            case :
                setReceiveBufferSize(channelvalue);
                break;
            case :
                setReuseAddress(channelasBoolean(value));
                break;
            case :
                setSendBufferSize(channelvalue);
                break;
            case :
            case :
                setSoTimeout(channelvalue);
                break;
            // can't set these
            case :
            case :
            case :
            case :
            case :
            case :
            case :
        }
    }
    private static boolean asBoolean(int value) {
        return value == 0 ? false : true;
    }
    private SocketType(Sock sock) {
        this. = sock;
    }
    private final Sock sock;
New to GrepCode? Check out our FAQ X