View Javadoc
1   /*
2   * Copyright 2011 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.sctp;
17  
18  import com.sun.nio.sctp.SctpServerChannel;
19  import com.sun.nio.sctp.SctpStandardSocketOptions;
20  import io.netty.buffer.ByteBufAllocator;
21  import io.netty.channel.ChannelException;
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  import io.netty.channel.WriteBufferWaterMark;
27  import io.netty.util.NetUtil;
28  
29  import java.io.IOException;
30  import java.util.Map;
31  
32  /**
33   * The default {@link SctpServerChannelConfig} implementation for SCTP.
34   */
35  public class DefaultSctpServerChannelConfig extends DefaultChannelConfig implements SctpServerChannelConfig {
36  
37      private final SctpServerChannel javaChannel;
38      private volatile int backlog = NetUtil.SOMAXCONN;
39  
40      /**
41       * Creates a new instance.
42       */
43      public DefaultSctpServerChannelConfig(
44              io.netty.channel.sctp.SctpServerChannel channel, SctpServerChannel javaChannel) {
45          super(channel);
46          if (javaChannel == null) {
47              throw new NullPointerException("javaChannel");
48          }
49          this.javaChannel = javaChannel;
50      }
51  
52      @Override
53      public Map<ChannelOption<?>, Object> getOptions() {
54          return getOptions(
55                  super.getOptions(),
56                  ChannelOption.SO_RCVBUF, ChannelOption.SO_SNDBUF, SctpChannelOption.SCTP_INIT_MAXSTREAMS);
57      }
58  
59      @SuppressWarnings("unchecked")
60      @Override
61      public <T> T getOption(ChannelOption<T> option) {
62          if (option == ChannelOption.SO_RCVBUF) {
63              return (T) Integer.valueOf(getReceiveBufferSize());
64          }
65          if (option == ChannelOption.SO_SNDBUF) {
66              return (T) Integer.valueOf(getSendBufferSize());
67          }
68          if (option == SctpChannelOption.SCTP_INIT_MAXSTREAMS) {
69              return (T) getInitMaxStreams();
70          }
71          return super.getOption(option);
72      }
73  
74      @Override
75      public <T> boolean setOption(ChannelOption<T> option, T value) {
76          validate(option, value);
77  
78          if (option == ChannelOption.SO_RCVBUF) {
79              setReceiveBufferSize((Integer) value);
80          } else if (option == ChannelOption.SO_SNDBUF) {
81              setSendBufferSize((Integer) value);
82          } else if (option == SctpChannelOption.SCTP_INIT_MAXSTREAMS) {
83              setInitMaxStreams((SctpStandardSocketOptions.InitMaxStreams) value);
84          } else {
85              return super.setOption(option, value);
86          }
87  
88          return true;
89      }
90  
91      @Override
92      public int getSendBufferSize() {
93          try {
94              return javaChannel.getOption(SctpStandardSocketOptions.SO_SNDBUF);
95          } catch (IOException e) {
96              throw new ChannelException(e);
97          }
98      }
99  
100     @Override
101     public SctpServerChannelConfig setSendBufferSize(int sendBufferSize) {
102         try {
103             javaChannel.setOption(SctpStandardSocketOptions.SO_SNDBUF, sendBufferSize);
104         } catch (IOException e) {
105             throw new ChannelException(e);
106         }
107         return this;
108     }
109 
110     @Override
111     public int getReceiveBufferSize() {
112         try {
113             return javaChannel.getOption(SctpStandardSocketOptions.SO_RCVBUF);
114         } catch (IOException e) {
115             throw new ChannelException(e);
116         }
117     }
118 
119     @Override
120     public SctpServerChannelConfig setReceiveBufferSize(int receiveBufferSize) {
121         try {
122             javaChannel.setOption(SctpStandardSocketOptions.SO_RCVBUF, receiveBufferSize);
123         } catch (IOException e) {
124             throw new ChannelException(e);
125         }
126         return this;
127     }
128 
129     @Override
130     public SctpStandardSocketOptions.InitMaxStreams getInitMaxStreams() {
131         try {
132             return javaChannel.getOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS);
133         } catch (IOException e) {
134             throw new ChannelException(e);
135         }
136     }
137 
138     @Override
139     public SctpServerChannelConfig setInitMaxStreams(SctpStandardSocketOptions.InitMaxStreams initMaxStreams) {
140         try {
141             javaChannel.setOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS, initMaxStreams);
142         } catch (IOException e) {
143             throw new ChannelException(e);
144         }
145         return this;
146     }
147 
148     @Override
149     public int getBacklog() {
150         return backlog;
151     }
152 
153     @Override
154     public SctpServerChannelConfig setBacklog(int backlog) {
155         if (backlog < 0) {
156             throw new IllegalArgumentException("backlog: " + backlog);
157         }
158         this.backlog = backlog;
159         return this;
160     }
161 
162     @Override
163     @Deprecated
164     public SctpServerChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
165         super.setMaxMessagesPerRead(maxMessagesPerRead);
166         return this;
167     }
168 
169     @Override
170     public SctpServerChannelConfig setWriteSpinCount(int writeSpinCount) {
171         super.setWriteSpinCount(writeSpinCount);
172         return this;
173     }
174 
175     @Override
176     public SctpServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
177         super.setConnectTimeoutMillis(connectTimeoutMillis);
178         return this;
179     }
180 
181     @Override
182     public SctpServerChannelConfig setAllocator(ByteBufAllocator allocator) {
183         super.setAllocator(allocator);
184         return this;
185     }
186 
187     @Override
188     public SctpServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
189         super.setRecvByteBufAllocator(allocator);
190         return this;
191     }
192 
193     @Override
194     public SctpServerChannelConfig setAutoRead(boolean autoRead) {
195         super.setAutoRead(autoRead);
196         return this;
197     }
198 
199     @Override
200     public SctpServerChannelConfig setAutoClose(boolean autoClose) {
201         super.setAutoClose(autoClose);
202         return this;
203     }
204 
205     @Override
206     public SctpServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
207         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
208         return this;
209     }
210 
211     @Override
212     public SctpServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
213         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
214         return this;
215     }
216 
217     @Override
218     public SctpServerChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
219         super.setWriteBufferWaterMark(writeBufferWaterMark);
220         return this;
221     }
222 
223     @Override
224     public SctpServerChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
225         super.setMessageSizeEstimator(estimator);
226         return this;
227     }
228 }