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