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  
29      private int minReadBufferSize = 64;
30  
31      private int readBufferSize = 2048;
32  
33      private int maxReadBufferSize = 65536;
34  
35      private int idleTimeForRead;
36  
37      private int idleTimeForWrite;
38  
39      private int idleTimeForBoth;
40  
41      private int writeTimeout = 60;
42  
43      private boolean useReadOperation;
44  
45      private int throughputCalculationInterval = 3;
46  
47      protected AbstractIoSessionConfig() {
48          // Do nothing
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      public final void setAll(IoSessionConfig config) {
55          if (config == null) {
56              throw new IllegalArgumentException("config");
57          }
58  
59          setReadBufferSize(config.getReadBufferSize());
60          setMinReadBufferSize(config.getMinReadBufferSize());
61          setMaxReadBufferSize(config.getMaxReadBufferSize());
62          setIdleTime(IdleStatus.BOTH_IDLE, config.getIdleTime(IdleStatus.BOTH_IDLE));
63          setIdleTime(IdleStatus.READER_IDLE, config.getIdleTime(IdleStatus.READER_IDLE));
64          setIdleTime(IdleStatus.WRITER_IDLE, config.getIdleTime(IdleStatus.WRITER_IDLE));
65          setWriteTimeout(config.getWriteTimeout());
66          setUseReadOperation(config.isUseReadOperation());
67          setThroughputCalculationInterval(config.getThroughputCalculationInterval());
68  
69          doSetAll(config);
70      }
71  
72      /**
73       * Implement this method to set all transport-specific configuration
74       * properties retrieved from the specified <tt>config</tt>.
75       * 
76       * @param config the {@link IoSessionConfig} to set
77       */
78      protected abstract void doSetAll(IoSessionConfig config);
79  
80      /**
81       * {@inheritDoc}
82       */
83      public int getReadBufferSize() {
84          return readBufferSize;
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      public void setReadBufferSize(int readBufferSize) {
91          if (readBufferSize <= 0) {
92              throw new IllegalArgumentException("readBufferSize: " + readBufferSize + " (expected: 1+)");
93          }
94          this.readBufferSize = readBufferSize;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public int getMinReadBufferSize() {
101         return minReadBufferSize;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     public void setMinReadBufferSize(int minReadBufferSize) {
108         if (minReadBufferSize <= 0) {
109             throw new IllegalArgumentException("minReadBufferSize: " + minReadBufferSize + " (expected: 1+)");
110         }
111         if (minReadBufferSize > maxReadBufferSize) {
112             throw new IllegalArgumentException("minReadBufferSize: " + minReadBufferSize + " (expected: smaller than "
113                     + maxReadBufferSize + ')');
114 
115         }
116         this.minReadBufferSize = minReadBufferSize;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     public int getMaxReadBufferSize() {
123         return maxReadBufferSize;
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     public void setMaxReadBufferSize(int maxReadBufferSize) {
130         if (maxReadBufferSize <= 0) {
131             throw new IllegalArgumentException("maxReadBufferSize: " + maxReadBufferSize + " (expected: 1+)");
132         }
133 
134         if (maxReadBufferSize < minReadBufferSize) {
135             throw new IllegalArgumentException("maxReadBufferSize: " + maxReadBufferSize + " (expected: greater than "
136                     + minReadBufferSize + ')');
137 
138         }
139         this.maxReadBufferSize = maxReadBufferSize;
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     public int getIdleTime(IdleStatus status) {
146         if (status == IdleStatus.BOTH_IDLE) {
147             return idleTimeForBoth;
148         }
149 
150         if (status == IdleStatus.READER_IDLE) {
151             return idleTimeForRead;
152         }
153 
154         if (status == IdleStatus.WRITER_IDLE) {
155             return idleTimeForWrite;
156         }
157 
158         throw new IllegalArgumentException("Unknown idle status: " + status);
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     public long getIdleTimeInMillis(IdleStatus status) {
165         return getIdleTime(status) * 1000L;
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     public void setIdleTime(IdleStatus status, int idleTime) {
172         if (idleTime < 0) {
173             throw new IllegalArgumentException("Illegal idle time: " + idleTime);
174         }
175 
176         if (status == IdleStatus.BOTH_IDLE) {
177             idleTimeForBoth = idleTime;
178         } else if (status == IdleStatus.READER_IDLE) {
179             idleTimeForRead = idleTime;
180         } else if (status == IdleStatus.WRITER_IDLE) {
181             idleTimeForWrite = idleTime;
182         } else {
183             throw new IllegalArgumentException("Unknown idle status: " + status);
184         }
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     public final int getBothIdleTime() {
191         return getIdleTime(IdleStatus.BOTH_IDLE);
192     }
193 
194     /**
195      * {@inheritDoc}
196      */
197     public final long getBothIdleTimeInMillis() {
198         return getIdleTimeInMillis(IdleStatus.BOTH_IDLE);
199     }
200 
201     /**
202      * {@inheritDoc}
203      */
204     public final int getReaderIdleTime() {
205         return getIdleTime(IdleStatus.READER_IDLE);
206     }
207 
208     /**
209      * {@inheritDoc}
210      */
211     public final long getReaderIdleTimeInMillis() {
212         return getIdleTimeInMillis(IdleStatus.READER_IDLE);
213     }
214 
215     /**
216      * {@inheritDoc}
217      */
218     public final int getWriterIdleTime() {
219         return getIdleTime(IdleStatus.WRITER_IDLE);
220     }
221 
222     /**
223      * {@inheritDoc}
224      */
225     public final long getWriterIdleTimeInMillis() {
226         return getIdleTimeInMillis(IdleStatus.WRITER_IDLE);
227     }
228 
229     /**
230      * {@inheritDoc}
231      */
232     public void setBothIdleTime(int idleTime) {
233         setIdleTime(IdleStatus.BOTH_IDLE, idleTime);
234     }
235 
236     /**
237      * {@inheritDoc}
238      */
239     public void setReaderIdleTime(int idleTime) {
240         setIdleTime(IdleStatus.READER_IDLE, idleTime);
241     }
242 
243     /**
244      * {@inheritDoc}
245      */
246     public void setWriterIdleTime(int idleTime) {
247         setIdleTime(IdleStatus.WRITER_IDLE, idleTime);
248     }
249 
250     /**
251      * {@inheritDoc}
252      */
253     public int getWriteTimeout() {
254         return writeTimeout;
255     }
256 
257     /**
258      * {@inheritDoc}
259      */
260     public long getWriteTimeoutInMillis() {
261         return writeTimeout * 1000L;
262     }
263 
264     /**
265      * {@inheritDoc}
266      */
267     public void setWriteTimeout(int writeTimeout) {
268         if (writeTimeout < 0) {
269             throw new IllegalArgumentException("Illegal write timeout: " + writeTimeout);
270         }
271         this.writeTimeout = writeTimeout;
272     }
273 
274     /**
275      * {@inheritDoc}
276      */
277     public boolean isUseReadOperation() {
278         return useReadOperation;
279     }
280 
281     /**
282      * {@inheritDoc}
283      */
284     public void setUseReadOperation(boolean useReadOperation) {
285         this.useReadOperation = useReadOperation;
286     }
287 
288     /**
289      * {@inheritDoc}
290      */
291     public int getThroughputCalculationInterval() {
292         return throughputCalculationInterval;
293     }
294 
295     /**
296      * {@inheritDoc}
297      */
298     public void setThroughputCalculationInterval(int throughputCalculationInterval) {
299         if (throughputCalculationInterval < 0) {
300             throw new IllegalArgumentException("throughputCalculationInterval: " + throughputCalculationInterval);
301         }
302 
303         this.throughputCalculationInterval = throughputCalculationInterval;
304     }
305 
306     /**
307      * {@inheritDoc}
308      */
309     public long getThroughputCalculationIntervalInMillis() {
310         return throughputCalculationInterval * 1000L;
311     }
312 }