View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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.echo;
17  
18  import java.util.concurrent.atomic.AtomicLong;
19  import java.util.logging.Level;
20  import java.util.logging.Logger;
21  
22  import org.jboss.netty.buffer.ChannelBuffer;
23  import org.jboss.netty.buffer.ChannelBuffers;
24  import org.jboss.netty.channel.ChannelHandlerContext;
25  import org.jboss.netty.channel.ChannelStateEvent;
26  import org.jboss.netty.channel.ExceptionEvent;
27  import org.jboss.netty.channel.MessageEvent;
28  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
29  
30  /**
31   * Handler implementation for the echo client.  It initiates the ping-pong
32   * traffic between the echo client and server by sending the first message to
33   * the server.
34   *
35   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
36   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
37   *
38   * @version $Rev: 2121 $, $Date: 2010-02-02 09:38:07 +0900 (Tue, 02 Feb 2010) $
39   */
40  public class EchoClientHandler extends SimpleChannelUpstreamHandler {
41  
42      private static final Logger logger = Logger.getLogger(
43              EchoClientHandler.class.getName());
44  
45      private final ChannelBuffer firstMessage;
46      private final AtomicLong transferredBytes = new AtomicLong();
47  
48      /**
49       * Creates a client-side handler.
50       */
51      public EchoClientHandler(int firstMessageSize) {
52          if (firstMessageSize <= 0) {
53              throw new IllegalArgumentException(
54                      "firstMessageSize: " + firstMessageSize);
55          }
56          firstMessage = ChannelBuffers.buffer(firstMessageSize);
57          for (int i = 0; i < firstMessage.capacity(); i ++) {
58              firstMessage.writeByte((byte) i);
59          }
60      }
61  
62      public long getTransferredBytes() {
63          return transferredBytes.get();
64      }
65  
66      @Override
67      public void channelConnected(
68              ChannelHandlerContext ctx, ChannelStateEvent e) {
69          // Send the first message.  Server will not send anything here
70          // because the firstMessage's capacity is 0.
71          e.getChannel().write(firstMessage);
72      }
73  
74      @Override
75      public void messageReceived(
76              ChannelHandlerContext ctx, MessageEvent e) {
77          // Send back the received message to the remote peer.
78          transferredBytes.addAndGet(((ChannelBuffer) e.getMessage()).readableBytes());
79          e.getChannel().write(e.getMessage());
80      }
81  
82      @Override
83      public void exceptionCaught(
84              ChannelHandlerContext ctx, ExceptionEvent e) {
85          // Close the connection when an exception is raised.
86          logger.log(
87                  Level.WARNING,
88                  "Unexpected exception from downstream.",
89                  e.getCause());
90          e.getChannel().close();
91      }
92  }