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 io.netty.channel.udt;
17  
18  import com.barchart.udt.OptionUDT;
19  import com.barchart.udt.SocketUDT;
20  import com.barchart.udt.nio.ChannelUDT;
21  import io.netty.buffer.ByteBufAllocator;
22  import io.netty.channel.ChannelOption;
23  import io.netty.channel.DefaultChannelConfig;
24  import io.netty.channel.MessageSizeEstimator;
25  import io.netty.channel.RecvByteBufAllocator;
26  
27  import java.io.IOException;
28  import java.util.Map;
29  
30  import static io.netty.channel.udt.UdtChannelOption.*;
31  
32  /**
33   * The default {@link UdtChannelConfig} implementation.
34   *
35   * @deprecated The UDT transport is no longer maintained and will be removed.
36   */
37  @Deprecated
38  public class DefaultUdtChannelConfig extends DefaultChannelConfig implements
39          UdtChannelConfig {
40  
41      private static final int K = 1024;
42      private static final int M = K * K;
43  
44      private volatile int protocolReceiveBufferSize = 10 * M;
45      private volatile int protocolSendBufferSize = 10 * M;
46  
47      private volatile int systemReceiveBufferSize = M;
48      private volatile int systemSendBufferSize = M;
49  
50      private volatile int allocatorReceiveBufferSize = 128 * K;
51      private volatile int allocatorSendBufferSize = 128 * K;
52  
53      private volatile int soLinger;
54  
55      private volatile boolean reuseAddress = true;
56  
57      public DefaultUdtChannelConfig(final UdtChannel channel,
58              final ChannelUDT channelUDT, final boolean apply)
59              throws IOException {
60          super(channel);
61          if (apply) {
62              apply(channelUDT);
63          }
64      }
65  
66      protected void apply(final ChannelUDT channelUDT) throws IOException {
67          final SocketUDT socketUDT = channelUDT.socketUDT();
68          socketUDT.setReuseAddress(isReuseAddress());
69          socketUDT.setSendBufferSize(getSendBufferSize());
70          if (getSoLinger() <= 0) {
71              socketUDT.setSoLinger(false, 0);
72          } else {
73              socketUDT.setSoLinger(true, getSoLinger());
74          }
75          socketUDT.setOption(OptionUDT.Protocol_Receive_Buffer_Size,
76                  getProtocolReceiveBufferSize());
77          socketUDT.setOption(OptionUDT.Protocol_Send_Buffer_Size,
78                  getProtocolSendBufferSize());
79          socketUDT.setOption(OptionUDT.System_Receive_Buffer_Size,
80                  getSystemReceiveBufferSize());
81          socketUDT.setOption(OptionUDT.System_Send_Buffer_Size,
82                  getSystemSendBufferSize());
83      }
84  
85      @Override
86      public int getProtocolReceiveBufferSize() {
87          return protocolReceiveBufferSize;
88      }
89  
90      @SuppressWarnings("unchecked")
91      @Override
92      public <T> T getOption(final ChannelOption<T> option) {
93          if (option == PROTOCOL_RECEIVE_BUFFER_SIZE) {
94              return (T) Integer.valueOf(getProtocolReceiveBufferSize());
95          }
96          if (option == PROTOCOL_SEND_BUFFER_SIZE) {
97              return (T) Integer.valueOf(getProtocolSendBufferSize());
98          }
99          if (option == SYSTEM_RECEIVE_BUFFER_SIZE) {
100             return (T) Integer.valueOf(getSystemReceiveBufferSize());
101         }
102         if (option == SYSTEM_SEND_BUFFER_SIZE) {
103             return (T) Integer.valueOf(getSystemSendBufferSize());
104         }
105         if (option == SO_RCVBUF) {
106             return (T) Integer.valueOf(getReceiveBufferSize());
107         }
108         if (option == SO_SNDBUF) {
109             return (T) Integer.valueOf(getSendBufferSize());
110         }
111         if (option == SO_REUSEADDR) {
112             return (T) Boolean.valueOf(isReuseAddress());
113         }
114         if (option == SO_LINGER) {
115             return (T) Integer.valueOf(getSoLinger());
116         }
117         return super.getOption(option);
118     }
119 
120     @Override
121     public Map<ChannelOption<?>, Object> getOptions() {
122         return getOptions(super.getOptions(), PROTOCOL_RECEIVE_BUFFER_SIZE,
123                 PROTOCOL_SEND_BUFFER_SIZE, SYSTEM_RECEIVE_BUFFER_SIZE,
124                 SYSTEM_SEND_BUFFER_SIZE, SO_RCVBUF, SO_SNDBUF, SO_REUSEADDR,
125                 SO_LINGER);
126     }
127 
128     @Override
129     public int getReceiveBufferSize() {
130         return allocatorReceiveBufferSize;
131     }
132 
133     @Override
134     public int getSendBufferSize() {
135         return allocatorSendBufferSize;
136     }
137 
138     @Override
139     public int getSoLinger() {
140         return soLinger;
141     }
142 
143     @Override
144     public boolean isReuseAddress() {
145         return reuseAddress;
146     }
147 
148     @Override
149     public UdtChannelConfig setProtocolReceiveBufferSize(final int protocolReceiveBufferSize) {
150         this.protocolReceiveBufferSize = protocolReceiveBufferSize;
151         return this;
152     }
153 
154     @Override
155     public <T> boolean setOption(final ChannelOption<T> option, final T value) {
156         validate(option, value);
157         if (option == PROTOCOL_RECEIVE_BUFFER_SIZE) {
158             setProtocolReceiveBufferSize((Integer) value);
159         } else if (option == PROTOCOL_SEND_BUFFER_SIZE) {
160             setProtocolSendBufferSize((Integer) value);
161         } else if (option == SYSTEM_RECEIVE_BUFFER_SIZE) {
162             setSystemReceiveBufferSize((Integer) value);
163         } else if (option == SYSTEM_SEND_BUFFER_SIZE) {
164             setSystemSendBufferSize((Integer) value);
165         } else if (option == SO_RCVBUF) {
166             setReceiveBufferSize((Integer) value);
167         } else if (option == SO_SNDBUF) {
168             setSendBufferSize((Integer) value);
169         } else if (option == SO_REUSEADDR) {
170             setReuseAddress((Boolean) value);
171         } else if (option == SO_LINGER) {
172             setSoLinger((Integer) value);
173         } else {
174             return super.setOption(option, value);
175         }
176         return true;
177     }
178 
179     @Override
180     public UdtChannelConfig setReceiveBufferSize(final int receiveBufferSize) {
181         allocatorReceiveBufferSize = receiveBufferSize;
182         return this;
183     }
184 
185     @Override
186     public UdtChannelConfig setReuseAddress(final boolean reuseAddress) {
187         this.reuseAddress = reuseAddress;
188         return this;
189     }
190 
191     @Override
192     public UdtChannelConfig setSendBufferSize(final int sendBufferSize) {
193         allocatorSendBufferSize = sendBufferSize;
194         return this;
195     }
196 
197     @Override
198     public UdtChannelConfig setSoLinger(final int soLinger) {
199         this.soLinger = soLinger;
200         return this;
201     }
202 
203     @Override
204     public int getSystemReceiveBufferSize() {
205         return systemReceiveBufferSize;
206     }
207 
208     @Override
209     public UdtChannelConfig setSystemSendBufferSize(
210             final int systemReceiveBufferSize) {
211         this.systemReceiveBufferSize = systemReceiveBufferSize;
212         return this;
213     }
214 
215     @Override
216     public int getProtocolSendBufferSize() {
217         return protocolSendBufferSize;
218     }
219 
220     @Override
221     public UdtChannelConfig setProtocolSendBufferSize(
222             final int protocolSendBufferSize) {
223         this.protocolSendBufferSize = protocolSendBufferSize;
224         return this;
225     }
226 
227     @Override
228     public UdtChannelConfig setSystemReceiveBufferSize(
229             final int systemSendBufferSize) {
230         this.systemSendBufferSize = systemSendBufferSize;
231         return this;
232     }
233 
234     @Override
235     public int getSystemSendBufferSize() {
236         return systemSendBufferSize;
237     }
238 
239     @Override
240     public UdtChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
241         super.setConnectTimeoutMillis(connectTimeoutMillis);
242         return this;
243     }
244 
245     @Override
246     public UdtChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
247         super.setMaxMessagesPerRead(maxMessagesPerRead);
248         return this;
249     }
250 
251     @Override
252     public UdtChannelConfig setWriteSpinCount(int writeSpinCount) {
253         super.setWriteSpinCount(writeSpinCount);
254         return this;
255     }
256 
257     @Override
258     public UdtChannelConfig setAllocator(ByteBufAllocator allocator) {
259         super.setAllocator(allocator);
260         return this;
261     }
262 
263     @Override
264     public UdtChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
265         super.setRecvByteBufAllocator(allocator);
266         return this;
267     }
268 
269     @Override
270     public UdtChannelConfig setAutoRead(boolean autoRead) {
271         super.setAutoRead(autoRead);
272         return this;
273     }
274 
275     @Override
276     public UdtChannelConfig setAutoClose(boolean autoClose) {
277         super.setAutoClose(autoClose);
278         return this;
279     }
280 
281     @Override
282     public UdtChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
283         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
284         return this;
285     }
286 
287     @Override
288     public UdtChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
289         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
290         return this;
291     }
292 
293     @Override
294     public UdtChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
295         super.setMessageSizeEstimator(estimator);
296         return this;
297     }
298 }