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