001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.transport.socket;
021
022import org.apache.mina.core.service.IoService;
023
024/**
025 * A default implementation of {@link SocketSessionConfig}.
026 *
027 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
028 */
029public class DefaultSocketSessionConfig extends AbstractSocketSessionConfig {
030    private static final boolean DEFAULT_REUSE_ADDRESS = false;
031
032    private static final int DEFAULT_TRAFFIC_CLASS = 0;
033
034    private static final boolean DEFAULT_KEEP_ALIVE = false;
035
036    private static final boolean DEFAULT_OOB_INLINE = false;
037
038    private static final int DEFAULT_SO_LINGER = -1;
039
040    private static final boolean DEFAULT_TCP_NO_DELAY = false;
041
042    protected IoService parent;
043
044    private boolean defaultReuseAddress;
045
046    private boolean reuseAddress;
047
048    /* The SO_RCVBUF parameter. Set to -1 (ie, will default to OS default) */
049    private int receiveBufferSize = -1;
050
051    /* The SO_SNDBUF parameter. Set to -1 (ie, will default to OS default) */
052    private int sendBufferSize = -1;
053
054    private int trafficClass = DEFAULT_TRAFFIC_CLASS;
055
056    private boolean keepAlive = DEFAULT_KEEP_ALIVE;
057
058    private boolean oobInline = DEFAULT_OOB_INLINE;
059
060    private int soLinger = DEFAULT_SO_LINGER;
061
062    private boolean tcpNoDelay = DEFAULT_TCP_NO_DELAY;
063
064    /**
065     * Creates a new instance.
066     */
067    public DefaultSocketSessionConfig() {
068        // Do nothing
069    }
070
071    /**
072     * Initialize this configuration.
073     * 
074     * @param parent The parent IoService.
075     */
076    public void init(IoService parent) {
077        this.parent = parent;
078
079        if (parent instanceof SocketAcceptor) {
080            defaultReuseAddress = true;
081        } else {
082            defaultReuseAddress = DEFAULT_REUSE_ADDRESS;
083        }
084
085        reuseAddress = defaultReuseAddress;
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public boolean isReuseAddress() {
093        return reuseAddress;
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    @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}