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