View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the 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 org.jboss.netty.channel.ChannelException;
19  import org.jboss.netty.channel.DefaultChannelConfig;
20  import org.jboss.netty.util.internal.ConversionUtil;
21  
22  import java.net.Socket;
23  import java.net.SocketException;
24  
25  /**
26   * The default {@link SocketChannelConfig} implementation.
27   */
28  public class DefaultSocketChannelConfig extends DefaultChannelConfig
29                                          implements SocketChannelConfig {
30  
31      private final Socket socket;
32  
33      /**
34       * Creates a new instance.
35       */
36      public DefaultSocketChannelConfig(Socket socket) {
37          if (socket == null) {
38              throw new NullPointerException("socket");
39          }
40          this.socket = socket;
41      }
42  
43      @Override
44      public boolean setOption(String key, Object value) {
45          if (super.setOption(key, value)) {
46              return true;
47          }
48  
49          if ("receiveBufferSize".equals(key)) {
50              setReceiveBufferSize(ConversionUtil.toInt(value));
51          } else if ("sendBufferSize".equals(key)) {
52              setSendBufferSize(ConversionUtil.toInt(value));
53          } else if ("tcpNoDelay".equals(key)) {
54              setTcpNoDelay(ConversionUtil.toBoolean(value));
55          } else if ("keepAlive".equals(key)) {
56              setKeepAlive(ConversionUtil.toBoolean(value));
57          } else if ("reuseAddress".equals(key)) {
58              setReuseAddress(ConversionUtil.toBoolean(value));
59          } else if ("soLinger".equals(key)) {
60              setSoLinger(ConversionUtil.toInt(value));
61          } else if ("trafficClass".equals(key)) {
62              setTrafficClass(ConversionUtil.toInt(value));
63          } else {
64              return false;
65          }
66          return true;
67      }
68  
69      public int getReceiveBufferSize() {
70          try {
71              return socket.getReceiveBufferSize();
72          } catch (SocketException e) {
73              throw new ChannelException(e);
74          }
75      }
76  
77      public int getSendBufferSize() {
78          try {
79              return socket.getSendBufferSize();
80          } catch (SocketException e) {
81              throw new ChannelException(e);
82          }
83      }
84  
85      public int getSoLinger() {
86          try {
87              return socket.getSoLinger();
88          } catch (SocketException e) {
89              throw new ChannelException(e);
90          }
91      }
92  
93      public int getTrafficClass() {
94          try {
95              return socket.getTrafficClass();
96          } catch (SocketException e) {
97              throw new ChannelException(e);
98          }
99      }
100 
101     public boolean isKeepAlive() {
102         try {
103             return socket.getKeepAlive();
104         } catch (SocketException e) {
105             throw new ChannelException(e);
106         }
107     }
108 
109     public boolean isReuseAddress() {
110         try {
111             return socket.getReuseAddress();
112         } catch (SocketException e) {
113             throw new ChannelException(e);
114         }
115     }
116 
117     public boolean isTcpNoDelay() {
118         try {
119             return socket.getTcpNoDelay();
120         } catch (SocketException e) {
121             throw new ChannelException(e);
122         }
123     }
124 
125     public void setKeepAlive(boolean keepAlive) {
126         try {
127             socket.setKeepAlive(keepAlive);
128         } catch (SocketException e) {
129             throw new ChannelException(e);
130         }
131     }
132 
133     public void setPerformancePreferences(
134             int connectionTime, int latency, int bandwidth) {
135         socket.setPerformancePreferences(connectionTime, latency, bandwidth);
136     }
137 
138     public void setReceiveBufferSize(int receiveBufferSize) {
139         try {
140             socket.setReceiveBufferSize(receiveBufferSize);
141         } catch (SocketException e) {
142             throw new ChannelException(e);
143         }
144     }
145 
146     public void setReuseAddress(boolean reuseAddress) {
147         try {
148             socket.setReuseAddress(reuseAddress);
149         } catch (SocketException e) {
150             throw new ChannelException(e);
151         }
152     }
153 
154     public void setSendBufferSize(int sendBufferSize) {
155         try {
156             socket.setSendBufferSize(sendBufferSize);
157         } catch (SocketException e) {
158             throw new ChannelException(e);
159         }
160     }
161 
162     public void setSoLinger(int soLinger) {
163         try {
164             if (soLinger < 0) {
165                 socket.setSoLinger(false, 0);
166             } else {
167                 socket.setSoLinger(true, soLinger);
168             }
169         } catch (SocketException e) {
170             throw new ChannelException(e);
171         }
172     }
173 
174     public void setTcpNoDelay(boolean tcpNoDelay) {
175         try {
176             socket.setTcpNoDelay(tcpNoDelay);
177         } catch (SocketException e) {
178             throw new ChannelException(e);
179         }
180     }
181 
182     public void setTrafficClass(int trafficClass) {
183         try {
184             socket.setTrafficClass(trafficClass);
185         } catch (SocketException e) {
186             throw new ChannelException(e);
187         }
188     }
189 }