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 029 private int minReadBufferSize = 64; 030 031 private int readBufferSize = 2048; 032 033 private int maxReadBufferSize = 65536; 034 035 private int idleTimeForRead; 036 037 private int idleTimeForWrite; 038 039 private int idleTimeForBoth; 040 041 private int writeTimeout = 60; 042 043 private boolean useReadOperation; 044 045 private int throughputCalculationInterval = 3; 046 047 protected AbstractIoSessionConfig() { 048 // Do nothing 049 } 050 051 /** 052 * {@inheritDoc} 053 */ 054 public final void setAll(IoSessionConfig config) { 055 if (config == null) { 056 throw new IllegalArgumentException("config"); 057 } 058 059 setReadBufferSize(config.getReadBufferSize()); 060 setMinReadBufferSize(config.getMinReadBufferSize()); 061 setMaxReadBufferSize(config.getMaxReadBufferSize()); 062 setIdleTime(IdleStatus.BOTH_IDLE, config.getIdleTime(IdleStatus.BOTH_IDLE)); 063 setIdleTime(IdleStatus.READER_IDLE, config.getIdleTime(IdleStatus.READER_IDLE)); 064 setIdleTime(IdleStatus.WRITER_IDLE, config.getIdleTime(IdleStatus.WRITER_IDLE)); 065 setWriteTimeout(config.getWriteTimeout()); 066 setUseReadOperation(config.isUseReadOperation()); 067 setThroughputCalculationInterval(config.getThroughputCalculationInterval()); 068 069 doSetAll(config); 070 } 071 072 /** 073 * Implement this method to set all transport-specific configuration 074 * properties retrieved from the specified <tt>config</tt>. 075 * 076 * @param config the {@link IoSessionConfig} to set 077 */ 078 protected abstract void doSetAll(IoSessionConfig config); 079 080 /** 081 * {@inheritDoc} 082 */ 083 public int getReadBufferSize() { 084 return readBufferSize; 085 } 086 087 /** 088 * {@inheritDoc} 089 */ 090 public void setReadBufferSize(int readBufferSize) { 091 if (readBufferSize <= 0) { 092 throw new IllegalArgumentException("readBufferSize: " + readBufferSize + " (expected: 1+)"); 093 } 094 this.readBufferSize = readBufferSize; 095 } 096 097 /** 098 * {@inheritDoc} 099 */ 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}