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.example.discard;
17  
18  import java.util.logging.Level;
19  import java.util.logging.Logger;
20  
21  import org.jboss.netty.buffer.ChannelBuffer;
22  import org.jboss.netty.buffer.ChannelBuffers;
23  import org.jboss.netty.channel.Channel;
24  import org.jboss.netty.channel.ChannelEvent;
25  import org.jboss.netty.channel.ChannelHandlerContext;
26  import org.jboss.netty.channel.ChannelState;
27  import org.jboss.netty.channel.ChannelStateEvent;
28  import org.jboss.netty.channel.ExceptionEvent;
29  import org.jboss.netty.channel.MessageEvent;
30  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
31  import org.jboss.netty.channel.WriteCompletionEvent;
32  
33  /**
34   * Handles a client-side channel.
35   */
36  public class DiscardClientHandler extends SimpleChannelUpstreamHandler {
37  
38      private static final Logger logger = Logger.getLogger(
39              DiscardClientHandler.class.getName());
40  
41      private long transferredBytes;
42      private final byte[] content;
43  
44      public DiscardClientHandler(int messageSize) {
45          if (messageSize <= 0) {
46              throw new IllegalArgumentException(
47                      "messageSize: " + messageSize);
48          }
49          content = new byte[messageSize];
50      }
51  
52      public long getTransferredBytes() {
53          return transferredBytes;
54      }
55  
56      @Override
57      public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
58          if (e instanceof ChannelStateEvent) {
59              if (((ChannelStateEvent) e).getState() != ChannelState.INTEREST_OPS) {
60                  logger.info(e.toString());
61              }
62          }
63  
64          // Let SimpleChannelHandler call actual event handler methods below.
65          super.handleUpstream(ctx, e);
66      }
67  
68      @Override
69      public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
70          // Send the initial messages.
71          generateTraffic(e);
72      }
73  
74      @Override
75      public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) {
76          // Keep sending messages whenever the current socket buffer has room.
77          generateTraffic(e);
78      }
79  
80      @Override
81      public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
82          // Server is supposed to send nothing.  Therefore, do nothing.
83      }
84  
85      @Override
86      public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) {
87          transferredBytes += e.getWrittenAmount();
88      }
89  
90      @Override
91      public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
92          // Close the connection when an exception is raised.
93          logger.log(
94                  Level.WARNING,
95                  "Unexpected exception from downstream.",
96                  e.getCause());
97          e.getChannel().close();
98      }
99  
100     private void generateTraffic(ChannelStateEvent e) {
101         // Keep generating traffic until the channel is unwritable.
102         // A channel becomes unwritable when its internal buffer is full.
103         // If you keep writing messages ignoring this property,
104         // you will end up with an OutOfMemoryError.
105         Channel channel = e.getChannel();
106         while (channel.isWritable()) {
107             ChannelBuffer m = nextMessage();
108             if (m == null) {
109                 break;
110             }
111             channel.write(m);
112         }
113     }
114 
115     private ChannelBuffer nextMessage() {
116         return ChannelBuffers.wrappedBuffer(content);
117     }
118 }