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 * TODO Add documentation
027 * 
028 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
029 */
030public abstract class AbstractDatagramSessionConfig extends AbstractIoSessionConfig implements DatagramSessionConfig {
031
032    private static final boolean DEFAULT_CLOSE_ON_PORT_UNREACHABLE = true;
033
034    private boolean closeOnPortUnreachable = DEFAULT_CLOSE_ON_PORT_UNREACHABLE;
035
036    protected AbstractDatagramSessionConfig() {
037        // Do nothing
038    }
039
040    @Override
041    protected void doSetAll(IoSessionConfig config) {
042        if (!(config instanceof DatagramSessionConfig)) {
043            return;
044        }
045
046        if (config instanceof AbstractDatagramSessionConfig) {
047            // Minimize unnecessary system calls by checking all 'propertyChanged' properties.
048            AbstractDatagramSessionConfig cfg = (AbstractDatagramSessionConfig) config;
049            if (cfg.isBroadcastChanged()) {
050                setBroadcast(cfg.isBroadcast());
051            }
052            if (cfg.isReceiveBufferSizeChanged()) {
053                setReceiveBufferSize(cfg.getReceiveBufferSize());
054            }
055            if (cfg.isReuseAddressChanged()) {
056                setReuseAddress(cfg.isReuseAddress());
057            }
058            if (cfg.isSendBufferSizeChanged()) {
059                setSendBufferSize(cfg.getSendBufferSize());
060            }
061            if (cfg.isTrafficClassChanged() && getTrafficClass() != cfg.getTrafficClass()) {
062                setTrafficClass(cfg.getTrafficClass());
063            }
064        } else {
065            DatagramSessionConfig cfg = (DatagramSessionConfig) config;
066            setBroadcast(cfg.isBroadcast());
067            setReceiveBufferSize(cfg.getReceiveBufferSize());
068            setReuseAddress(cfg.isReuseAddress());
069            setSendBufferSize(cfg.getSendBufferSize());
070            if (getTrafficClass() != cfg.getTrafficClass()) {
071                setTrafficClass(cfg.getTrafficClass());
072            }
073        }
074    }
075
076    /**
077     * @return <tt>true</tt> if and only if the <tt>broadcast</tt> property
078     * has been changed by its setter method.  The system call related with
079     * the property is made only when this method returns <tt>true</tt>.  By
080     * default, this method always returns <tt>true</tt> to simplify implementation
081     * of subclasses, but overriding the default behavior is always encouraged.
082     */
083    protected boolean isBroadcastChanged() {
084        return true;
085    }
086
087    /**
088     * @return <tt>true</tt> if and only if the <tt>receiveBufferSize</tt> property
089     * has been changed by its setter method.  The system call related with
090     * the property is made only when this method returns <tt>true</tt>.  By
091     * default, this method always returns <tt>true</tt> to simplify implementation
092     * of subclasses, but overriding the default behavior is always encouraged.
093     */
094    protected boolean isReceiveBufferSizeChanged() {
095        return true;
096    }
097
098    /**
099     * @return <tt>true</tt> if and only if the <tt>reuseAddress</tt> property
100     * has been changed by its setter method.  The system call related with
101     * the property is made only when this method returns <tt>true</tt>.  By
102     * default, this method always returns <tt>true</tt> to simplify implementation
103     * of subclasses, but overriding the default behavior is always encouraged.
104     */
105    protected boolean isReuseAddressChanged() {
106        return true;
107    }
108
109    /**
110     * @return <tt>true</tt> if and only if the <tt>sendBufferSize</tt> property
111     * has been changed by its setter method.  The system call related with
112     * the property is made only when this method returns <tt>true</tt>.  By
113     * default, this method always returns <tt>true</tt> to simplify implementation
114     * of subclasses, but overriding the default behavior is always encouraged.
115     */
116    protected boolean isSendBufferSizeChanged() {
117        return true;
118    }
119
120    /**
121     * @return <tt>true</tt> if and only if the <tt>trafficClass</tt> property
122     * has been changed by its setter method.  The system call related with
123     * the property is made only when this method returns <tt>true</tt>.  By
124     * default, this method always returns <tt>true</tt> to simplify implementation
125     * of subclasses, but overriding the default behavior is always encouraged.
126     */
127    protected boolean isTrafficClassChanged() {
128        return true;
129    }
130
131    /**
132     * {@inheritDoc}
133     */
134    public boolean isCloseOnPortUnreachable() {
135        return closeOnPortUnreachable;
136    }
137
138    /**
139     * {@inheritDoc}
140     */
141    public void setCloseOnPortUnreachable(boolean closeOnPortUnreachable) {
142        this.closeOnPortUnreachable = closeOnPortUnreachable;
143    }
144}