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