View Javadoc

1   /*
2    *   @(#) $Id: BaseSession.java 165120 2005-04-28 09:03:57Z 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: 165120 $, $Date: 2005-04-28 18:03:57 +0900 (?, 28  4? 2005) $
33   */
34  public abstract class BaseSession implements Session
35  {
36      private final Map attributes = new HashMap();
37  
38      private long readBytes;
39      
40      private long writtenBytes;
41      
42      private long lastReadTime;
43      
44      private long lastWriteTime;
45  
46      private boolean idleForBoth;
47  
48      private boolean idleForRead;
49  
50      private boolean idleForWrite;
51  
52  
53      protected BaseSession()
54      {
55      }
56      
57      public void close()
58      {
59          this.close( false );
60      }
61  
62      public Object getAttachment()
63      {
64          return attributes.get( "" );
65      }
66  
67      public Object setAttachment( Object attachment )
68      {
69          synchronized( attributes )
70          {
71              return attributes.put( "", attachment );
72          }
73      }
74  
75      public Object getAttribute( String key )
76      {
77          return attributes.get( key );
78      }
79  
80      public Object setAttribute( String key, Object value )
81      {
82          synchronized( attributes )
83          {
84              return attributes.put( key, value );
85          }
86      }
87      
88      public Object removeAttribute( String key )
89      {
90          synchronized( attributes )
91          {
92              return attributes.remove( key );
93          }
94      }
95  
96      public Set getAttributeKeys() {
97          synchronized( attributes )
98          {
99              return attributes.keySet();
100         }
101     }
102     
103     public long getReadBytes()
104     {
105         return readBytes;
106     }
107 
108     public long getWrittenBytes()
109     {
110         return writtenBytes;
111     }
112 
113     public void increaseReadBytes( int increment )
114     {
115         readBytes += increment;
116         lastReadTime = System.currentTimeMillis();
117     }
118 
119     public void increaseWrittenBytes( int increment )
120     {
121         writtenBytes += increment;
122         lastWriteTime = System.currentTimeMillis();
123     }
124 
125     public long getLastIoTime()
126     {
127         return Math.max( lastReadTime, lastWriteTime );
128     }
129 
130     public long getLastReadTime()
131     {
132         return lastReadTime;
133     }
134 
135     public long getLastWriteTime()
136     {
137         return lastWriteTime;
138     }
139 
140     public boolean isIdle( IdleStatus status )
141     {
142         if( status == IdleStatus.BOTH_IDLE )
143             return idleForBoth;
144 
145         if( status == IdleStatus.READER_IDLE )
146             return idleForRead;
147 
148         if( status == IdleStatus.WRITER_IDLE )
149             return idleForWrite;
150 
151         throw new IllegalArgumentException( "Unknown idle status: " + status );
152     }
153 
154     public void setIdle( IdleStatus status, boolean value )
155     {
156         if( status == IdleStatus.BOTH_IDLE )
157             idleForBoth = value;
158         else if( status == IdleStatus.READER_IDLE )
159             idleForRead = value;
160         else if( status == IdleStatus.WRITER_IDLE )
161             idleForWrite = value;
162         else
163             throw new IllegalArgumentException( "Unknown idle status: "
164                                                 + status );
165     }
166 }