001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.example.tcp.perf;
021
022import java.net.InetSocketAddress;
023import java.util.concurrent.CountDownLatch;
024import java.util.concurrent.TimeUnit;
025
026import org.apache.mina.core.buffer.IoBuffer;
027import org.apache.mina.core.future.ConnectFuture;
028import org.apache.mina.core.service.IoConnector;
029import org.apache.mina.core.service.IoHandlerAdapter;
030import org.apache.mina.core.session.IdleStatus;
031import org.apache.mina.core.session.IoSession;
032import org.apache.mina.transport.socket.nio.NioSocketConnector;
033
034/**
035 * An UDP client taht just send thousands of small messages to a UdpServer. 
036 * 
037 * This class is used for performance test purposes. It does nothing at all, but send a message
038 * repetitly to a server.
039 * 
040 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
041 */
042public class TcpClient extends IoHandlerAdapter {
043    /** The connector */
044    private IoConnector connector;
045
046    /** The session */
047    private static IoSession session;
048    
049    /** The buffer containing the message to send */
050    private IoBuffer buffer = IoBuffer.allocate(8);
051    
052    /** Timers **/
053    private long t0;
054    private long t1;
055
056    /** the counter used for the sent messages */
057    private CountDownLatch counter;
058    
059    /**
060     * Create the UdpClient's instance
061     */
062    public TcpClient() {
063        connector = new NioSocketConnector();
064
065        connector.setHandler(this);
066        ConnectFuture connFuture = connector.connect(new InetSocketAddress("localhost", TcpServer.PORT));
067
068        connFuture.awaitUninterruptibly();
069
070        session = connFuture.getSession();
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    @Override
077    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
078        cause.printStackTrace();
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public void messageReceived(IoSession session, Object message) throws Exception {
086        long received = ((IoBuffer)message).getLong();
087        
088        if (received != counter.getCount()) {
089            System.out.println("Error !");
090            session.closeNow();
091        } else {
092            if (counter.getCount() == 0L) {
093                t1 = System.currentTimeMillis();
094                
095                System.out.println("------------->  end " + (t1 - t0));
096                session.closeNow();
097            } else {
098                counter.countDown();
099                
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}