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