1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.stream;
17
18 import static org.jboss.netty.channel.Channels.*;
19
20 import java.io.IOException;
21 import java.nio.channels.ClosedChannelException;
22 import java.util.Queue;
23 import java.util.concurrent.ConcurrentLinkedQueue;
24 import java.util.concurrent.atomic.AtomicBoolean;
25
26 import org.jboss.netty.buffer.ChannelBuffers;
27 import org.jboss.netty.channel.Channel;
28 import org.jboss.netty.channel.ChannelDownstreamHandler;
29 import org.jboss.netty.channel.ChannelEvent;
30 import org.jboss.netty.channel.ChannelFuture;
31 import org.jboss.netty.channel.ChannelFutureListener;
32 import org.jboss.netty.channel.ChannelHandler;
33 import org.jboss.netty.channel.ChannelHandlerContext;
34 import org.jboss.netty.channel.ChannelPipeline;
35 import org.jboss.netty.channel.ChannelStateEvent;
36 import org.jboss.netty.channel.ChannelUpstreamHandler;
37 import org.jboss.netty.channel.LifeCycleAwareChannelHandler;
38 import org.jboss.netty.channel.MessageEvent;
39 import org.jboss.netty.logging.InternalLogger;
40 import org.jboss.netty.logging.InternalLoggerFactory;
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 public class ChunkedWriteHandler
76 implements ChannelUpstreamHandler, ChannelDownstreamHandler, LifeCycleAwareChannelHandler {
77
78 private static final InternalLogger logger =
79 InternalLoggerFactory.getInstance(ChunkedWriteHandler.class);
80
81 private final Queue<MessageEvent> queue = new ConcurrentLinkedQueue<MessageEvent>();
82
83 private volatile ChannelHandlerContext ctx;
84 private final AtomicBoolean flush = new AtomicBoolean(false);
85 private MessageEvent currentEvent;
86 private volatile boolean flushNeeded;
87
88
89
90
91 public void resumeTransfer() {
92 ChannelHandlerContext ctx = this.ctx;
93 if (ctx == null) {
94 return;
95 }
96
97 try {
98 flush(ctx, false);
99 } catch (Exception e) {
100 if (logger.isWarnEnabled()) {
101 logger.warn("Unexpected exception while sending chunks.", e);
102 }
103 }
104 }
105
106 public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
107 throws Exception {
108 if (!(e instanceof MessageEvent)) {
109 ctx.sendDownstream(e);
110 return;
111 }
112
113 boolean offered = queue.offer((MessageEvent) e);
114 assert offered;
115
116 final Channel channel = ctx.getChannel();
117
118
119 if (channel.isWritable() || !channel.isConnected()) {
120 this.ctx = ctx;
121 flush(ctx, false);
122 }
123 }
124
125 public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e)
126 throws Exception {
127 if (e instanceof ChannelStateEvent) {
128 ChannelStateEvent cse = (ChannelStateEvent) e;
129 switch (cse.getState()) {
130 case INTEREST_OPS:
131
132 flush(ctx, true);
133 break;
134 case OPEN:
135 if (!Boolean.TRUE.equals(cse.getValue())) {
136
137 flush(ctx, true);
138 }
139 break;
140 }
141 }
142 ctx.sendUpstream(e);
143 }
144
145 private void discard(ChannelHandlerContext ctx, boolean fireNow) {
146 ClosedChannelException cause = null;
147
148 for (;;) {
149 MessageEvent currentEvent = this.currentEvent;
150
151 if (this.currentEvent == null) {
152 currentEvent = queue.poll();
153 } else {
154 this.currentEvent = null;
155 }
156
157 if (currentEvent == null) {
158 break;
159 }
160
161
162 Object m = currentEvent.getMessage();
163 if (m instanceof ChunkedInput) {
164 closeInput((ChunkedInput) m);
165 }
166
167
168 if (cause == null) {
169 cause = new ClosedChannelException();
170 }
171 currentEvent.getFuture().setFailure(cause);
172
173 currentEvent = null;
174 }
175
176
177 if (cause != null) {
178 if (fireNow) {
179 fireExceptionCaught(ctx.getChannel(), cause);
180 } else {
181 fireExceptionCaughtLater(ctx.getChannel(), cause);
182 }
183 }
184 }
185
186 private void flush(ChannelHandlerContext ctx, boolean fireNow) throws Exception {
187 boolean acquired = false;
188 final Channel channel = ctx.getChannel();
189 boolean suspend = false;
190 flushNeeded = true;
191
192 if (acquired = flush.compareAndSet(false, true)) {
193 flushNeeded = false;
194 try {
195
196 if (!channel.isConnected()) {
197 discard(ctx, fireNow);
198 return;
199 }
200
201 while (channel.isWritable()) {
202 if (currentEvent == null) {
203 currentEvent = queue.poll();
204 }
205
206 if (currentEvent == null) {
207 break;
208 }
209
210 if (currentEvent.getFuture().isDone()) {
211
212
213 currentEvent = null;
214 } else {
215 final MessageEvent currentEvent = this.currentEvent;
216 Object m = currentEvent.getMessage();
217 if (m instanceof ChunkedInput) {
218 final ChunkedInput chunks = (ChunkedInput) m;
219 Object chunk;
220 boolean endOfInput;
221 try {
222 chunk = chunks.nextChunk();
223 endOfInput = chunks.isEndOfInput();
224 if (chunk == null) {
225 chunk = ChannelBuffers.EMPTY_BUFFER;
226
227 suspend = !endOfInput;
228 } else {
229 suspend = false;
230 }
231 } catch (Throwable t) {
232 this.currentEvent = null;
233
234 currentEvent.getFuture().setFailure(t);
235 if (fireNow) {
236 fireExceptionCaught(ctx, t);
237 } else {
238 fireExceptionCaughtLater(ctx, t);
239 }
240
241 closeInput(chunks);
242 break;
243 }
244
245 if (suspend) {
246
247
248
249 break;
250 } else {
251 ChannelFuture writeFuture;
252 if (endOfInput) {
253 this.currentEvent = null;
254 writeFuture = currentEvent.getFuture();
255
256
257
258
259
260
261 writeFuture.addListener(new ChannelFutureListener() {
262
263 public void operationComplete(ChannelFuture future) throws Exception {
264 closeInput(chunks);
265 }
266 });
267 } else {
268 writeFuture = future(channel);
269 writeFuture.addListener(new ChannelFutureListener() {
270 public void operationComplete(ChannelFuture future) throws Exception {
271 if (!future.isSuccess()) {
272 currentEvent.getFuture().setFailure(future.getCause());
273 closeInput((ChunkedInput) currentEvent.getMessage());
274 }
275 }
276 });
277 }
278
279 write(
280 ctx, writeFuture, chunk,
281 currentEvent.getRemoteAddress());
282 }
283 } else {
284 this.currentEvent = null;
285 ctx.sendDownstream(currentEvent);
286 }
287 }
288
289 if (!channel.isConnected()) {
290 discard(ctx, fireNow);
291 return;
292 }
293 }
294 } finally {
295
296 flush.set(false);
297 }
298
299 }
300
301 if (acquired && (!channel.isConnected() || channel.isWritable() && !queue.isEmpty() && !suspend
302 || flushNeeded)) {
303 flush(ctx, fireNow);
304 }
305 }
306
307 static void closeInput(ChunkedInput chunks) {
308 try {
309 chunks.close();
310 } catch (Throwable t) {
311 if (logger.isWarnEnabled()) {
312 logger.warn("Failed to close a chunked input.", t);
313 }
314 }
315 }
316
317 public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
318
319
320 }
321
322 public void afterAdd(ChannelHandlerContext ctx) throws Exception {
323
324
325 }
326
327 public void beforeRemove(ChannelHandlerContext ctx) throws Exception {
328
329
330
331 flush(ctx, false);
332 }
333
334
335 public void afterRemove(ChannelHandlerContext ctx) throws Exception {
336
337
338
339 Throwable cause = null;
340 boolean fireExceptionCaught = false;
341
342 for (;;) {
343 MessageEvent currentEvent = this.currentEvent;
344
345 if (this.currentEvent == null) {
346 currentEvent = queue.poll();
347 } else {
348 this.currentEvent = null;
349 }
350
351 if (currentEvent == null) {
352 break;
353 }
354
355 Object m = currentEvent.getMessage();
356 if (m instanceof ChunkedInput) {
357 closeInput((ChunkedInput) m);
358 }
359
360
361 if (cause == null) {
362 cause = new IOException("Unable to flush event, discarding");
363 }
364 currentEvent.getFuture().setFailure(cause);
365 fireExceptionCaught = true;
366
367 currentEvent = null;
368 }
369
370 if (fireExceptionCaught) {
371 fireExceptionCaughtLater(ctx.getChannel(), cause);
372 }
373 }
374 }