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