View Javadoc

1   /*
2    *   @(#) $Id: IoThreadPoolFilter.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.io.filter;
20  
21  import org.apache.mina.common.ByteBuffer;
22  import org.apache.mina.common.IdleStatus;
23  import org.apache.mina.common.Session;
24  import org.apache.mina.io.IoHandler;
25  import org.apache.mina.io.IoFilter;
26  import org.apache.mina.io.IoSession;
27  import org.apache.mina.util.BaseThreadPool;
28  import org.apache.mina.util.EventType;
29  import org.apache.mina.util.ThreadPool;
30  
31  /***
32   * A Thread-pooling filter.  This filter forwards {@link IoHandler} events 
33   * to its thread pool.
34   * 
35   * @author Trustin Lee (trustin@apache.org)
36   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
37   * 
38   * @see ThreadPool
39   * @see BaseThreadPool
40   */
41  public class IoThreadPoolFilter extends BaseThreadPool implements ThreadPool, IoFilter
42  {
43      /***
44       * Creates a new instanceof this filter with default thread pool settings.
45       * You'll have to invoke {@link #start()} method to start threads actually.
46       */
47      public IoThreadPoolFilter()
48      {
49      }
50  
51      public void sessionOpened( NextFilter nextFilter, IoSession session )
52      {
53          fireEvent( nextFilter, session, EventType.OPENED, null );
54      }
55  
56      public void sessionClosed( NextFilter nextFilter, IoSession session )
57      {
58          fireEvent( nextFilter, session, EventType.CLOSED, null );
59      }
60  
61      public void sessionIdle( NextFilter nextFilter, IoSession session,
62                              IdleStatus status )
63      {
64          fireEvent( nextFilter, session, EventType.IDLE, status );
65      }
66  
67      public void exceptionCaught( NextFilter nextFilter, IoSession session,
68                                  Throwable cause )
69      {
70          fireEvent( nextFilter, session, EventType.EXCEPTION, cause );
71      }
72  
73      public void dataRead( NextFilter nextFilter, IoSession session,
74                            ByteBuffer buf )
75      {
76          // MINA will release the buffer if this method returns.
77          buf.acquire();
78          fireEvent( nextFilter, session, EventType.READ, buf );
79      }
80  
81      public void dataWritten( NextFilter nextFilter, IoSession session,
82                              Object marker )
83      {
84          fireEvent( nextFilter, session, EventType.WRITTEN, marker );
85      }
86  
87      protected void processEvent( Object nextFilter0, Session session0,
88                                   EventType type, Object data )
89      {
90          NextFilter nextFilter = ( NextFilter ) nextFilter0;
91          IoSession session = ( IoSession ) session0;
92          if( type == EventType.READ )
93          {
94              ByteBuffer buf = ( ByteBuffer ) data;
95              nextFilter.dataRead( session, buf );
96              buf.release();
97          }
98          else if( type == EventType.WRITTEN )
99          {
100             nextFilter.dataWritten( session, data );
101         }
102         else if( type == EventType.EXCEPTION )
103         {
104             nextFilter.exceptionCaught( session, ( Throwable ) data );
105         }
106         else if( type == EventType.IDLE )
107         {
108             nextFilter.sessionIdle( session, ( IdleStatus ) data );
109         }
110         else if( type == EventType.OPENED )
111         {
112             nextFilter.sessionOpened( session );
113         }
114         else if( type == EventType.CLOSED )
115         {
116             nextFilter.sessionClosed( session );
117         }
118     }
119 
120     public void filterWrite( NextFilter nextFilter, IoSession session, ByteBuffer buf, Object marker ) throws Exception
121     {
122         nextFilter.filterWrite( session, buf, marker );
123     }
124 }