1 | /* |
2 | * @(#) $Id: ProtocolThreadPoolFilter.java 264677 2005-08-30 02:44:35Z 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.protocol.filter; |
20 | |
21 | import org.apache.mina.common.IdleStatus; |
22 | import org.apache.mina.common.Session; |
23 | import org.apache.mina.protocol.ProtocolHandler; |
24 | import org.apache.mina.protocol.ProtocolFilter; |
25 | import org.apache.mina.protocol.ProtocolSession; |
26 | import org.apache.mina.util.BaseThreadPool; |
27 | import org.apache.mina.util.EventType; |
28 | import org.apache.mina.util.ThreadPool; |
29 | |
30 | /** |
31 | * A Thread-pooling filter. This filter forwards {@link ProtocolHandler} events |
32 | * to its thread pool. |
33 | * |
34 | * @author Trustin Lee (trustin@apache.org) |
35 | * @version $Rev: 264677 $, $Date: 2005-08-30 11:44:35 +0900 $ |
36 | * |
37 | * @see ThreadPool |
38 | * @see BaseThreadPool |
39 | */ |
40 | public class ProtocolThreadPoolFilter extends BaseThreadPool implements ThreadPool, ProtocolFilter |
41 | { |
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 ProtocolThreadPoolFilter() |
48 | { |
49 | this( "ProtocolThreadPool" ); |
50 | } |
51 | |
52 | /** |
53 | * Creates a new instance of this filter with default thread pool settings. |
54 | * You'll have to invoke {@link #start()} method to start threads actually. |
55 | * |
56 | * @param threadNamePrefix the prefix of the thread names this pool will create. |
57 | */ |
58 | public ProtocolThreadPoolFilter( String threadNamePrefix ) |
59 | { |
60 | super( threadNamePrefix ); |
61 | } |
62 | |
63 | public void sessionOpened( NextFilter nextFilter, |
64 | ProtocolSession session ) |
65 | { |
66 | fireEvent( nextFilter, session, EventType.OPENED, null ); |
67 | } |
68 | |
69 | public void sessionClosed( NextFilter nextFilter, |
70 | ProtocolSession session ) |
71 | { |
72 | fireEvent( nextFilter, session, EventType.CLOSED, null ); |
73 | } |
74 | |
75 | public void sessionIdle( NextFilter nextFilter, |
76 | ProtocolSession session, IdleStatus status ) |
77 | { |
78 | fireEvent( nextFilter, session, EventType.IDLE, status ); |
79 | } |
80 | |
81 | public void exceptionCaught( NextFilter nextFilter, |
82 | ProtocolSession session, Throwable cause ) |
83 | { |
84 | fireEvent( nextFilter, session, EventType.EXCEPTION, cause ); |
85 | } |
86 | |
87 | public void messageReceived( NextFilter nextFilter, |
88 | ProtocolSession session, Object message ) |
89 | { |
90 | fireEvent( nextFilter, session, EventType.RECEIVED, message ); |
91 | } |
92 | |
93 | public void messageSent( NextFilter nextFilter, |
94 | ProtocolSession session, Object message ) |
95 | { |
96 | fireEvent( nextFilter, session, EventType.SENT, message ); |
97 | } |
98 | |
99 | protected void processEvent( Object nextFilter0, |
100 | Session session0, EventType type, |
101 | Object data ) |
102 | { |
103 | NextFilter nextFilter = ( NextFilter ) nextFilter0; |
104 | ProtocolSession session = ( ProtocolSession ) session0; |
105 | |
106 | if( type == EventType.RECEIVED ) |
107 | { |
108 | nextFilter.messageReceived( session, data ); |
109 | } |
110 | else if( type == EventType.SENT ) |
111 | { |
112 | nextFilter.messageSent( session, data ); |
113 | } |
114 | else if( type == EventType.EXCEPTION ) |
115 | { |
116 | nextFilter.exceptionCaught( session, ( Throwable ) data ); |
117 | } |
118 | else if( type == EventType.IDLE ) |
119 | { |
120 | nextFilter.sessionIdle( session, ( IdleStatus ) data ); |
121 | } |
122 | else if( type == EventType.OPENED ) |
123 | { |
124 | nextFilter.sessionOpened( session ); |
125 | } |
126 | else if( type == EventType.CLOSED ) |
127 | { |
128 | nextFilter.sessionClosed( session ); |
129 | } |
130 | } |
131 | |
132 | public void filterWrite( NextFilter nextFilter, ProtocolSession session, Object message ) |
133 | { |
134 | nextFilter.filterWrite( session, message ); |
135 | } |
136 | } |