View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.transport.socket;
21  
22  import org.apache.mina.core.service.IoService;
23  
24  /**
25   * A default implementation of {@link SocketSessionConfig}.
26   *
27   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
28   */
29  public class DefaultSocketSessionConfig extends AbstractSocketSessionConfig {
30      private static final boolean DEFAULT_REUSE_ADDRESS = false;
31  
32      private static final int DEFAULT_TRAFFIC_CLASS = 0;
33  
34      private static final boolean DEFAULT_KEEP_ALIVE = false;
35  
36      private static final boolean DEFAULT_OOB_INLINE = false;
37  
38      private static final int DEFAULT_SO_LINGER = -1;
39  
40      private static final boolean DEFAULT_TCP_NO_DELAY = false;
41  
42      protected IoService parent;
43  
44      private boolean defaultReuseAddress;
45  
46      private boolean reuseAddress;
47  
48      /* The SO_RCVBUF parameter. Set to -1 (ie, will default to OS default) */
49      private int receiveBufferSize = -1;
50  
51      /* The SO_SNDBUF parameter. Set to -1 (ie, will default to OS default) */
52      private int sendBufferSize = -1;
53  
54      private int trafficClass = DEFAULT_TRAFFIC_CLASS;
55  
56      private boolean keepAlive = DEFAULT_KEEP_ALIVE;
57  
58      private boolean oobInline = DEFAULT_OOB_INLINE;
59  
60      private int soLinger = DEFAULT_SO_LINGER;
61  
62      private boolean tcpNoDelay = DEFAULT_TCP_NO_DELAY;
63  
64      /**
65       * Creates a new instance.
66       */
67      public DefaultSocketSessionConfig() {
68          // Do nothing
69      }
70  
71      /**
72       * Initialize this configuration.
73       * 
74       * @param parent The parent IoService.
75       */
76      public void init(IoService parent) {
77          this.parent = parent;
78  
79          if (parent instanceof SocketAcceptor) {
80              defaultReuseAddress = true;
81          } else {
82              defaultReuseAddress = DEFAULT_REUSE_ADDRESS;
83          }
84  
85          reuseAddress = defaultReuseAddress;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public boolean isReuseAddress() {
93          return reuseAddress;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public void setReuseAddress(boolean reuseAddress) {
101         this.reuseAddress = reuseAddress;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public int getReceiveBufferSize() {
109         return receiveBufferSize;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public void setReceiveBufferSize(int receiveBufferSize) {
117         this.receiveBufferSize = receiveBufferSize;
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public int getSendBufferSize() {
125         return sendBufferSize;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public void setSendBufferSize(int sendBufferSize) {
133         this.sendBufferSize = sendBufferSize;
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public int getTrafficClass() {
141         return trafficClass;
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public void setTrafficClass(int trafficClass) {
149         this.trafficClass = trafficClass;
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
156     public boolean isKeepAlive() {
157         return keepAlive;
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public void setKeepAlive(boolean keepAlive) {
165         this.keepAlive = keepAlive;
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     @Override
172     public boolean isOobInline() {
173         return oobInline;
174     }
175 
176     /**
177      * {@inheritDoc}
178      */
179     @Override
180     public void setOobInline(boolean oobInline) {
181         this.oobInline = oobInline;
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
188     public int getSoLinger() {
189         return soLinger;
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     @Override
196     public void setSoLinger(int soLinger) {
197         this.soLinger = soLinger;
198     }
199 
200     /**
201      * {@inheritDoc}
202      */
203     @Override
204     public boolean isTcpNoDelay() {
205         return tcpNoDelay;
206     }
207 
208     /**
209      * {@inheritDoc}
210      */
211     @Override
212     public void setTcpNoDelay(boolean tcpNoDelay) {
213         this.tcpNoDelay = tcpNoDelay;
214     }
215 
216     /**
217      * {@inheritDoc}
218      */
219     @Override
220     protected boolean isKeepAliveChanged() {
221         return keepAlive != DEFAULT_KEEP_ALIVE;
222     }
223 
224     /**
225      * {@inheritDoc}
226      */
227     @Override
228     protected boolean isOobInlineChanged() {
229         return oobInline != DEFAULT_OOB_INLINE;
230     }
231 
232     /**
233      * {@inheritDoc}
234      */
235     @Override
236     protected boolean isReceiveBufferSizeChanged() {
237         return receiveBufferSize != -1;
238     }
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     protected boolean isReuseAddressChanged() {
245         return reuseAddress != defaultReuseAddress;
246     }
247 
248     /**
249      * {@inheritDoc}
250      */
251     @Override
252     protected boolean isSendBufferSizeChanged() {
253         return sendBufferSize != -1;
254     }
255 
256     /**
257      * {@inheritDoc}
258      */
259     @Override
260     protected boolean isSoLingerChanged() {
261         return soLinger != DEFAULT_SO_LINGER;
262     }
263 
264     /**
265      * {@inheritDoc}
266      */
267     @Override
268     protected boolean isTcpNoDelayChanged() {
269         return tcpNoDelay != DEFAULT_TCP_NO_DELAY;
270     }
271 
272     /**
273      * {@inheritDoc}
274      */
275     @Override
276     protected boolean isTrafficClassChanged() {
277         return trafficClass != DEFAULT_TRAFFIC_CLASS;
278     }
279 }