1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.channel;
17
18 import java.net.SocketAddress;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.jboss.netty.logging.InternalLogger;
22 import org.jboss.netty.logging.InternalLoggerFactory;
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public class SimpleChannelHandler implements ChannelUpstreamHandler, ChannelDownstreamHandler {
75
76 private static final InternalLogger logger =
77 InternalLoggerFactory.getInstance(SimpleChannelHandler.class.getName());
78
79
80
81
82 public SimpleChannelHandler() {
83 super();
84 }
85
86
87
88
89
90
91 public void handleUpstream(
92 ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
93
94 if (e instanceof MessageEvent) {
95 messageReceived(ctx, (MessageEvent) e);
96 } else if (e instanceof WriteCompletionEvent) {
97 WriteCompletionEvent evt = (WriteCompletionEvent) e;
98 writeComplete(ctx, evt);
99 } else if (e instanceof ChildChannelStateEvent) {
100 ChildChannelStateEvent evt = (ChildChannelStateEvent) e;
101 if (evt.getChildChannel().isOpen()) {
102 childChannelOpen(ctx, evt);
103 } else {
104 childChannelClosed(ctx, evt);
105 }
106 } else if (e instanceof ChannelStateEvent) {
107 ChannelStateEvent evt = (ChannelStateEvent) e;
108 switch (evt.getState()) {
109 case OPEN:
110 if (Boolean.TRUE.equals(evt.getValue())) {
111 channelOpen(ctx, evt);
112 } else {
113 channelClosed(ctx, evt);
114 }
115 break;
116 case BOUND:
117 if (evt.getValue() != null) {
118 channelBound(ctx, evt);
119 } else {
120 channelUnbound(ctx, evt);
121 }
122 break;
123 case CONNECTED:
124 if (evt.getValue() != null) {
125 channelConnected(ctx, evt);
126 } else {
127 channelDisconnected(ctx, evt);
128 }
129 break;
130 case INTEREST_OPS:
131 channelInterestChanged(ctx, evt);
132 break;
133 default:
134 ctx.sendUpstream(e);
135 }
136 } else if (e instanceof ExceptionEvent) {
137 exceptionCaught(ctx, (ExceptionEvent) e);
138 } else {
139 ctx.sendUpstream(e);
140 }
141 }
142
143
144
145
146
147 public void messageReceived(
148 ChannelHandlerContext ctx, MessageEvent e) throws Exception {
149 ctx.sendUpstream(e);
150 }
151
152
153
154
155
156 public void exceptionCaught(
157 ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
158 if (this == ctx.getPipeline().getLast()) {
159 logger.warn(
160 "EXCEPTION, please implement " + getClass().getName() +
161 ".exceptionCaught() for proper handling.", e.getCause());
162 }
163 ctx.sendUpstream(e);
164 }
165
166
167
168
169 public void channelOpen(
170 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
171 ctx.sendUpstream(e);
172 }
173
174
175
176
177
178 public void channelBound(
179 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
180 ctx.sendUpstream(e);
181 }
182
183
184
185
186
187 public void channelConnected(
188 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
189 ctx.sendUpstream(e);
190 }
191
192
193
194
195
196 public void channelInterestChanged(
197 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
198 ctx.sendUpstream(e);
199 }
200
201
202
203
204 public void channelDisconnected(
205 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
206 ctx.sendUpstream(e);
207 }
208
209
210
211
212 public void channelUnbound(
213 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
214 ctx.sendUpstream(e);
215 }
216
217
218
219
220
221 public void channelClosed(
222 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
223 ctx.sendUpstream(e);
224 }
225
226
227
228
229 public void writeComplete(
230 ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {
231 ctx.sendUpstream(e);
232 }
233
234
235
236
237
238 public void childChannelOpen(
239 ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
240 ctx.sendUpstream(e);
241 }
242
243
244
245
246
247 public void childChannelClosed(
248 ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
249 ctx.sendUpstream(e);
250 }
251
252
253
254
255
256
257 public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
258 throws Exception {
259
260 if (e instanceof MessageEvent) {
261 writeRequested(ctx, (MessageEvent) e);
262 } else if (e instanceof ChannelStateEvent) {
263 ChannelStateEvent evt = (ChannelStateEvent) e;
264 switch (evt.getState()) {
265 case OPEN:
266 if (!Boolean.TRUE.equals(evt.getValue())) {
267 closeRequested(ctx, evt);
268 }
269 break;
270 case BOUND:
271 if (evt.getValue() != null) {
272 bindRequested(ctx, evt);
273 } else {
274 unbindRequested(ctx, evt);
275 }
276 break;
277 case CONNECTED:
278 if (evt.getValue() != null) {
279 connectRequested(ctx, evt);
280 } else {
281 disconnectRequested(ctx, evt);
282 }
283 break;
284 case INTEREST_OPS:
285 setInterestOpsRequested(ctx, evt);
286 break;
287 default:
288 ctx.sendDownstream(e);
289 }
290 } else {
291 ctx.sendDownstream(e);
292 }
293 }
294
295
296
297
298 public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
299 ctx.sendDownstream(e);
300 }
301
302
303
304
305 public void bindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
306 ctx.sendDownstream(e);
307
308 }
309
310
311
312
313 public void connectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
314 ctx.sendDownstream(e);
315
316 }
317
318
319
320
321 public void setInterestOpsRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
322 ctx.sendDownstream(e);
323 }
324
325
326
327
328 public void disconnectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
329 ctx.sendDownstream(e);
330
331 }
332
333
334
335
336 public void unbindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
337 ctx.sendDownstream(e);
338
339 }
340
341
342
343
344 public void closeRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
345 ctx.sendDownstream(e);
346 }
347 }