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