1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.channel;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.logging.InternalLogger;
20 import org.jboss.netty.logging.InternalLoggerFactory;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class SimpleChannelUpstreamHandler implements ChannelUpstreamHandler {
60
61 private static final InternalLogger logger =
62 InternalLoggerFactory.getInstance(SimpleChannelUpstreamHandler.class.getName());
63
64
65
66
67 public SimpleChannelUpstreamHandler() {
68 super();
69 }
70
71
72
73
74
75
76 public void handleUpstream(
77 ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
78
79 if (e instanceof MessageEvent) {
80 messageReceived(ctx, (MessageEvent) e);
81 } else if (e instanceof WriteCompletionEvent) {
82 WriteCompletionEvent evt = (WriteCompletionEvent) e;
83 writeComplete(ctx, evt);
84 } else if (e instanceof ChildChannelStateEvent) {
85 ChildChannelStateEvent evt = (ChildChannelStateEvent) e;
86 if (evt.getChildChannel().isOpen()) {
87 childChannelOpen(ctx, evt);
88 } else {
89 childChannelClosed(ctx, evt);
90 }
91 } else if (e instanceof ChannelStateEvent) {
92 ChannelStateEvent evt = (ChannelStateEvent) e;
93 switch (evt.getState()) {
94 case OPEN:
95 if (Boolean.TRUE.equals(evt.getValue())) {
96 channelOpen(ctx, evt);
97 } else {
98 channelClosed(ctx, evt);
99 }
100 break;
101 case BOUND:
102 if (evt.getValue() != null) {
103 channelBound(ctx, evt);
104 } else {
105 channelUnbound(ctx, evt);
106 }
107 break;
108 case CONNECTED:
109 if (evt.getValue() != null) {
110 channelConnected(ctx, evt);
111 } else {
112 channelDisconnected(ctx, evt);
113 }
114 break;
115 case INTEREST_OPS:
116 channelInterestChanged(ctx, evt);
117 break;
118 default:
119 ctx.sendUpstream(e);
120 }
121 } else if (e instanceof ExceptionEvent) {
122 exceptionCaught(ctx, (ExceptionEvent) e);
123 } else {
124 ctx.sendUpstream(e);
125 }
126 }
127
128
129
130
131
132 public void messageReceived(
133 ChannelHandlerContext ctx, MessageEvent e) throws Exception {
134 ctx.sendUpstream(e);
135 }
136
137
138
139
140
141 public void exceptionCaught(
142 ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
143 if (this == ctx.getPipeline().getLast()) {
144 logger.warn(
145 "EXCEPTION, please implement " + getClass().getName() +
146 ".exceptionCaught() for proper handling.", e.getCause());
147 }
148 ctx.sendUpstream(e);
149 }
150
151
152
153
154
155
156
157 public void channelOpen(
158 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
159 ctx.sendUpstream(e);
160 }
161
162
163
164
165
166
167
168
169 public void channelBound(
170 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
171 ctx.sendUpstream(e);
172 }
173
174
175
176
177
178
179
180
181 public void channelConnected(
182 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
183 ctx.sendUpstream(e);
184 }
185
186
187
188
189
190 public void channelInterestChanged(
191 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
192 ctx.sendUpstream(e);
193 }
194
195
196
197
198 public void channelDisconnected(
199 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
200 ctx.sendUpstream(e);
201 }
202
203
204
205
206 public void channelUnbound(
207 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
208 ctx.sendUpstream(e);
209 }
210
211
212
213
214
215 public void channelClosed(
216 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
217 ctx.sendUpstream(e);
218 }
219
220
221
222
223 public void writeComplete(
224 ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {
225 ctx.sendUpstream(e);
226 }
227
228
229
230
231
232 public void childChannelOpen(
233 ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
234 ctx.sendUpstream(e);
235 }
236
237
238
239
240
241 public void childChannelClosed(
242 ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
243 ctx.sendUpstream(e);
244 }
245 }