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