View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * License.  You may obtain a copy of the License at:
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.channel.socket;
17  
18  import java.net.Socket;
19  import java.net.SocketException;
20  
21  import org.jboss.netty.channel.ChannelException;
22  import org.jboss.netty.channel.DefaultChannelConfig;
23  import org.jboss.netty.util.internal.ConversionUtil;
24  
25  /**
26   * The default {@link SocketChannelConfig} implementation.
27   *
28   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
29   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
30   *
31   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
32   *
33   */
34  public class DefaultSocketChannelConfig extends DefaultChannelConfig
35                                          implements SocketChannelConfig {
36  
37      private final Socket socket;
38  
39      /**
40       * Creates a new instance.
41       */
42      public DefaultSocketChannelConfig(Socket socket) {
43          if (socket == null) {
44              throw new NullPointerException("socket");
45          }
46          this.socket = socket;
47      }
48  
49      @Override
50      public boolean setOption(String key, Object value) {
51          if (super.setOption(key, value)) {
52              return true;
53          }
54  
55          if (key.equals("receiveBufferSize")) {
56              setReceiveBufferSize(ConversionUtil.toInt(value));
57          } else if (key.equals("sendBufferSize")) {
58              setSendBufferSize(ConversionUtil.toInt(value));
59          } else if (key.equals("tcpNoDelay")) {
60              setTcpNoDelay(ConversionUtil.toBoolean(value));
61          } else if (key.equals("keepAlive")) {
62              setKeepAlive(ConversionUtil.toBoolean(value));
63          } else if (key.equals("reuseAddress")) {
64              setReuseAddress(ConversionUtil.toBoolean(value));
65          } else if (key.equals("soLinger")) {
66              setSoLinger(ConversionUtil.toInt(value));
67          } else if (key.equals("trafficClass")) {
68              setTrafficClass(ConversionUtil.toInt(value));
69          } else {
70              return false;
71          }
72          return true;
73      }
74  
75      public int getReceiveBufferSize() {
76          try {
77              return socket.getReceiveBufferSize();
78          } catch (SocketException e) {
79              throw new ChannelException(e);
80          }
81      }
82  
83      public int getSendBufferSize() {
84          try {
85              return socket.getSendBufferSize();
86          } catch (SocketException e) {
87              throw new ChannelException(e);
88          }
89      }
90  
91      public int getSoLinger() {
92          try {
93              return socket.getSoLinger();
94          } catch (SocketException e) {
95              throw new ChannelException(e);
96          }
97      }
98  
99      public int getTrafficClass() {
100         try {
101             return socket.getTrafficClass();
102         } catch (SocketException e) {
103             throw new ChannelException(e);
104         }
105     }
106 
107     public boolean isKeepAlive() {
108         try {
109             return socket.getKeepAlive();
110         } catch (SocketException e) {
111             throw new ChannelException(e);
112         }
113     }
114 
115     public boolean isReuseAddress() {
116         try {
117             return socket.getReuseAddress();
118         } catch (SocketException e) {
119             throw new ChannelException(e);
120         }
121     }
122 
123     public boolean isTcpNoDelay() {
124         try {
125             return socket.getTcpNoDelay();
126         } catch (SocketException e) {
127             throw new ChannelException(e);
128         }
129     }
130 
131     public void setKeepAlive(boolean keepAlive) {
132         try {
133             socket.setKeepAlive(keepAlive);
134         } catch (SocketException e) {
135             throw new ChannelException(e);
136         }
137     }
138 
139     public void setPerformancePreferences(
140             int connectionTime, int latency, int bandwidth) {
141         socket.setPerformancePreferences(connectionTime, latency, bandwidth);
142     }
143 
144     public void setReceiveBufferSize(int receiveBufferSize) {
145         try {
146             socket.setReceiveBufferSize(receiveBufferSize);
147         } catch (SocketException e) {
148             throw new ChannelException(e);
149         }
150     }
151 
152     public void setReuseAddress(boolean reuseAddress) {
153         try {
154             socket.setReuseAddress(reuseAddress);
155         } catch (SocketException e) {
156             throw new ChannelException(e);
157         }
158     }
159 
160     public void setSendBufferSize(int sendBufferSize) {
161         try {
162             socket.setSendBufferSize(sendBufferSize);
163         } catch (SocketException e) {
164             throw new ChannelException(e);
165         }
166     }
167 
168     public void setSoLinger(int soLinger) {
169         try {
170             if (soLinger < 0) {
171                 socket.setSoLinger(false, 0);
172             } else {
173                 socket.setSoLinger(true, soLinger);
174             }
175         } catch (SocketException e) {
176             throw new ChannelException(e);
177         }
178     }
179 
180     public void setTcpNoDelay(boolean tcpNoDelay) {
181         try {
182             socket.setTcpNoDelay(tcpNoDelay);
183         } catch (SocketException e) {
184             throw new ChannelException(e);
185         }
186     }
187 
188     public void setTrafficClass(int trafficClass) {
189         try {
190             socket.setTrafficClass(trafficClass);
191         } catch (SocketException e) {
192             throw new ChannelException(e);
193         }
194     }
195 }