View Javadoc

    1   /*
    2    * Copyright 2010 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.objectecho;
    17
    18  import java.util.ArrayList;
    19  import java.util.List;
    20  import java.util.concurrent.atomic.AtomicLong;
    21  import java.util.logging.Level;
    22  import java.util.logging.Logger;
    23
    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
    32  /**
    33   * Handler implementation for the object echo client.  It initiates the
    34   * ping-pong traffic between the object echo client and server by sending the
    35   * first message to the server.
    36   *
    37   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    38   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    39   *
    40   * @version $Rev: 2121 $, $Date: 2010-02-02 09:38:07 +0900 (Tue, 02 Feb 2010) $
    41   */
    42  public class ObjectEchoClientHandler extends SimpleChannelUpstreamHandler {
    43
    44      private static final Logger logger = Logger.getLogger(
    45              ObjectEchoClientHandler.class.getName());
    46
    47      private final List<Integer> firstMessage;
    48      private final AtomicLong transferredMessages = new AtomicLong();
    49
    50      /**
    51       * Creates a client-side handler.
    52       */
    53      public ObjectEchoClientHandler(int firstMessageSize) {
    54          if (firstMessageSize <= 0) {
    55              throw new IllegalArgumentException(
    56                      "firstMessageSize: " + firstMessageSize);
    57          }
    58          firstMessage = new ArrayList<Integer>(firstMessageSize);
    59          for (int i = 0; i < firstMessageSize; i ++) {
    60              firstMessage.add(Integer.valueOf(i));
    61          }
    62      }
    63
    64      public long getTransferredMessages() {
    65          return transferredMessages.get();
    66      }
    67
    68      @Override
    69      public void handleUpstream(
    70              ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
    71          if (e instanceof ChannelStateEvent &&
    72              ((ChannelStateEvent) e).getState() != ChannelState.INTEREST_OPS) {
    73              logger.info(e.toString());
    74          }
    75          super.handleUpstream(ctx, e);
    76      }
    77
    78      @Override
    79      public void channelConnected(
    80              ChannelHandlerContext ctx, ChannelStateEvent e) {
    81          // Send the first message if this handler is a client-side handler.
    82          e.getChannel().write(firstMessage);
    83      }
    84
    85      @Override
    86      public void messageReceived(
    87              ChannelHandlerContext ctx, MessageEvent e) {
    88          // Echo back the received object to the client.
    89          transferredMessages.incrementAndGet();
    90          e.getChannel().write(e.getMessage());
    91      }
    92
    93      @Override
    94      public void exceptionCaught(
    95              ChannelHandlerContext ctx, ExceptionEvent e) {
    96          logger.log(
    97                  Level.WARNING,
    98                  "Unexpected exception from downstream.",
    99                  e.getCause());
    100         e.getChannel().close();
    101     }
    102 }