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.nio.ByteBuffer;
25  import java.util.concurrent.CountDownLatch;
26  
27  import org.apache.mina.api.IdleStatus;
28  import org.apache.mina.api.IoHandler;
29  import org.apache.mina.api.IoService;
30  import org.apache.mina.api.IoSession;
31  import org.apache.mina.core.BenchmarkClient;
32  import org.apache.mina.transport.nio.NioTcpClient;
33  
34  /**
35   * A MINA 3 TCP CLient.
36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
37   */
38  public class Mina3TcpBenchmarkClient implements BenchmarkClient {
39      // The TCP client
40      private NioTcpClient client;
41  
42      /**
43       * {@inheritDoc}
44       */
45      public void start(int port, final CountDownLatch counter, final byte[] data) throws IOException {
46          client = new NioTcpClient();
47          client.getSessionConfig().setSendBufferSize(64 * 1024);
48          client.getSessionConfig().setTcpNoDelay(true);
49          client.setIoHandler(new IoHandler() {
50              private void sendMessage(IoSession session, byte[] data) {
51                  ByteBuffer iobuf = ByteBuffer.wrap(data);
52                  session.write(iobuf);
53              }
54  
55              public void sessionOpened(IoSession session) {
56                  sendMessage(session, data);
57              }
58  
59              public void messageReceived(IoSession session, Object message) {
60                  if (message instanceof ByteBuffer) {
61                      ByteBuffer buffer = (ByteBuffer) message;
62  
63                      for (int i = 0; i < buffer.remaining(); ++i) {
64                          counter.countDown();
65                          long count = counter.getCount();
66                          if (count > 0) {
67                              sendMessage(session, data);
68                          }
69                      }
70                  }
71              }
72  
73              public void exceptionCaught(IoSession session, Exception cause) {
74                  cause.printStackTrace();
75              }
76  
77              @Override
78              public void sessionClosed(IoSession session) {
79              }
80  
81              @Override
82              public void sessionIdle(IoSession session, IdleStatus status) {
83              }
84  
85              @Override
86              public void messageSent(IoSession session, Object message) {
87              }
88  
89              @Override
90              public void serviceActivated(IoService service) {
91              }
92  
93              @Override
94              public void serviceInactivated(IoService service) {
95              }
96          });
97  
98          client.connect(new InetSocketAddress(port));
99      }
100 
101     /**
102      * {@inheritedDoc}
103      */
104     public void stop() throws IOException {
105         client.disconnect();
106     }
107 }