View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.session;
21  
22  /**
23   * A base implementation of {@link IoSessionConfig}.
24   *
25   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
26   */
27  public abstract class AbstractIoSessionConfig implements IoSessionConfig {
28      /** The minimum size of the buffer used to read incoming data */
29      private int minReadBufferSize = 64;
30      
31      /** The default size of the buffer used to read incoming data */
32      private int readBufferSize = 2048;
33  
34      /** The maximum size of the buffer used to read incoming data */
35      private int maxReadBufferSize = 65536;
36  
37      /** The delay before we notify a session that it has been idle on read. Default to infinite */
38      private int idleTimeForRead;
39  
40      /** The delay before we notify a session that it has been idle on write. Default to infinite */
41      private int idleTimeForWrite;
42  
43      /** 
44       * The delay before we notify a session that it has been idle on read and write. 
45       * Default to infinite 
46       **/
47      private int idleTimeForBoth;
48  
49      /** The delay to wait for a write operation to complete before bailing out */
50      private int writeTimeout = 60;
51  
52      /** A flag set to true when weallow the application to do a session.read(). Default to false */
53      private boolean useReadOperation;
54  
55      private int throughputCalculationInterval = 3;
56  
57      protected AbstractIoSessionConfig() {
58          // Do nothing
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public void setAll(IoSessionConfig config) {
66          if (config == null) {
67              throw new IllegalArgumentException("config");
68          }
69  
70          setReadBufferSize(config.getReadBufferSize());
71          setMaxReadBufferSize(config.getMaxReadBufferSize());
72          setMinReadBufferSize(config.getMinReadBufferSize());
73          setIdleTime(IdleStatus.BOTH_IDLE, config.getIdleTime(IdleStatus.BOTH_IDLE));
74          setIdleTime(IdleStatus.READER_IDLE, config.getIdleTime(IdleStatus.READER_IDLE));
75          setIdleTime(IdleStatus.WRITER_IDLE, config.getIdleTime(IdleStatus.WRITER_IDLE));
76          setWriteTimeout(config.getWriteTimeout());
77          setUseReadOperation(config.isUseReadOperation());
78          setThroughputCalculationInterval(config.getThroughputCalculationInterval());
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public int getReadBufferSize() {
86          return readBufferSize;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public void setReadBufferSize(int readBufferSize) {
94          if (readBufferSize <= 0) {
95              throw new IllegalArgumentException("readBufferSize: " + readBufferSize + " (expected: 1+)");
96          }
97          this.readBufferSize = readBufferSize;
98      }
99  
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 }