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.session.AbstractIoSessionConfig;
023import org.apache.mina.core.session.IoSessionConfig;
024
025/**
026 * The TCP transport session configuration.
027 * 
028 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
029 */
030public abstract class AbstractSocketSessionConfig extends AbstractIoSessionConfig implements SocketSessionConfig {
031    /**
032     * {@inheritDoc}
033     */
034    @Override
035    public void setAll(IoSessionConfig config) {
036        super.setAll(config);
037        
038        if (!(config instanceof SocketSessionConfig)) {
039            return;
040        }
041
042        if (config instanceof AbstractSocketSessionConfig) {
043            // Minimize unnecessary system calls by checking all 'propertyChanged' properties.
044            AbstractSocketSessionConfig cfg = (AbstractSocketSessionConfig) config;
045            if (cfg.isKeepAliveChanged()) {
046                setKeepAlive(cfg.isKeepAlive());
047            }
048            if (cfg.isOobInlineChanged()) {
049                setOobInline(cfg.isOobInline());
050            }
051            if (cfg.isReceiveBufferSizeChanged()) {
052                setReceiveBufferSize(cfg.getReceiveBufferSize());
053            }
054            if (cfg.isReuseAddressChanged()) {
055                setReuseAddress(cfg.isReuseAddress());
056            }
057            if (cfg.isSendBufferSizeChanged()) {
058                setSendBufferSize(cfg.getSendBufferSize());
059            }
060            if (cfg.isSoLingerChanged()) {
061                setSoLinger(cfg.getSoLinger());
062            }
063            if (cfg.isTcpNoDelayChanged()) {
064                setTcpNoDelay(cfg.isTcpNoDelay());
065            }
066            if (cfg.isTrafficClassChanged() && getTrafficClass() != cfg.getTrafficClass()) {
067                setTrafficClass(cfg.getTrafficClass());
068            }
069        } else {
070            SocketSessionConfig cfg = (SocketSessionConfig) config;
071            setKeepAlive(cfg.isKeepAlive());
072            setOobInline(cfg.isOobInline());
073            setReceiveBufferSize(cfg.getReceiveBufferSize());
074            setReuseAddress(cfg.isReuseAddress());
075            setSendBufferSize(cfg.getSendBufferSize());
076            setSoLinger(cfg.getSoLinger());
077            setTcpNoDelay(cfg.isTcpNoDelay());
078            if (getTrafficClass() != cfg.getTrafficClass()) {
079                setTrafficClass(cfg.getTrafficClass());
080            }
081        }
082    }
083
084    /**
085     * @return <tt>true</tt> if and only if the <tt>keepAlive</tt> property
086     * has been changed by its setter method.  The system call related with
087     * the property is made only when this method returns <tt>true</tt>.  By
088     * default, this method always returns <tt>true</tt> to simplify implementation
089     * of subclasses, but overriding the default behavior is always encouraged.
090     */
091    protected boolean isKeepAliveChanged() {
092        return true;
093    }
094
095    /**
096     * @return <tt>true</tt> if and only if the <tt>oobInline</tt> property
097     * has been changed by its setter method.  The system call related with
098     * the property is made only when this method returns <tt>true</tt>.  By
099     * default, this method always returns <tt>true</tt> to simplify implementation
100     * of subclasses, but overriding the default behavior is always encouraged.
101     */
102    protected boolean isOobInlineChanged() {
103        return true;
104    }
105
106    /**
107     * @return <tt>true</tt> if and only if the <tt>receiveBufferSize</tt> property
108     * has been changed by its setter method.  The system call related with
109     * the property is made only when this method returns <tt>true</tt>.  By
110     * default, this method always returns <tt>true</tt> to simplify implementation
111     * of subclasses, but overriding the default behavior is always encouraged.
112     */
113    protected boolean isReceiveBufferSizeChanged() {
114        return true;
115    }
116
117    /**
118     * @return <tt>true</tt> if and only if the <tt>reuseAddress</tt> property
119     * has been changed by its setter method.  The system call related with
120     * the property is made only when this method returns <tt>true</tt>.  By
121     * default, this method always returns <tt>true</tt> to simplify implementation
122     * of subclasses, but overriding the default behavior is always encouraged.
123     */
124    protected boolean isReuseAddressChanged() {
125        return true;
126    }
127
128    /**
129     * @return <tt>true</tt> if and only if the <tt>sendBufferSize</tt> property
130     * has been changed by its setter method.  The system call related with
131     * the property is made only when this method returns <tt>true</tt>.  By
132     * default, this method always returns <tt>true</tt> to simplify implementation
133     * of subclasses, but overriding the default behavior is always encouraged.
134     */
135    protected boolean isSendBufferSizeChanged() {
136        return true;
137    }
138
139    /**
140     * @return <tt>true</tt> if and only if the <tt>soLinger</tt> property
141     * has been changed by its setter method.  The system call related with
142     * the property is made only when this method returns <tt>true</tt>.  By
143     * default, this method always returns <tt>true</tt> to simplify implementation
144     * of subclasses, but overriding the default behavior is always encouraged.
145     */
146    protected boolean isSoLingerChanged() {
147        return true;
148    }
149
150    /**
151     * @return <tt>true</tt> if and only if the <tt>tcpNoDelay</tt> property
152     * has been changed by its setter method.  The system call related with
153     * the property is made only when this method returns <tt>true</tt>.  By
154     * default, this method always returns <tt>true</tt> to simplify implementation
155     * of subclasses, but overriding the default behavior is always encouraged.
156     */
157    protected boolean isTcpNoDelayChanged() {
158        return true;
159    }
160
161    /**
162     * @return <tt>true</tt> if and only if the <tt>trafficClass</tt> property
163     * has been changed by its setter method.  The system call related with
164     * the property is made only when this method returns <tt>true</tt>.  By
165     * default, this method always returns <tt>true</tt> to simplify implementation
166     * of subclasses, but overriding the default behavior is always encouraged.
167     */
168    protected boolean isTrafficClassChanged() {
169        return true;
170    }
171}