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;
023
024import org.apache.mina.core.buffer.IoBuffer;
025import org.apache.mina.core.future.ConnectFuture;
026import org.apache.mina.core.service.IoConnector;
027import org.apache.mina.core.service.IoHandlerAdapter;
028import org.apache.mina.core.session.IdleStatus;
029import org.apache.mina.core.session.IoSession;
030import org.apache.mina.transport.socket.nio.NioSocketConnector;
031
032/**
033 * An UDP client taht just send thousands of small messages to a UdpServer. 
034 * 
035 * This class is used for performance test purposes. It does nothing at all, but send a message
036 * repetitly to a server.
037 * 
038 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
039 */
040public class TcpClient extends IoHandlerAdapter {
041    /** The connector */
042    private IoConnector connector;
043
044    /** The session */
045    private static IoSession session;
046
047    private boolean received = false;
048
049    /**
050     * Create the UdpClient's instance
051     */
052    public TcpClient() {
053        connector = new NioSocketConnector();
054
055        connector.setHandler(this);
056        ConnectFuture connFuture = connector.connect(new InetSocketAddress("localhost", TcpServer.PORT));
057
058        connFuture.awaitUninterruptibly();
059
060        session = connFuture.getSession();
061    }
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
068        cause.printStackTrace();
069    }
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public void messageReceived(IoSession session, Object message) throws Exception {
076        received = true;
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public void messageSent(IoSession session, Object message) throws Exception {
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public void sessionClosed(IoSession session) throws Exception {
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public void sessionCreated(IoSession session) throws Exception {
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
105    }
106
107    /**
108     * {@inheritDoc}
109     */
110    @Override
111    public void sessionOpened(IoSession session) throws Exception {
112    }
113
114    /**
115     * The main method : instanciates a client, and send N messages. We sleep 
116     * between each K messages sent, to avoid the server saturation.
117     * @param args The arguments
118     * @throws Exception If something went wrong
119     */
120    public static void main(String[] args) throws Exception {
121        TcpClient client = new TcpClient();
122
123        long t0 = System.currentTimeMillis();
124
125        for (int i = 0; i <= TcpServer.MAX_RECEIVED; i++) {
126            IoBuffer buffer = IoBuffer.allocate(4);
127            buffer.putInt(i);
128            buffer.flip();
129            session.write(buffer);
130
131            while (client.received == false) {
132                Thread.sleep(1);
133            }
134
135            client.received = false;
136
137            if (i % 10000 == 0) {
138                System.out.println("Sent " + i + " messages");
139            }
140        }
141
142        long t1 = System.currentTimeMillis();
143
144        System.out.println("Sent messages delay : " + (t1 - t0));
145
146        Thread.sleep(100000);
147
148        client.connector.dispose(true);
149    }
150}