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 import java.util.List;
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 public class SimpleChannelUpstreamHandler implements ChannelUpstreamHandler {
57
58 private static final InternalLogger logger =
59 InternalLoggerFactory.getInstance(SimpleChannelUpstreamHandler.class.getName());
60
61
62
63
64
65
66 public void handleUpstream(
67 ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
68
69 if (e instanceof MessageEvent) {
70 messageReceived(ctx, (MessageEvent) e);
71 } else if (e instanceof WriteCompletionEvent) {
72 WriteCompletionEvent evt = (WriteCompletionEvent) e;
73 writeComplete(ctx, evt);
74 } else if (e instanceof ChildChannelStateEvent) {
75 ChildChannelStateEvent evt = (ChildChannelStateEvent) e;
76 if (evt.getChildChannel().isOpen()) {
77 childChannelOpen(ctx, evt);
78 } else {
79 childChannelClosed(ctx, evt);
80 }
81 } else if (e instanceof ChannelStateEvent) {
82 ChannelStateEvent evt = (ChannelStateEvent) e;
83 switch (evt.getState()) {
84 case OPEN:
85 if (Boolean.TRUE.equals(evt.getValue())) {
86 channelOpen(ctx, evt);
87 } else {
88 channelClosed(ctx, evt);
89 }
90 break;
91 case BOUND:
92 if (evt.getValue() != null) {
93 channelBound(ctx, evt);
94 } else {
95 channelUnbound(ctx, evt);
96 }
97 break;
98 case CONNECTED:
99 if (evt.getValue() != null) {
100 channelConnected(ctx, evt);
101 } else {
102 channelDisconnected(ctx, evt);
103 }
104 break;
105 case INTEREST_OPS:
106 channelInterestChanged(ctx, evt);
107 break;
108 default:
109 ctx.sendUpstream(e);
110 }
111 } else if (e instanceof ExceptionEvent) {
112 exceptionCaught(ctx, (ExceptionEvent) e);
113 } else {
114 ctx.sendUpstream(e);
115 }
116 }
117
118
119
120
121
122 public void messageReceived(
123 ChannelHandlerContext ctx, MessageEvent e) throws Exception {
124 ctx.sendUpstream(e);
125 }
126
127
128
129
130
131 public void exceptionCaught(
132 ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
133 ChannelPipeline pipeline = ctx.getPipeline();
134
135 ChannelHandler last = pipeline.getLast();
136 if (!(last instanceof ChannelUpstreamHandler) && ctx instanceof DefaultChannelPipeline) {
137
138 List<String> names = ctx.getPipeline().getNames();
139 for (int i = names.size() - 1; i >= 0; i--) {
140 ChannelHandler handler = ctx.getPipeline().get(names.get(i));
141 if (handler instanceof ChannelUpstreamHandler) {
142
143 last = handler;
144 break;
145 }
146 }
147 }
148 if (this == last) {
149 logger.warn(
150 "EXCEPTION, please implement " + getClass().getName() +
151 ".exceptionCaught() for proper handling.", e.getCause());
152 }
153 ctx.sendUpstream(e);
154 }
155
156
157
158
159
160
161
162
163 public void channelOpen(
164 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
165 ctx.sendUpstream(e);
166 }
167
168
169
170
171
172
173
174
175
176 public void channelBound(
177 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
178 ctx.sendUpstream(e);
179 }
180
181
182
183
184
185
186
187
188
189 public void channelConnected(
190 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
191 ctx.sendUpstream(e);
192 }
193
194
195
196
197
198 public void channelInterestChanged(
199 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
200 ctx.sendUpstream(e);
201 }
202
203
204
205
206 public void channelDisconnected(
207 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
208 ctx.sendUpstream(e);
209 }
210
211
212
213
214 public void channelUnbound(
215 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
216 ctx.sendUpstream(e);
217 }
218
219
220
221
222
223 public void channelClosed(
224 ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
225 ctx.sendUpstream(e);
226 }
227
228
229
230
231 public void writeComplete(
232 ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {
233 ctx.sendUpstream(e);
234 }
235
236
237
238
239
240 public void childChannelOpen(
241 ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
242 ctx.sendUpstream(e);
243 }
244
245
246
247
248
249 public void childChannelClosed(
250 ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
251 ctx.sendUpstream(e);
252 }
253 }