View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.stream;
17  
18  import org.jboss.netty.buffer.ChannelBuffers;
19  import org.jboss.netty.channel.Channel;
20  import org.jboss.netty.channel.ChannelDownstreamHandler;
21  import org.jboss.netty.channel.ChannelEvent;
22  import org.jboss.netty.channel.ChannelFuture;
23  import org.jboss.netty.channel.ChannelFutureListener;
24  import org.jboss.netty.channel.ChannelHandler;
25  import org.jboss.netty.channel.ChannelHandlerContext;
26  import org.jboss.netty.channel.ChannelPipeline;
27  import org.jboss.netty.channel.ChannelStateEvent;
28  import org.jboss.netty.channel.ChannelUpstreamHandler;
29  import org.jboss.netty.channel.LifeCycleAwareChannelHandler;
30  import org.jboss.netty.channel.MessageEvent;
31  import org.jboss.netty.logging.InternalLogger;
32  import org.jboss.netty.logging.InternalLoggerFactory;
33  
34  import java.io.IOException;
35  import java.nio.channels.ClosedChannelException;
36  import java.util.Queue;
37  import java.util.concurrent.ConcurrentLinkedQueue;
38  import java.util.concurrent.atomic.AtomicBoolean;
39  
40  import static org.jboss.netty.channel.Channels.*;
41  
42  /**
43   * A {@link ChannelHandler} that adds support for writing a large data stream
44   * asynchronously neither spending a lot of memory nor getting
45   * {@link OutOfMemoryError}.  Large data streaming such as file
46   * transfer requires complicated state management in a {@link ChannelHandler}
47   * implementation.  {@link ChunkedWriteHandler} manages such complicated states
48   * so that you can send a large data stream without difficulties.
49   * <p>
50   * To use {@link ChunkedWriteHandler} in your application, you have to insert
51   * a new {@link ChunkedWriteHandler} instance:
52   * <pre>
53   * {@link ChannelPipeline} p = ...;
54   * p.addLast("streamer", <b>new {@link ChunkedWriteHandler}()</b>);
55   * p.addLast("handler", new MyHandler());
56   * </pre>
57   * Once inserted, you can write a {@link ChunkedInput} so that the
58   * {@link ChunkedWriteHandler} can pick it up and fetch the content of the
59   * stream chunk by chunk and write the fetched chunk downstream:
60   * <pre>
61   * {@link Channel} ch = ...;
62   * ch.write(new {@link ChunkedFile}(new File("video.mkv"));
63   * </pre>
64   *
65   * <h3>Sending a stream which generates a chunk intermittently</h3>
66   *
67   * Some {@link ChunkedInput} generates a chunk on a certain event or timing.
68   * Such {@link ChunkedInput} implementation often returns {@code null} on
69   * {@link ChunkedInput#nextChunk()}, resulting in the indefinitely suspended
70   * transfer.  To resume the transfer when a new chunk is available, you have to
71   * call {@link #resumeTransfer()}.
72   * @apiviz.landmark
73   * @apiviz.has org.jboss.netty.handler.stream.ChunkedInput oneway - - reads from
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       * Continues to fetch the chunks from the input.
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         // call flush if the channel is writable or not connected. flush(..) will take care of the rest
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                 // Continue writing when the channel becomes writable.
132                 flush(ctx, true);
133                 break;
134             case OPEN:
135                 if (!Boolean.TRUE.equals(cse.getValue())) {
136                     // Fail all pending writes
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             Object m = currentEvent.getMessage();
162             if (m instanceof ChunkedInput) {
163                 closeInput((ChunkedInput) m);
164             }
165 
166             // Trigger a ClosedChannelException
167             if (cause == null) {
168                 cause = new ClosedChannelException();
169             }
170             currentEvent.getFuture().setFailure(cause);
171         }
172 
173         if (cause != null) {
174             if (fireNow) {
175                 fireExceptionCaught(ctx.getChannel(), cause);
176             } else {
177                 fireExceptionCaughtLater(ctx.getChannel(), cause);
178             }
179         }
180     }
181 
182     private void flush(ChannelHandlerContext ctx, boolean fireNow) throws Exception {
183         boolean acquired;
184         final Channel channel = ctx.getChannel();
185         boolean suspend = false;
186         flushNeeded = true;
187         // use CAS to see if the have flush already running, if so we don't need to take futher actions
188         if (acquired = flush.compareAndSet(false, true)) {
189             flushNeeded = false;
190             try {
191                 if (!channel.isConnected()) {
192                     discard(ctx, fireNow);
193                     return;
194                 }
195 
196                 while (channel.isWritable()) {
197                     if (currentEvent == null) {
198                         currentEvent = queue.poll();
199                     }
200 
201                     if (currentEvent == null) {
202                         break;
203                     }
204 
205                     if (currentEvent.getFuture().isDone()) {
206                         // Skip the current request because the previous partial write
207                         // attempt for the current request has been failed.
208                         currentEvent = null;
209                     } else {
210                         final MessageEvent currentEvent = this.currentEvent;
211                         Object m = currentEvent.getMessage();
212                         if (m instanceof ChunkedInput) {
213                             final ChunkedInput chunks = (ChunkedInput) m;
214                             Object chunk;
215                             boolean endOfInput;
216                             try {
217                                 chunk = chunks.nextChunk();
218                                 endOfInput = chunks.isEndOfInput();
219                                 if (chunk == null) {
220                                     chunk = ChannelBuffers.EMPTY_BUFFER;
221                                     // No need to suspend when reached at the end.
222                                     suspend = !endOfInput;
223                                 } else {
224                                     suspend = false;
225                                 }
226                             } catch (Throwable t) {
227                                 this.currentEvent = null;
228 
229                                 currentEvent.getFuture().setFailure(t);
230                                 if (fireNow) {
231                                     fireExceptionCaught(ctx, t);
232                                 } else {
233                                     fireExceptionCaughtLater(ctx, t);
234                                 }
235 
236                                 closeInput(chunks);
237                                 break;
238                             }
239 
240                             if (suspend) {
241                                 // ChunkedInput.nextChunk() returned null and it has
242                                 // not reached at the end of input.  Let's wait until
243                                 // more chunks arrive.  Nothing to write or notify.
244                                 break;
245                             } else {
246                                 ChannelFuture writeFuture;
247                                 if (endOfInput) {
248                                     this.currentEvent = null;
249                                     writeFuture = currentEvent.getFuture();
250 
251                                     // Register a listener which will close the input once the write
252                                     // is complete. This is needed because the Chunk may have some
253                                     // resource bound that can not be closed before its not written
254                                     //
255                                     // See https://github.com/netty/netty/issues/303
256                                     writeFuture.addListener(new ChannelFutureListener() {
257 
258                                         public void operationComplete(ChannelFuture future) throws Exception {
259                                             closeInput(chunks);
260                                         }
261                                     });
262                                 } else {
263                                     writeFuture = future(channel);
264                                     writeFuture.addListener(new ChannelFutureListener() {
265                                         public void operationComplete(ChannelFuture future) throws Exception {
266                                             if (!future.isSuccess()) {
267                                                 currentEvent.getFuture().setFailure(future.getCause());
268                                                 closeInput((ChunkedInput) currentEvent.getMessage());
269                                             }
270                                         }
271                                     });
272                                 }
273 
274                                 write(
275                                         ctx, writeFuture, chunk,
276                                         currentEvent.getRemoteAddress());
277                             }
278                         } else {
279                             this.currentEvent = null;
280                             ctx.sendDownstream(currentEvent);
281                         }
282                     }
283 
284                     if (!channel.isConnected()) {
285                         discard(ctx, fireNow);
286                         return;
287                     }
288                 }
289             } finally {
290                 // mark the flush as done
291                 flush.set(false);
292             }
293         }
294 
295         if (acquired && (!channel.isConnected() || channel.isWritable() && !queue.isEmpty() && !suspend
296                 || flushNeeded)) {
297             flush(ctx, fireNow);
298         }
299     }
300 
301     static void closeInput(ChunkedInput chunks) {
302         try {
303             chunks.close();
304         } catch (Throwable t) {
305             if (logger.isWarnEnabled()) {
306                 logger.warn("Failed to close a chunked input.", t);
307             }
308         }
309     }
310 
311     public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
312         // nothing to do
313     }
314 
315     public void afterAdd(ChannelHandlerContext ctx) throws Exception {
316         // nothing to do
317     }
318 
319     public void beforeRemove(ChannelHandlerContext ctx) throws Exception {
320         // try to flush again a last time.
321         //
322         // See #304
323         flush(ctx, false);
324     }
325 
326     // This method should not need any synchronization as the ChunkedWriteHandler will not receive any new events
327     public void afterRemove(ChannelHandlerContext ctx) throws Exception {
328         // Fail all MessageEvent's that are left. This is needed because otherwise we would never notify the
329         // ChannelFuture and the registered FutureListener. See #304
330         Throwable cause = null;
331         boolean fireExceptionCaught = false;
332 
333         for (;;) {
334             MessageEvent currentEvent = this.currentEvent;
335 
336             if (this.currentEvent == null) {
337                 currentEvent = queue.poll();
338             } else {
339                 this.currentEvent = null;
340             }
341 
342             if (currentEvent == null) {
343                 break;
344             }
345 
346             Object m = currentEvent.getMessage();
347             if (m instanceof ChunkedInput) {
348                 closeInput((ChunkedInput) m);
349             }
350 
351             // Create exception
352             if (cause == null) {
353                 cause = new IOException("Unable to flush event, discarding");
354             }
355             currentEvent.getFuture().setFailure(cause);
356             fireExceptionCaught = true;
357         }
358 
359         if (fireExceptionCaught) {
360             fireExceptionCaughtLater(ctx.getChannel(), cause);
361         }
362     }
363 }