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 com.sun.nio.sctp.SctpStandardSocketOptions.InitMaxStreams;
21  import io.netty.buffer.ByteBufAllocator;
22  import io.netty.channel.ChannelException;
23  import io.netty.channel.ChannelOption;
24  import io.netty.channel.DefaultChannelConfig;
25  import io.netty.channel.MessageSizeEstimator;
26  import io.netty.channel.RecvByteBufAllocator;
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          return super.getOption(option);
69      }
70  
71      @Override
72      public <T> boolean setOption(ChannelOption<T> option, T value) {
73          validate(option, value);
74  
75          if (option == ChannelOption.SO_RCVBUF) {
76              setReceiveBufferSize((Integer) value);
77          } else if (option == ChannelOption.SO_SNDBUF) {
78              setSendBufferSize((Integer) value);
79          } else if (option == SctpChannelOption.SCTP_INIT_MAXSTREAMS) {
80              setInitMaxStreams((InitMaxStreams) value);
81          } else {
82              return super.setOption(option, value);
83          }
84  
85          return true;
86      }
87  
88      @Override
89      public int getSendBufferSize() {
90          try {
91              return javaChannel.getOption(SctpStandardSocketOptions.SO_SNDBUF);
92          } catch (IOException e) {
93              throw new ChannelException(e);
94          }
95      }
96  
97      @Override
98      public SctpServerChannelConfig setSendBufferSize(int sendBufferSize) {
99          try {
100             javaChannel.setOption(SctpStandardSocketOptions.SO_SNDBUF, sendBufferSize);
101         } catch (IOException e) {
102             throw new ChannelException(e);
103         }
104         return this;
105     }
106 
107     @Override
108     public int getReceiveBufferSize() {
109         try {
110             return javaChannel.getOption(SctpStandardSocketOptions.SO_RCVBUF);
111         } catch (IOException e) {
112             throw new ChannelException(e);
113         }
114     }
115 
116     @Override
117     public SctpServerChannelConfig setReceiveBufferSize(int receiveBufferSize) {
118         try {
119             javaChannel.setOption(SctpStandardSocketOptions.SO_RCVBUF, receiveBufferSize);
120         } catch (IOException e) {
121             throw new ChannelException(e);
122         }
123         return this;
124     }
125 
126     @Override
127     public InitMaxStreams getInitMaxStreams() {
128         try {
129             return javaChannel.getOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS);
130         } catch (IOException e) {
131             throw new ChannelException(e);
132         }
133     }
134 
135     @Override
136     public SctpServerChannelConfig setInitMaxStreams(InitMaxStreams initMaxStreams) {
137         try {
138             javaChannel.setOption(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS, initMaxStreams);
139         } catch (IOException e) {
140             throw new ChannelException(e);
141         }
142         return this;
143     }
144 
145     @Override
146     public int getBacklog() {
147         return backlog;
148     }
149 
150     @Override
151     public SctpServerChannelConfig setBacklog(int backlog) {
152         if (backlog < 0) {
153             throw new IllegalArgumentException("backlog: " + backlog);
154         }
155         this.backlog = backlog;
156         return this;
157     }
158 
159     @Override
160     public SctpServerChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
161         super.setMaxMessagesPerRead(maxMessagesPerRead);
162         return this;
163     }
164 
165     @Override
166     public SctpServerChannelConfig setWriteSpinCount(int writeSpinCount) {
167         super.setWriteSpinCount(writeSpinCount);
168         return this;
169     }
170 
171     @Override
172     public SctpServerChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
173         super.setConnectTimeoutMillis(connectTimeoutMillis);
174         return this;
175     }
176 
177     @Override
178     public SctpServerChannelConfig setAllocator(ByteBufAllocator allocator) {
179         super.setAllocator(allocator);
180         return this;
181     }
182 
183     @Override
184     public SctpServerChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
185         super.setRecvByteBufAllocator(allocator);
186         return this;
187     }
188 
189     @Override
190     public SctpServerChannelConfig setAutoRead(boolean autoRead) {
191         super.setAutoRead(autoRead);
192         return this;
193     }
194 
195     @Override
196     public SctpServerChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
197         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
198         return this;
199     }
200 
201     @Override
202     public SctpServerChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
203         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
204         return this;
205     }
206 
207     @Override
208     public SctpServerChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
209         super.setMessageSizeEstimator(estimator);
210         return this;
211     }
212 }