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