View Javadoc

1   /*
2    *   @(#) $Id: BaseSession.java 210062 2005-07-11 03:52:38Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   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, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
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 Trustin Lee (trustin@apache.org)
32   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
33   */
34  public abstract class BaseSession implements Session
35  {
36      private final Map attributes = new HashMap();
37  
38      private long readBytes;
39      private long writtenBytes;
40      private long writtenWriteRequests;
41      
42      private long lastReadTime;
43      private long lastWriteTime;
44  
45      private boolean idleForBoth;
46      private boolean idleForRead;
47      private boolean idleForWrite;
48  
49  
50      protected BaseSession()
51      {
52          lastReadTime = lastWriteTime = System.currentTimeMillis();
53      }
54      
55      public void close()
56      {
57          this.close( false );
58      }
59  
60      public Object getAttachment()
61      {
62          return attributes.get( "" );
63      }
64  
65      public Object setAttachment( Object attachment )
66      {
67          synchronized( attributes )
68          {
69              return attributes.put( "", attachment );
70          }
71      }
72  
73      public Object getAttribute( String key )
74      {
75          return attributes.get( key );
76      }
77  
78      public Object setAttribute( String key, Object value )
79      {
80          synchronized( attributes )
81          {
82              return attributes.put( key, value );
83          }
84      }
85      
86      public Object removeAttribute( String key )
87      {
88          synchronized( attributes )
89          {
90              return attributes.remove( key );
91          }
92      }
93  
94      public Set getAttributeKeys() {
95          synchronized( attributes )
96          {
97              return attributes.keySet();
98          }
99      }
100     
101     public long getReadBytes()
102     {
103         return readBytes;
104     }
105 
106     public long getWrittenBytes()
107     {
108         return writtenBytes;
109     }
110 
111     public long getWrittenWriteRequests()
112     {
113         return writtenWriteRequests;
114     }
115     
116     public void increaseReadBytes( int increment )
117     {
118         readBytes += increment;
119         lastReadTime = System.currentTimeMillis();
120     }
121 
122     public void increaseWrittenBytes( int increment )
123     {
124         writtenBytes += increment;
125         lastWriteTime = System.currentTimeMillis();
126     }
127 
128     public void increaseWrittenWriteRequests()
129     {
130         writtenWriteRequests ++;
131     }
132 
133     public long getLastIoTime()
134     {
135         return Math.max( lastReadTime, lastWriteTime );
136     }
137 
138     public long getLastReadTime()
139     {
140         return lastReadTime;
141     }
142 
143     public long getLastWriteTime()
144     {
145         return lastWriteTime;
146     }
147 
148     public boolean isIdle( IdleStatus status )
149     {
150         if( status == IdleStatus.BOTH_IDLE )
151             return idleForBoth;
152 
153         if( status == IdleStatus.READER_IDLE )
154             return idleForRead;
155 
156         if( status == IdleStatus.WRITER_IDLE )
157             return idleForWrite;
158 
159         throw new IllegalArgumentException( "Unknown idle status: " + status );
160     }
161 
162     public void setIdle( IdleStatus status, boolean value )
163     {
164         if( status == IdleStatus.BOTH_IDLE )
165             idleForBoth = value;
166         else if( status == IdleStatus.READER_IDLE )
167             idleForRead = value;
168         else if( status == IdleStatus.WRITER_IDLE )
169             idleForWrite = value;
170         else
171             throw new IllegalArgumentException( "Unknown idle status: "
172                                                 + status );
173     }
174 }