1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.common;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.apache.mina.common.IdleStatus;
26 import org.apache.mina.common.Session;
27
28 /***
29 * Base implementation of {@link Session}.
30 *
31 * @author The Apache Directory Project (dev@directory.apache.org)
32 * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $
33 */
34 public abstract class BaseSession implements Session
35 {
36 private final Map attributes = new HashMap();
37 private final long creationTime;
38
39 private long readBytes;
40 private long writtenBytes;
41 private long writtenWriteRequests;
42
43 private long lastReadTime;
44 private long lastWriteTime;
45
46 private int idleCountForBoth;
47 private int idleCountForRead;
48 private int idleCountForWrite;
49
50 private long lastIdleTimeForBoth;
51 private long lastIdleTimeForRead;
52 private long lastIdleTimeForWrite;
53
54 protected BaseSession()
55 {
56 creationTime = lastReadTime = lastWriteTime =
57 lastIdleTimeForBoth = lastIdleTimeForRead = lastIdleTimeForWrite =
58 System.currentTimeMillis();
59 }
60
61 public void close()
62 {
63 this.close( false );
64 }
65
66 public Object getAttachment()
67 {
68 return attributes.get( "" );
69 }
70
71 public Object setAttachment( Object attachment )
72 {
73 synchronized( attributes )
74 {
75 return attributes.put( "", attachment );
76 }
77 }
78
79 public Object getAttribute( String key )
80 {
81 return attributes.get( key );
82 }
83
84 public Object setAttribute( String key, Object value )
85 {
86 synchronized( attributes )
87 {
88 return attributes.put( key, value );
89 }
90 }
91
92 public Object removeAttribute( String key )
93 {
94 synchronized( attributes )
95 {
96 return attributes.remove( key );
97 }
98 }
99
100 public Set getAttributeKeys() {
101 synchronized( attributes )
102 {
103 return attributes.keySet();
104 }
105 }
106
107 public long getReadBytes()
108 {
109 return readBytes;
110 }
111
112 public long getWrittenBytes()
113 {
114 return writtenBytes;
115 }
116
117 public long getWrittenWriteRequests()
118 {
119 return writtenWriteRequests;
120 }
121
122 public void increaseReadBytes( int increment )
123 {
124 readBytes += increment;
125 lastReadTime = System.currentTimeMillis();
126 }
127
128 public void increaseWrittenBytes( int increment )
129 {
130 writtenBytes += increment;
131 lastWriteTime = System.currentTimeMillis();
132 }
133
134 public void increaseWrittenWriteRequests()
135 {
136 writtenWriteRequests ++;
137 }
138
139 public long getCreationTime()
140 {
141 return creationTime;
142 }
143
144 public long getLastIoTime()
145 {
146 return Math.max( lastReadTime, lastWriteTime );
147 }
148
149 public long getLastReadTime()
150 {
151 return lastReadTime;
152 }
153
154 public long getLastWriteTime()
155 {
156 return lastWriteTime;
157 }
158
159 public boolean isIdle( IdleStatus status )
160 {
161 if( status == IdleStatus.BOTH_IDLE )
162 return idleCountForBoth > 0;
163
164 if( status == IdleStatus.READER_IDLE )
165 return idleCountForRead > 0;
166
167 if( status == IdleStatus.WRITER_IDLE )
168 return idleCountForWrite > 0;
169
170 throw new IllegalArgumentException( "Unknown idle status: " + status );
171 }
172
173 public int getIdleCount( IdleStatus status )
174 {
175 if( status == IdleStatus.BOTH_IDLE )
176 return idleCountForBoth;
177
178 if( status == IdleStatus.READER_IDLE )
179 return idleCountForRead;
180
181 if( status == IdleStatus.WRITER_IDLE )
182 return idleCountForWrite;
183
184 throw new IllegalArgumentException( "Unknown idle status: " + status );
185 }
186
187 public long getLastIdleTime( IdleStatus status )
188 {
189 if( status == IdleStatus.BOTH_IDLE )
190 return lastIdleTimeForBoth;
191
192 if( status == IdleStatus.READER_IDLE )
193 return lastIdleTimeForRead;
194
195 if( status == IdleStatus.WRITER_IDLE )
196 return lastIdleTimeForWrite;
197
198 throw new IllegalArgumentException( "Unknown idle status: " + status );
199 }
200
201 public void increaseIdleCount( IdleStatus status )
202 {
203 if( status == IdleStatus.BOTH_IDLE )
204 {
205 idleCountForBoth ++;
206 lastIdleTimeForBoth = System.currentTimeMillis();
207 }
208 else if( status == IdleStatus.READER_IDLE )
209 {
210 idleCountForRead ++;
211 lastIdleTimeForRead = System.currentTimeMillis();
212 }
213 else if( status == IdleStatus.WRITER_IDLE )
214 {
215 idleCountForWrite ++;
216 lastIdleTimeForWrite = System.currentTimeMillis();
217 }
218 else
219 throw new IllegalArgumentException( "Unknown idle status: "
220 + status );
221 }
222
223 public void resetIdleCount( IdleStatus status )
224 {
225 if( status == IdleStatus.BOTH_IDLE )
226 idleCountForBoth = 0;
227 else if( status == IdleStatus.READER_IDLE )
228 idleCountForRead = 0;
229 else if( status == IdleStatus.WRITER_IDLE )
230 idleCountForWrite = 0;
231 else
232 throw new IllegalArgumentException( "Unknown idle status: "
233 + status );
234 }
235 }