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.core.session;
021
022/**
023 * A base implementation of {@link IoSessionConfig}.
024 *
025 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
026 */
027public abstract class AbstractIoSessionConfig implements IoSessionConfig {
028    /** The minimum size of the buffer used to read incoming data */
029    private int minReadBufferSize = 64;
030    
031    /** The default size of the buffer used to read incoming data */
032    private int readBufferSize = 2048;
033
034    /** The maximum size of the buffer used to read incoming data */
035    private int maxReadBufferSize = 65536;
036
037    /** The delay before we notify a session that it has been idle on read. Default to infinite */
038    private int idleTimeForRead;
039
040    /** The delay before we notify a session that it has been idle on write. Default to infinite */
041    private int idleTimeForWrite;
042
043    /** 
044     * The delay before we notify a session that it has been idle on read and write. 
045     * Default to infinite 
046     **/
047    private int idleTimeForBoth;
048
049    /** The delay to wait for a write operation to complete before bailing out */
050    private int writeTimeout = 60;
051
052    /** A flag set to true when weallow the application to do a session.read(). Default to false */
053    private boolean useReadOperation;
054
055    private int throughputCalculationInterval = 3;
056
057    protected AbstractIoSessionConfig() {
058        // Do nothing
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    public void setAll(IoSessionConfig config) {
066        if (config == null) {
067            throw new IllegalArgumentException("config");
068        }
069
070        setReadBufferSize(config.getReadBufferSize());
071        setMinReadBufferSize(config.getMinReadBufferSize());
072        setMaxReadBufferSize(config.getMaxReadBufferSize());
073        setIdleTime(IdleStatus.BOTH_IDLE, config.getIdleTime(IdleStatus.BOTH_IDLE));
074        setIdleTime(IdleStatus.READER_IDLE, config.getIdleTime(IdleStatus.READER_IDLE));
075        setIdleTime(IdleStatus.WRITER_IDLE, config.getIdleTime(IdleStatus.WRITER_IDLE));
076        setWriteTimeout(config.getWriteTimeout());
077        setUseReadOperation(config.isUseReadOperation());
078        setThroughputCalculationInterval(config.getThroughputCalculationInterval());
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public int getReadBufferSize() {
086        return readBufferSize;
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public void setReadBufferSize(int readBufferSize) {
094        if (readBufferSize <= 0) {
095            throw new IllegalArgumentException("readBufferSize: " + readBufferSize + " (expected: 1+)");
096        }
097        this.readBufferSize = readBufferSize;
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public int getMinReadBufferSize() {
105        return minReadBufferSize;
106    }
107
108    /**
109     * {@inheritDoc}
110     */
111    @Override
112    public void setMinReadBufferSize(int minReadBufferSize) {
113        if (minReadBufferSize <= 0) {
114            throw new IllegalArgumentException("minReadBufferSize: " + minReadBufferSize + " (expected: 1+)");
115        }
116        if (minReadBufferSize > maxReadBufferSize) {
117            throw new IllegalArgumentException("minReadBufferSize: " + minReadBufferSize + " (expected: smaller than "
118                    + maxReadBufferSize + ')');
119
120        }
121        this.minReadBufferSize = minReadBufferSize;
122    }
123
124    /**
125     * {@inheritDoc}
126     */
127    @Override
128    public int getMaxReadBufferSize() {
129        return maxReadBufferSize;
130    }
131
132    /**
133     * {@inheritDoc}
134     */
135    @Override
136    public void setMaxReadBufferSize(int maxReadBufferSize) {
137        if (maxReadBufferSize <= 0) {
138            throw new IllegalArgumentException("maxReadBufferSize: " + maxReadBufferSize + " (expected: 1+)");
139        }
140
141        if (maxReadBufferSize < minReadBufferSize) {
142            throw new IllegalArgumentException("maxReadBufferSize: " + maxReadBufferSize + " (expected: greater than "
143                    + minReadBufferSize + ')');
144
145        }
146        this.maxReadBufferSize = maxReadBufferSize;
147    }
148
149    /**
150     * {@inheritDoc}
151     */
152    @Override
153    public int getIdleTime(IdleStatus status) {
154        if (status == IdleStatus.BOTH_IDLE) {
155            return idleTimeForBoth;
156        }
157
158        if (status == IdleStatus.READER_IDLE) {
159            return idleTimeForRead;
160        }
161
162        if (status == IdleStatus.WRITER_IDLE) {
163            return idleTimeForWrite;
164        }
165
166        throw new IllegalArgumentException("Unknown idle status: " + status);
167    }
168
169    /**
170     * {@inheritDoc}
171     */
172    @Override
173    public long getIdleTimeInMillis(IdleStatus status) {
174        return getIdleTime(status) * 1000L;
175    }
176
177    /**
178     * {@inheritDoc}
179     */
180    @Override
181    public void setIdleTime(IdleStatus status, int idleTime) {
182        if (idleTime < 0) {
183            throw new IllegalArgumentException("Illegal idle time: " + idleTime);
184        }
185
186        if (status == IdleStatus.BOTH_IDLE) {
187            idleTimeForBoth = idleTime;
188        } else if (status == IdleStatus.READER_IDLE) {
189            idleTimeForRead = idleTime;
190        } else if (status == IdleStatus.WRITER_IDLE) {
191            idleTimeForWrite = idleTime;
192        } else {
193            throw new IllegalArgumentException("Unknown idle status: " + status);
194        }
195    }
196
197    /**
198     * {@inheritDoc}
199     */
200    @Override
201    public final int getBothIdleTime() {
202        return getIdleTime(IdleStatus.BOTH_IDLE);
203    }
204
205    /**
206     * {@inheritDoc}
207     */
208    @Override
209    public final long getBothIdleTimeInMillis() {
210        return getIdleTimeInMillis(IdleStatus.BOTH_IDLE);
211    }
212
213    /**
214     * {@inheritDoc}
215     */
216    @Override
217    public final int getReaderIdleTime() {
218        return getIdleTime(IdleStatus.READER_IDLE);
219    }
220
221    /**
222     * {@inheritDoc}
223     */
224    @Override
225    public final long getReaderIdleTimeInMillis() {
226        return getIdleTimeInMillis(IdleStatus.READER_IDLE);
227    }
228
229    /**
230     * {@inheritDoc}
231     */
232    @Override
233    public final int getWriterIdleTime() {
234        return getIdleTime(IdleStatus.WRITER_IDLE);
235    }
236
237    /**
238     * {@inheritDoc}
239     */
240    @Override
241    public final long getWriterIdleTimeInMillis() {
242        return getIdleTimeInMillis(IdleStatus.WRITER_IDLE);
243    }
244
245    /**
246     * {@inheritDoc}
247     */
248    @Override
249    public void setBothIdleTime(int idleTime) {
250        setIdleTime(IdleStatus.BOTH_IDLE, idleTime);
251    }
252
253    /**
254     * {@inheritDoc}
255     */
256    @Override
257    public void setReaderIdleTime(int idleTime) {
258        setIdleTime(IdleStatus.READER_IDLE, idleTime);
259    }
260
261    /**
262     * {@inheritDoc}
263     */
264    @Override
265    public void setWriterIdleTime(int idleTime) {
266        setIdleTime(IdleStatus.WRITER_IDLE, idleTime);
267    }
268
269    /**
270     * {@inheritDoc}
271     */
272    @Override
273    public int getWriteTimeout() {
274        return writeTimeout;
275    }
276
277    /**
278     * {@inheritDoc}
279     */
280    @Override
281    public long getWriteTimeoutInMillis() {
282        return writeTimeout * 1000L;
283    }
284
285    /**
286     * {@inheritDoc}
287     */
288    @Override
289    public void setWriteTimeout(int writeTimeout) {
290        if (writeTimeout < 0) {
291            throw new IllegalArgumentException("Illegal write timeout: " + writeTimeout);
292        }
293        this.writeTimeout = writeTimeout;
294    }
295
296    /**
297     * {@inheritDoc}
298     */
299    @Override
300    public boolean isUseReadOperation() {
301        return useReadOperation;
302    }
303
304    /**
305     * {@inheritDoc}
306     */
307    @Override
308    public void setUseReadOperation(boolean useReadOperation) {
309        this.useReadOperation = useReadOperation;
310    }
311
312    /**
313     * {@inheritDoc}
314     */
315    @Override
316    public int getThroughputCalculationInterval() {
317        return throughputCalculationInterval;
318    }
319
320    /**
321     * {@inheritDoc}
322     */
323    @Override
324    public void setThroughputCalculationInterval(int throughputCalculationInterval) {
325        if (throughputCalculationInterval < 0) {
326            throw new IllegalArgumentException("throughputCalculationInterval: " + throughputCalculationInterval);
327        }
328
329        this.throughputCalculationInterval = throughputCalculationInterval;
330    }
331
332    /**
333     * {@inheritDoc}
334     */
335    @Override
336    public long getThroughputCalculationIntervalInMillis() {
337        return throughputCalculationInterval * 1000L;
338    }
339}