View Javadoc

1   /***
2    * 
3    */
4   package org.apache.mina.protocol.io;
5   
6   import java.net.SocketAddress;
7   import java.util.Set;
8   
9   import org.apache.mina.common.IdleStatus;
10  import org.apache.mina.common.SessionConfig;
11  import org.apache.mina.common.TransportType;
12  import org.apache.mina.io.IoSession;
13  import org.apache.mina.protocol.ProtocolDecoder;
14  import org.apache.mina.protocol.ProtocolEncoder;
15  import org.apache.mina.protocol.ProtocolHandler;
16  import org.apache.mina.protocol.ProtocolFilterChain;
17  import org.apache.mina.protocol.ProtocolSession;
18  import org.apache.mina.protocol.ProtocolSessionFilterChain;
19  import org.apache.mina.protocol.ProtocolSessionManagerFilterChain;
20  import org.apache.mina.protocol.SimpleProtocolDecoderOutput;
21  import org.apache.mina.protocol.SimpleProtocolEncoderOutput;
22  import org.apache.mina.protocol.io.IoAdapter.SessionHandlerAdapter;
23  import org.apache.mina.util.Queue;
24  
25  /***
26   * A {@link ProtocolSession} that is backed by {@link IoSession}.
27   * 
28   * @author Trustin Lee (trustin@apache.org)
29   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
30   */
31  public class IoProtocolSession implements ProtocolSession
32  {
33      private final ProtocolSessionFilterChain filterChain;
34  
35      final IoSession session;
36  
37      final SessionHandlerAdapter shAdapter;
38  
39      final Queue writeQueue = new Queue();
40      
41      final ProtocolEncoder encoder;
42      
43      final ProtocolDecoder decoder;
44  
45      final SimpleProtocolEncoderOutput encOut;
46  
47      final SimpleProtocolDecoderOutput decOut;
48  
49      IoProtocolSession( ProtocolSessionManagerFilterChain managerFilterChain,
50                         IoSession session, SessionHandlerAdapter shAdapter )
51      {
52          this.filterChain = new ProtocolSessionFilterChain( managerFilterChain );
53          this.session = session;
54          this.shAdapter = shAdapter;
55          this.encoder = shAdapter.codecFactory.newEncoder();
56          this.decoder = shAdapter.codecFactory.newDecoder();
57          this.encOut = new SimpleProtocolEncoderOutput();
58          this.decOut = new SimpleProtocolDecoderOutput();
59      }
60      
61      /***
62       * Returns the {@link IoSession} this session is backed by.
63       */
64      public IoSession getIoSession()
65      {
66          return session;   
67      }
68      
69      public ProtocolFilterChain getFilterChain()
70      {
71          return filterChain;
72      }
73  
74      public ProtocolHandler getHandler()
75      {
76          return shAdapter.handler;
77      }
78  
79      public ProtocolEncoder getEncoder()
80      {
81          return encoder;
82      }
83  
84      public ProtocolDecoder getDecoder()
85      {
86          return decoder;
87      }
88  
89      public void close()
90      {
91          session.close();
92      }
93      
94      public void close( boolean wait )
95      {
96          session.close( wait );
97      }
98  
99      public Object getAttachment()
100     {
101         return session.getAttachment();
102     }
103 
104     public Object setAttachment( Object attachment )
105     {
106         return session.setAttachment( attachment );
107     }
108 
109     public Object getAttribute( String key )
110     {
111         return session.getAttribute( key );
112     }
113 
114     public Object setAttribute( String key, Object value )
115     {
116         return session.setAttribute( key, value );
117     }
118     
119     public Object removeAttribute( String key )
120     {
121         return session.removeAttribute( key );
122     }
123 
124     public Set getAttributeKeys()
125     {
126         return session.getAttributeKeys();
127     }
128 
129     public void write( Object message )
130     {
131         filterChain.filterWrite( this, message );
132     }
133 
134     public long getWrittenWriteRequests()
135     {
136         return session.getWrittenWriteRequests();
137     }
138 
139     public int getScheduledWriteRequests()
140     {
141         return session.getScheduledWriteRequests();
142     }
143 
144     public TransportType getTransportType()
145     {
146         return session.getTransportType();
147     }
148 
149     public boolean isConnected()
150     {
151         return session.isConnected();
152     }
153 
154     public SessionConfig getConfig()
155     {
156         return session.getConfig();
157     }
158 
159     public SocketAddress getRemoteAddress()
160     {
161         return session.getRemoteAddress();
162     }
163 
164     public SocketAddress getLocalAddress()
165     {
166         return session.getLocalAddress();
167     }
168 
169     public long getReadBytes()
170     {
171         return session.getReadBytes();
172     }
173 
174     public long getWrittenBytes()
175     {
176         return session.getWrittenBytes();
177     }
178 
179     public long getLastIoTime()
180     {
181         return session.getLastIoTime();
182     }
183 
184     public long getLastReadTime()
185     {
186         return session.getLastReadTime();
187     }
188 
189     public long getLastWriteTime()
190     {
191         return session.getLastWriteTime();
192     }
193 
194     public boolean isIdle( IdleStatus status )
195     {
196         return session.isIdle( status );
197     }
198 }