View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.core.nio.tcp;
    21
    22  import java.io.IOException;
    23  import java.net.InetSocketAddress;
    24  import java.util.concurrent.CountDownLatch;
    25
    26  import org.apache.mina.core.BenchmarkClient;
    27  import org.jboss.netty.bootstrap.ClientBootstrap;
    28  import org.jboss.netty.buffer.ChannelBuffer;
    29  import org.jboss.netty.buffer.ChannelBuffers;
    30  import org.jboss.netty.channel.ChannelFactory;
    31  import org.jboss.netty.channel.ChannelHandlerContext;
    32  import org.jboss.netty.channel.ChannelPipeline;
    33  import org.jboss.netty.channel.ChannelPipelineFactory;
    34  import org.jboss.netty.channel.ChannelStateEvent;
    35  import org.jboss.netty.channel.Channels;
    36  import org.jboss.netty.channel.MessageEvent;
    37  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
    38  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
    39
    40  /**
    41   * A Netty 3 TCP CLient.
    42   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    43   */
    44  public class Netty3TcpBenchmarkClient implements BenchmarkClient {
    45
    46      private ChannelFactory factory;
    47
    48      /**
    49       *
    50       */
    51      public Netty3TcpBenchmarkClient() {
    52      }
    53
    54      /**
    55       * {@inheritedDoc}
    56       */
    57      public void start(final int port, final CountDownLatch counter, final byte[] data) throws IOException {
    58          factory = new NioClientSocketChannelFactory();
    59          ClientBootstrap bootstrap = new ClientBootstrap(factory);
    60          bootstrap.setOption("sendBufferSize", 64 * 1024);
    61          bootstrap.setOption("tcpNoDelay", true);
    62          bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    63              public ChannelPipeline getPipeline() throws Exception {
    64                  return Channels.pipeline(new SimpleChannelUpstreamHandler() {
    65                      private void sendMessage(ChannelHandlerContext ctx, byte[] data) {
    66                          ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(data);
    67                          ctx.getChannel().write(buffer);
    68                      }
    69
    70                      @Override
    71                      public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    72                          if (e.getMessage() instanceof ChannelBuffer) {
    73                              ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
    74                              for (int i = 0; i < buffer.readableBytes(); ++i) {
    75                                  counter.countDown();
    76                                  if (counter.getCount() > 0) {
    77                                      sendMessage(ctx, data);
    78                                  } else {
    79                                      ctx.getChannel().close();
    80                                  }
    81                              }
    82                          } else {
    83                              throw new IllegalArgumentException(e.getMessage().getClass().getName());
    84                          }
    85                      }
    86
    87                      @Override
    88                      public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    89                          sendMessage(ctx, data);
    90                      }
    91
    92                  });
    93              }
    94          });
    95          bootstrap.connect(new InetSocketAddress(port));
    96      }
    97
    98      /**
    99       * {@inheritedDoc}
    100      */
    101     public void stop() throws IOException {
    102         factory.releaseExternalResources();
    103     }
    104 }