1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.core.session;
21
22
23
24
25
26
27 public abstract class AbstractIoSessionConfig implements IoSessionConfig {
28
29 private int minReadBufferSize = 64;
30
31
32 private int readBufferSize = 2048;
33
34
35 private int maxReadBufferSize = 65536;
36
37
38 private int idleTimeForRead;
39
40
41 private int idleTimeForWrite;
42
43
44
45
46
47 private int idleTimeForBoth;
48
49
50 private int writeTimeout = 60;
51
52
53 private boolean useReadOperation;
54
55 private int throughputCalculationInterval = 3;
56
57 protected AbstractIoSessionConfig() {
58
59 }
60
61
62
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
83
84 @Override
85 public int getReadBufferSize() {
86 return readBufferSize;
87 }
88
89
90
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
102
103 @Override
104 public int getMinReadBufferSize() {
105 return minReadBufferSize;
106 }
107
108
109
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
126
127 @Override
128 public int getMaxReadBufferSize() {
129 return maxReadBufferSize;
130 }
131
132
133
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
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
171
172 @Override
173 public long getIdleTimeInMillis(IdleStatus status) {
174 return getIdleTime(status) * 1000L;
175 }
176
177
178
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
199
200 @Override
201 public final int getBothIdleTime() {
202 return getIdleTime(IdleStatus.BOTH_IDLE);
203 }
204
205
206
207
208 @Override
209 public final long getBothIdleTimeInMillis() {
210 return getIdleTimeInMillis(IdleStatus.BOTH_IDLE);
211 }
212
213
214
215
216 @Override
217 public final int getReaderIdleTime() {
218 return getIdleTime(IdleStatus.READER_IDLE);
219 }
220
221
222
223
224 @Override
225 public final long getReaderIdleTimeInMillis() {
226 return getIdleTimeInMillis(IdleStatus.READER_IDLE);
227 }
228
229
230
231
232 @Override
233 public final int getWriterIdleTime() {
234 return getIdleTime(IdleStatus.WRITER_IDLE);
235 }
236
237
238
239
240 @Override
241 public final long getWriterIdleTimeInMillis() {
242 return getIdleTimeInMillis(IdleStatus.WRITER_IDLE);
243 }
244
245
246
247
248 @Override
249 public void setBothIdleTime(int idleTime) {
250 setIdleTime(IdleStatus.BOTH_IDLE, idleTime);
251 }
252
253
254
255
256 @Override
257 public void setReaderIdleTime(int idleTime) {
258 setIdleTime(IdleStatus.READER_IDLE, idleTime);
259 }
260
261
262
263
264 @Override
265 public void setWriterIdleTime(int idleTime) {
266 setIdleTime(IdleStatus.WRITER_IDLE, idleTime);
267 }
268
269
270
271
272 @Override
273 public int getWriteTimeout() {
274 return writeTimeout;
275 }
276
277
278
279
280 @Override
281 public long getWriteTimeoutInMillis() {
282 return writeTimeout * 1000L;
283 }
284
285
286
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
298
299 @Override
300 public boolean isUseReadOperation() {
301 return useReadOperation;
302 }
303
304
305
306
307 @Override
308 public void setUseReadOperation(boolean useReadOperation) {
309 this.useReadOperation = useReadOperation;
310 }
311
312
313
314
315 @Override
316 public int getThroughputCalculationInterval() {
317 return throughputCalculationInterval;
318 }
319
320
321
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
334
335 @Override
336 public long getThroughputCalculationIntervalInMillis() {
337 return throughputCalculationInterval * 1000L;
338 }
339 }