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.example.tcp.perf;
21  
22  import java.net.InetSocketAddress;
23  import java.util.concurrent.CountDownLatch;
24  import java.util.concurrent.TimeUnit;
25  
26  import org.apache.mina.core.buffer.IoBuffer;
27  import org.apache.mina.core.future.ConnectFuture;
28  import org.apache.mina.core.service.IoConnector;
29  import org.apache.mina.core.service.IoHandlerAdapter;
30  import org.apache.mina.core.session.IdleStatus;
31  import org.apache.mina.core.session.IoSession;
32  import org.apache.mina.transport.socket.nio.NioSocketConnector;
33  
34  /**
35   * An UDP client taht just send thousands of small messages to a UdpServer. 
36   * 
37   * This class is used for performance test purposes. It does nothing at all, but send a message
38   * repetitly to a server.
39   * 
40   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
41   */
42  public class TcpClient extends IoHandlerAdapter {
43      /** The connector */
44      private IoConnector connector;
45  
46      /** The session */
47      private static IoSession session;
48      
49      /** The buffer containing the message to send */
50      private IoBuffer buffer = IoBuffer.allocate(8);
51      
52      /** Timers **/
53      private long t0;
54      private long t1;
55  
56      /** the counter used for the sent messages */
57      private CountDownLatch counter;
58      
59      /**
60       * Create the UdpClient's instance
61       */
62      public TcpClient() {
63          connector = new NioSocketConnector();
64  
65          connector.setHandler(this);
66          ConnectFuture connFuture = connector.connect(new InetSocketAddress("localhost", TcpServer.PORT));
67  
68          connFuture.awaitUninterruptibly();
69  
70          session = connFuture.getSession();
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
78          cause.printStackTrace();
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public void messageReceived(IoSession session, Object message) throws Exception {
86          long received = ((IoBuffer)message).getLong();
87          
88          if (received != counter.getCount()) {
89              System.out.println("Error !");
90              session.closeNow();
91          } else {
92              if (counter.getCount() == 0L) {
93                  t1 = System.currentTimeMillis();
94                  
95                  System.out.println("------------->  end " + (t1 - t0));
96                  session.closeNow();
97              } else {
98                  counter.countDown();
99                  
100                 buffer.flip();
101                 buffer.putLong(counter.getCount());
102                 buffer.flip();
103                 session.write(buffer);
104             }
105         }
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public void messageSent(IoSession session, Object message) throws Exception {
113         if (counter.getCount() % 10000 == 0) {
114             System.out.println("Sent " + counter + " messages");
115         }
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public void sessionClosed(IoSession session) throws Exception {
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public void sessionCreated(IoSession session) throws Exception {
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public void sessionOpened(IoSession session) throws Exception {
144     }
145 
146     /**
147      * The main method : instanciates a client, and send N messages. We sleep 
148      * between each K messages sent, to avoid the server saturation.
149      * @param args The arguments
150      * @throws Exception If something went wrong
151      */
152     public static void main(String[] args) throws Exception {
153         TcpClient client = new TcpClient();
154 
155         client.t0 = System.currentTimeMillis();
156         client.counter = new CountDownLatch(TcpServer.MAX_RECEIVED);
157         client.buffer.putLong(client.counter.getCount());
158         client.buffer.flip();
159         session.write(client.buffer);
160         int nbSeconds = 0;
161 
162         while ((client.counter.getCount() > 0) && (nbSeconds < 120)) {
163             // Wait for one second
164             client.counter.await(1, TimeUnit.SECONDS);
165             nbSeconds++;
166         }
167 
168         client.connector.dispose(true);
169     }
170 }